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.
This commit is contained in:
Arlo Breault 2019-08-22 12:28:26 -04:00
parent 1e33ae830f
commit 1c550599b8
2 changed files with 23 additions and 9 deletions

View file

@ -1,4 +1,4 @@
/* global Util, Params, Config, UI, Broker, Snowflake, Popup, Parse */ /* global Util, Params, Config, UI, Broker, Snowflake, Popup, Parse, availableLangs */
/* /*
UI UI
@ -83,9 +83,6 @@ function setSnowflakeCookie(val, expires) {
} }
const defaultLang = 'en_US'; const defaultLang = 'en_US';
const availableLangs = new Set([
'en_US',
]);
// Resolve as in, // Resolve as in,
// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Internationalization#Localized_string_selection // https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Internationalization#Localized_string_selection

View file

@ -2,6 +2,7 @@
/* global require, process */ /* global require, process */
var { writeFileSync, readdirSync, statSync } = require('fs');
var { execSync, spawn } = require('child_process'); var { execSync, spawn } = require('child_process');
// All files required. // All files required.
@ -36,9 +37,11 @@ var SHARED_FILES = [
'_locales', '_locales',
]; ];
var concatJS = function(outDir, init, outFile) { var concatJS = function(outDir, init, outFile, pre) {
var files = FILES.concat(`init-${init}.js`); 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) { var copyTranslations = function(outDir) {
@ -46,6 +49,20 @@ var copyTranslations = function(outDir) {
execSync(`cp -rf translation/* ${outDir}/_locales/`); 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 tasks = new Map();
var task = function(key, msg, func) { var task = function(key, msg, func) {
@ -76,7 +93,7 @@ task('build', 'build the snowflake proxy', function() {
execSync(`rm -rf ${outDir}`); execSync(`rm -rf ${outDir}`);
execSync(`cp -r ${STATIC}/ ${outDir}/`); execSync(`cp -r ${STATIC}/ ${outDir}/`);
copyTranslations(outDir); copyTranslations(outDir);
concatJS(outDir, 'badge', 'embed.js'); concatJS(outDir, 'badge', 'embed.js', availableLangs());
console.log('Snowflake prepared.'); console.log('Snowflake prepared.');
}); });
@ -85,13 +102,13 @@ task('webext', 'build the webextension', function() {
execSync(`git clean -f -x -d ${outDir}/`); execSync(`git clean -f -x -d ${outDir}/`);
execSync(`cp -r ${STATIC}/{${SHARED_FILES.join(',')}} ${outDir}/`, { shell: '/bin/bash' }); execSync(`cp -r ${STATIC}/{${SHARED_FILES.join(',')}} ${outDir}/`, { shell: '/bin/bash' });
copyTranslations(outDir); copyTranslations(outDir);
concatJS(outDir, 'webext', 'snowflake.js'); concatJS(outDir, 'webext', 'snowflake.js', '');
console.log('Webextension prepared.'); console.log('Webextension prepared.');
}); });
task('node', 'build the node binary', function() { task('node', 'build the node binary', function() {
execSync('mkdir -p build'); execSync('mkdir -p build');
concatJS('build', 'node', 'snowflake.js'); concatJS('build', 'node', 'snowflake.js', '');
console.log('Node prepared.'); console.log('Node prepared.');
}); });