[tor-commits] [snowflake-webext/master] Remove the duplication around availableLangs

arlo at torproject.org arlo at torproject.org
Fri Feb 5 16:01:16 UTC 2021


commit 7dc400c32631d3f8058aaac19105fb32bd9dba51
Author: Arlo Breault <abreault at wikimedia.org>
Date:   Thu Feb 4 16:53:55 2021 -0500

    Remove the duplication around availableLangs
    
    This consolidates it from either a Set or an object literal to just a
    Map in all cases.
---
 make.js         | 17 ++++-------------
 static/index.js | 18 +++++++++---------
 2 files changed, 13 insertions(+), 22 deletions(-)

diff --git a/make.js b/make.js
index 1699e52..e00a88a 100755
--- a/make.js
+++ b/make.js
@@ -78,21 +78,12 @@ var getDirs = function() {
   return dirs;
 };
 
-var availableLangs = function() {
-  let out = "const availableLangs = new Set([\n";
-  let dirs = getDirs();
-  dirs = dirs.map(d => `  '${d}',`);
-  out += dirs.join("\n");
-  out += "\n]);\n\n";
-  return out;
-};
-
 var translatedLangs = function() {
-  let out = "const availableLangs = {\n";
+  let out = "const availableLangs = new Map([\n";
   let dirs = getDirs();
-  dirs = dirs.map(d => `'${d}': {"name": '${getDisplayName(d)}'},`);
+  dirs = dirs.map(d => `['${d}', '${getDisplayName(d)}'],`);
   out += dirs.join("\n");
-  out += "\n};\n\n";
+  out += "\n]);\n\n";
   return out;
 };
 
@@ -165,7 +156,7 @@ task('build', 'build the snowflake proxy', function() {
   execSync(`cp -r ${STATIC}/ ${outDir}/`);
   addVersion(outDir);
   copyTranslations(outDir);
-  concatJS(outDir, 'badge', 'embed.js', availableLangs());
+  concatJS(outDir, 'badge', 'embed.js', translatedLangs());
   writeFileSync(`${outDir}/index.js`, translatedLangs(), 'utf8');
   execSync(`cat ${STATIC}/index.js >> ${outDir}/index.js`);
   fillIndex(outDir);
diff --git a/static/index.js b/static/index.js
index 410fc1d..b296ae4 100644
--- a/static/index.js
+++ b/static/index.js
@@ -29,11 +29,11 @@ var getLang = function() {
     lang = override_lang;
   }
 
-  if (Object.prototype.hasOwnProperty.call(availableLangs, lang)) {
+  if (availableLangs.has(lang)) {
     return lang;
   }
   lang = lang.split('_')[0];
-  if (Object.prototype.hasOwnProperty.call(availableLangs, lang)) {
+  if (availableLangs.has(lang)) {
     return lang;
   }
   return defaultLang;
@@ -56,16 +56,16 @@ var fill = function(n, func) {
   }
 };
 
-
-fetch(`./_locales/${getLang()}/messages.json`)
+let gotLang = getLang();
+fetch(`./_locales/${gotLang}/messages.json`)
 .then((res) => {
   if (!res.ok) { return; }
   return res.json();
 })
 .then((json) => {
   var language = document.getElementById('language-switcher');
-  var lang = `${getLang()}`;
-  language.innerText = availableLangs[lang].name + ' (' + lang + ')';
+  var lang = `${gotLang}`;
+  language.innerText = `${availableLangs.get(lang)} (${lang})`;
   var messages = new Messages(json);
   fill(document.body, (m) => {
     return messages.getMessage(m);
@@ -73,11 +73,11 @@ fetch(`./_locales/${getLang()}/messages.json`)
 });
 
 // Populate language switcher list
-for (var lang in availableLangs) {
+availableLangs.forEach((name, lang) => {
   var languageList = document.getElementById('supported-languages');
   var link = document.createElement('a');
   link.setAttribute('href', '?lang='+lang);
   link.setAttribute('class', "dropdown-item");
-  link.innerText = availableLangs[lang].name + ' (' + lang + ')';
+  link.innerText = `${name} (${lang})`;
   languageList.lastChild.after(link);
-}
+});





More information about the tor-commits mailing list