[tor-commits] [snowflake/master] Automate generating the list of available languages for the badge

arlo at torproject.org arlo at torproject.org
Mon Aug 26 19:15:10 UTC 2019


commit 1c550599b8239161fe8ea2bb8bb2541225770453
Author: Arlo Breault <arlolra at gmail.com>
Date:   Thu Aug 22 12:28:26 2019 -0400

    Automate generating the list of available languages for the badge
    
    Note that getMessage in the badge depends on having a complete set of
    translations, unlike the webextension, which will fallback to the
    default for a string.
---
 proxy/init-badge.js |  5 +----
 proxy/make.js       | 27 ++++++++++++++++++++++-----
 2 files changed, 23 insertions(+), 9 deletions(-)

diff --git a/proxy/init-badge.js b/proxy/init-badge.js
index 1acc923..ed6dee9 100644
--- a/proxy/init-badge.js
+++ b/proxy/init-badge.js
@@ -1,4 +1,4 @@
-/* global Util, Params, Config, UI, Broker, Snowflake, Popup, Parse */
+/* global Util, Params, Config, UI, Broker, Snowflake, Popup, Parse, availableLangs */
 
 /*
 UI
@@ -83,9 +83,6 @@ function setSnowflakeCookie(val, expires) {
 }
 
 const defaultLang = 'en_US';
-const availableLangs = new Set([
-  'en_US',
-]);
 
 // Resolve as in,
 // https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Internationalization#Localized_string_selection
diff --git a/proxy/make.js b/proxy/make.js
index 2aa0bf4..928c9ba 100755
--- a/proxy/make.js
+++ b/proxy/make.js
@@ -2,6 +2,7 @@
 
 /* global require, process */
 
+var { writeFileSync, readdirSync, statSync } = require('fs');
 var { execSync, spawn } = require('child_process');
 
 // All files required.
@@ -36,9 +37,11 @@ var SHARED_FILES = [
   '_locales',
 ];
 
-var concatJS = function(outDir, init, outFile) {
+var concatJS = function(outDir, init, outFile, pre) {
   var files = FILES.concat(`init-${init}.js`);
-  execSync(`cat ${files.join(' ')} > ${outDir}/${outFile}`);
+  var outPath = `${outDir}/${outFile}`;
+  writeFileSync(outPath, pre, 'utf8');
+  execSync(`cat ${files.join(' ')} >> ${outPath}`);
 };
 
 var copyTranslations = function(outDir) {
@@ -46,6 +49,20 @@ var copyTranslations = function(outDir) {
   execSync(`cp -rf translation/* ${outDir}/_locales/`);
 };
 
+var availableLangs = function() {
+  let out = "const availableLangs = new Set([\n";
+  let dirs = readdirSync('translation').filter((f) => {
+     const s = statSync(`translation/${f}`);
+     return s.isDirectory();
+  });
+  dirs.push('en_US');
+  dirs.sort();
+  dirs = dirs.map(d => `  '${d}',`);
+  out += dirs.join("\n");
+  out += "\n]);\n\n";
+  return out;
+};
+
 var tasks = new Map();
 
 var task = function(key, msg, func) {
@@ -76,7 +93,7 @@ task('build', 'build the snowflake proxy', function() {
   execSync(`rm -rf ${outDir}`);
   execSync(`cp -r ${STATIC}/ ${outDir}/`);
   copyTranslations(outDir);
-  concatJS(outDir, 'badge', 'embed.js');
+  concatJS(outDir, 'badge', 'embed.js', availableLangs());
   console.log('Snowflake prepared.');
 });
 
@@ -85,13 +102,13 @@ task('webext', 'build the webextension', function() {
   execSync(`git clean -f -x -d ${outDir}/`);
   execSync(`cp -r ${STATIC}/{${SHARED_FILES.join(',')}} ${outDir}/`, { shell: '/bin/bash' });
   copyTranslations(outDir);
-  concatJS(outDir, 'webext', 'snowflake.js');
+  concatJS(outDir, 'webext', 'snowflake.js', '');
   console.log('Webextension prepared.');
 });
 
 task('node', 'build the node binary', function() {
   execSync('mkdir -p build');
-  concatJS('build', 'node', 'snowflake.js');
+  concatJS('build', 'node', 'snowflake.js', '');
   console.log('Node prepared.');
 });
 





More information about the tor-commits mailing list