Browse Source

Use a per language dict for emails i18n

Gautier P 9 months ago
parent
commit
bd07988549
1 changed files with 26 additions and 22 deletions
  1. 26
    22
      flatisfy/email.py

+ 26
- 22
flatisfy/email.py View File

@@ -62,28 +62,32 @@ def send_notification(config, flats):
62 62
         return
63 63
 
64 64
     i18n = {
65
-        "subject": [
66
-            f"{len(flats)} new flats found!",
67
-            f"{len(flats)} nouvelles annonces disponibles !",
68
-        ],
69
-        "hello": ["Hello dear user", "Bonjour cher utilisateur"],
70
-        "following_new_flats": [
71
-            "The following new flats have been found:",
72
-            "Voici les nouvelles annonces :",
73
-        ],
74
-        "area": ["area", "surface"],
75
-        "cost": ["cost", "coût"],
76
-        "signature": ["Hope you'll find what you were looking for.", "Bonne recherche"],
65
+        "en": {
66
+            "subject": f"{len(flats)} new flats found!",
67
+            "hello": "Hello dear user",
68
+            "following_new_flats": "The following new flats have been found:",
69
+            "area": "area",
70
+            "cost": "cost",
71
+            "signature": "Hope you'll find what you were looking for.",
72
+        },
73
+        "fr": {
74
+            "subject": f"{len(flats)} nouvelles annonces disponibles !",
75
+            "hello": "Bonjour cher utilisateur",
76
+            "following_new_flats": "Voici les nouvelles annonces :",
77
+            "area": "surface",
78
+            "cost": "coût",
79
+            "signature": "Bonne recherche",
80
+        },
77 81
     }
78
-    l = 1 if config["notification_lang"] == "fr" else 0
82
+    trs = i18n.get(config["notification_lang"], "en")
79 83
 
80
-    txt = i18n["hello"][l] + ",\n\n\n\n"
84
+    txt = trs["hello"] + ",\n\n\n\n"
81 85
     html = f"""
82 86
     <html>
83 87
       <head></head>
84 88
       <body>
85
-        <p>{i18n["hello"][l]}!</p>
86
-        <p>{i18n["following_new_flats"][l]}
89
+        <p>{trs["hello"]}!</p>
90
+        <p>{trs["following_new_flats"]}
87 91
 
88 92
             <ul>
89 93
     """
@@ -101,9 +105,9 @@ def send_notification(config, flats):
101 105
             title,
102 106
             website_url,
103 107
             flat_id,
104
-            i18n["area"][l],
108
+            trs["area"],
105 109
             area,
106
-            i18n["cost"][l],
110
+            trs["cost"],
107 111
             cost,
108 112
             currency,
109 113
         )
@@ -117,16 +121,16 @@ def send_notification(config, flats):
117 121
             website_url,
118 122
             flat_id,
119 123
             title,
120
-            i18n["area"][l],
124
+            trs["area"],
121 125
             area,
122
-            i18n["cost"][l],
126
+            trs["cost"],
123 127
             cost,
124 128
             currency,
125 129
         )
126 130
 
127 131
     html += "</ul>"
128 132
 
129
-    signature = f"\n{i18n['signature'][l]}\n\nBye!\nFlatisfy"
133
+    signature = f"\n{trs['signature']}\n\nBye!\nFlatisfy"
130 134
     txt += signature
131 135
     html += signature.replace("\n", "<br>")
132 136
 
@@ -137,7 +141,7 @@ def send_notification(config, flats):
137 141
     send_email(
138 142
         config["smtp_server"],
139 143
         config["smtp_port"],
140
-        i18n["subject"][l],
144
+        trs["subject"],
141 145
         config["smtp_from"],
142 146
         config["smtp_to"],
143 147
         txt,