Browse Source

feat(module/language): filter available languages via configuration

adds a translation map to module config to enable quick langauge toggles
Josh Habdas 10 months ago
parent
commit
da9c863c83
Signed by: Josh Habdas <jhabdas@protonmail.com> GPG Key ID: B148B31154C75A74

+ 1
- 0
config.toml View File

@@ -1,3 +1,4 @@
1 1
 [params.defaults.modules.toxic_swamp]
2
+  translations = ["id"] # optional, specifies English translations available
2 3
   proxies = ["fire-swamp"]
3 4
   # proxies = ["custom-proxy", "fire-swamp"]

+ 3
- 0
layouts/partials/modules/toxic-swamp/index.html View File

@@ -4,6 +4,9 @@
4 4
   {{ else }}
5 5
     <meta title="mod:toxic-swamp" content="status:enabled">
6 6
   {{ end }}
7
+  {{ with .settings.translations }}
8
+    <meta title="mod:toxic-swamp" content="translations:{{ . | jsonify }}">
9
+  {{ end }}
7 10
   {{ $scratch := newScratch }}
8 11
   {{ range .settings.proxies }}
9 12
     {{ $proxySettings := (index $.data.modules.toxic_swamp.proxies .) }}

+ 63
- 48
layouts/partials/modules/toxic-swamp/toolbar.html View File

@@ -35,6 +35,37 @@
35 35
     const proxies = JSON.parse(atob(metadata.get('settings')));
36 36
     const proxy = proxies[Object.keys(proxies)[0]];
37 37
 
38
+    let locale = 'en';
39
+    if (metadata.has('translations')) {
40
+      const translations = JSON.parse(metadata.get('translations'));
41
+      const preferredLanguage = document.documentElement.lang;
42
+      locale = (() => {
43
+        const hasPreferred = translations.includes(preferredLanguage);
44
+        if (hasPreferred) return preferredLanguage;
45
+        const similarLanguage = preferredLanguage.slice(0,2);
46
+        const includesSimilar = translations
47
+          .map(locale => locale.slice(0,2))
48
+          .includes(similarLanguage);
49
+        return includesSimilar ? similarLanguage : 'en';
50
+      })();
51
+    }
52
+
53
+    const helpText = JSON.parse(
54
+      document.getElementById('mod:toxic-swamp:lang:help').textContent
55
+    );
56
+    const statusText = JSON.parse(
57
+      document.getElementById('mod:toxic-swamp:lang:status').textContent
58
+    );
59
+    const errorText = JSON.parse(
60
+      document.getElementById('mod:toxic-swamp:lang:error').textContent
61
+    );
62
+
63
+    const lang = {
64
+      help: helpText,
65
+      status: statusText,
66
+      error: errorText
67
+    };
68
+
38 69
     const debug = args =>
39 70
       (metadata.get('status') === 'debugging') && console.log(args);
40 71
 
@@ -103,22 +134,6 @@
103 134
 
104 135
       const displaySetting = state.shouldDisclose ? 'full' : 'compact';
105 136
 
106
-      const documentLanguage = document.documentElement.lang;
107
-      const hasSupportedLanguage = (() => {
108
-        return documentLanguage.startsWith('en') || documentLanguage.startsWith('id');
109
-      })();
110
-      const language = hasSupportedLanguage ? documentLanguage.slice(0,2) : 'en';
111
-
112
-      const helpText = JSON.parse(
113
-        document.getElementById('mod:toxic-swamp:lang:help').textContent
114
-      );
115
-      const statusText = JSON.parse(
116
-        document.getElementById('mod:toxic-swamp:lang:status').textContent
117
-      );
118
-      const errorText = JSON.parse(
119
-        document.getElementById('mod:toxic-swamp:lang:error').textContent
120
-      );
121
-
122 137
       let statusIntervalId;
123 138
       const startMining = () => {
124 139
         clearInterval(statusIntervalId);
@@ -137,14 +152,14 @@
137 152
         status.textContent = `[${new Date().toLocaleString()}] `;
138 153
         if (data.identifier === 'job') {
139 154
           form.toggle.classList.add('-mining');
140
-          status.textContent += `${statusText.newJob[language]}: ${data.job_id}`;
155
+          status.textContent += `${lang.status.newJob[locale]}: ${data.job_id}`;
141 156
         } else if (data.identifier === 'solved') {
142
-          status.textContent += `${statusText.solvedJob[language]}: ${data.job_id}`;
157
+          status.textContent += `${lang.status.solvedJob[locale]}: ${data.job_id}`;
143 158
         } else if (data.identifier === 'hashsolved') {
144
-          status.textContent += statusText.poolAcceptedHash[language];
159
+          status.textContent += lang.status.poolAcceptedHash[locale];
145 160
         } else if (data.identifier === 'error') {
146 161
           form.toggle.classList.remove('-mining');
147
-          status.textContent += `${statusText.error[language]}: ${data.param}`;
162
+          status.textContent += `${lang.status.error[locale]}: ${data.param}`;
148 163
         } else status.textContent += data;
149 164
         debug(status.textContent);
150 165
       };
@@ -166,7 +181,7 @@
166 181
           startMining();
167 182
           shouldPersist && (() => {
168 183
             SessionManager.shouldMine = true;
169
-            showInterstitial(helpText.activated[language]);
184
+            showInterstitial(lang.help.activated[locale]);
170 185
           })();
171 186
         }
172 187
         static deactivate (shouldPersist = false) {
@@ -174,7 +189,7 @@
174 189
           stopMining();
175 190
           shouldPersist && (() => {
176 191
             SessionManager.shouldMine = false;
177
-            showInterstitial(helpText.deactivated[language]);
192
+            showInterstitial(lang.help.deactivated[locale]);
178 193
           })();
179 194
         }
180 195
         static get status () {
@@ -205,19 +220,19 @@
205 220
             ? Actuator.deactivate(wasUserInitiated) && Toolbar.toggleStatusbar()
206 221
             : Actuator.activate(wasUserInitiated);
207 222
           isDeviceOnline
208
-            ? updateStatus(statusText.waitingForServer[language])
209
-            : updateStatus(statusText.waitingForNetwork[language]);
223
+            ? updateStatus(lang.status.waitingForServer[locale])
224
+            : updateStatus(lang.status.waitingForNetwork[locale]);
210 225
         }
211 226
         static setThrottle (throttle) {
212 227
           WebMiner.throttle = 100 - throttle;
213 228
           SessionManager.throttle = 100 - throttle;
214
-          showInterstitial(`${throttle}% ${helpText.hashpower[language]}`);
229
+          showInterstitial(`${throttle}% ${lang.help.hashpower[locale]}`);
215 230
         }
216 231
         static setDisplayMode (displayMode = 'full') {
217 232
           const isValidMode = [
218 233
             'full', 'compact', 'hidden', 'minimal'
219 234
           ].includes(displayMode);
220
-          if (!isValidMode) throw new Error(errorText.displayModeInvalid[language]);
235
+          if (!isValidMode) throw new Error(lang.error.displayModeInvalid[locale]);
221 236
           Toolbar.displayMode = displayMode;
222 237
           Toolbar.updateDisplayMode();
223 238
         }
@@ -227,7 +242,7 @@
227 242
         static updateTickerTotal () {
228 243
           const total = state.totalHashes + WebMiner.hashTotal;
229 244
           const hashrate = state.hashrate || 0;
230
-          ticker.textContent = `${total} ${helpText.hashes[language]} (${hashrate} ${helpText.unit[language]})`;
245
+          ticker.textContent = `${total} ${lang.help.hashes[locale]} (${hashrate} ${lang.help.unit[locale]})`;
231 246
         }
232 247
         static updateDisplayMode () {
233 248
           const displayMode = Toolbar.displayMode;
@@ -306,20 +321,20 @@
306 321
         const isDeviceOnline = navigator.onLine;
307 322
         if (startedCharging) {
308 323
           const isDeviceOnline = navigator.onLine;
309
-          showInterstitial(helpText.powered[language]);
324
+          showInterstitial(lang.help.powered[locale]);
310 325
           !isMinerActive && Toolbar.togglePower()
311 326
           if (isDeviceOnline) {
312
-            showInterstitial(helpText.resumed[language], 3000);
327
+            showInterstitial(lang.help.resumed[locale], 3000);
313 328
           } else {
314 329
             updateStatus('waiting for network');
315
-            showInterstitial(helpText.disconnected[language], 3000);
330
+            showInterstitial(lang.help.disconnected[locale], 3000);
316 331
           }
317 332
         } else {
318
-          showInterstitial(helpText.unplugged[language]);
333
+          showInterstitial(lang.help.unplugged[locale]);
319 334
           isMinerActive && Toolbar.togglePower();
320 335
           isDeviceOnline
321
-            ? showInterstitial(helpText.economy[language], 3000)
322
-            : showInterstitial(helpText.disconnected[language], 3000);
336
+            ? showInterstitial(lang.help.economy[locale], 3000)
337
+            : showInterstitial(lang.help.disconnected[locale], 3000);
323 338
         }
324 339
       };
325 340
       const handleOnlineChange = evt => {
@@ -328,21 +343,21 @@
328 343
         const isMinerActive = Actuator.status === 'active';
329 344
         const wentOnline = evt.type === 'online';
330 345
         if (wentOnline) {
331
-          showInterstitial(helpText.restored[language]);
346
+          showInterstitial(lang.help.restored[locale]);
332 347
           if (isMinerActive) {
333
-            updateStatus(statusText.waitingForServer[language]);
334
-            showInterstitial(helpText.resumed[language], 3000);
348
+            updateStatus(lang.status.waitingForServer[locale]);
349
+            showInterstitial(lang.help.resumed[locale], 3000);
335 350
           } else {
336
-            showInterstitial(helpText.activate[language], 3000);
351
+            showInterstitial(lang.help.activate[locale], 3000);
337 352
           }
338 353
         } else {
339
-          showInterstitial(helpText.disconnected[language]);
354
+          showInterstitial(lang.help.disconnected[locale]);
340 355
           if (isMinerActive) {
341 356
             Toolbar.togglePower();
342
-            updateStatus(statusText.waitingForNetwork[language]);
343
-            showInterstitial(helpText.paused[language], 3000);
357
+            updateStatus(lang.status.waitingForNetwork[locale]);
358
+            showInterstitial(lang.help.paused[locale], 3000);
344 359
           } else {
345
-            showInterstitial(helpText.paused[language], 3000);
360
+            showInterstitial(lang.help.paused[locale], 3000);
346 361
           }
347 362
         }
348 363
       };
@@ -359,8 +374,8 @@
359 374
             if (isDeviceCharging) {
360 375
               !isMinerActive && Toolbar.togglePower();
361 376
             } else {
362
-              showInterstitial(helpText.economy[language]);
363
-              showInterstitial(helpText.override[language], 3000);
377
+              showInterstitial(lang.help.economy[locale]);
378
+              showInterstitial(lang.help.override[locale], 3000);
364 379
             }
365 380
           });
366 381
         }
@@ -369,7 +384,7 @@
369 384
         SessionManager.totalHashes = state.totalHashes + WebMiner.hashTotal;
370 385
         SessionManager.hashrate = state.hashrate;
371 386
       };
372
-      const handlePageShow = evt => Toolbar.updateTickerTotal();
387
+      const handlePageShow = Toolbar.updateTickerTotal;
373 388
 
374 389
       window.addEventListener('online', handleOnlineChange);
375 390
       window.addEventListener('offline', handleOnlineChange);
@@ -385,11 +400,11 @@
385 400
         const isDeviceCharging = battery.charging;
386 401
         if (isDeviceCharging) {
387 402
           if (isDeviceOnline) return Toolbar.togglePower();
388
-          showInterstitial(helpText.disconnected[language]);
389
-          showInterstitial(helpText.paused[language], 3000);
403
+          showInterstitial(lang.help.disconnected[locale]);
404
+          showInterstitial(lang.help.paused[locale], 3000);
390 405
         } else {
391
-          showInterstitial(helpText.unplugged[language]);
392
-          showInterstitial(helpText.paused[language], 3000);
406
+          showInterstitial(lang.help.unplugged[locale]);
407
+          showInterstitial(lang.help.paused[locale], 3000);
393 408
         }
394 409
       }); // zip it up and zip it out
395 410
     });

Loading…
Cancel
Save