Update the gulp importl10n
command to fetch the active language codes
Rather than having to manually maintain a static list of language codes, it's much easier to simply fetch the active ones from `mozilla-central` instead. As part of this the code in `external/importL10n/locales.js` was modernized slightly, by using Promises/async functions to get rid of a bunch of annoying callbacks (which shouldn't be a problem for reasonably modern Node.js versions).
This commit is contained in:
parent
0786363b7c
commit
ed71ec3785
113
external/importL10n/locales.js
vendored
113
external/importL10n/locales.js
vendored
@ -19,27 +19,41 @@ var fs = require('fs');
|
||||
var https = require('https');
|
||||
var path = require('path');
|
||||
|
||||
// Defines all languages that have a translation at mozilla-central.
|
||||
// Fetches all languages that have an *active* translation in mozilla-central.
|
||||
// This is used in gulpfile.js for the `importl10n` command.
|
||||
var langCodes = [
|
||||
'ach', 'af', 'ak', 'an', 'ar', 'ast', 'az', 'be', 'bg', 'bn-BD', 'bn-IN',
|
||||
'br', 'brx', 'bs', 'ca', 'cak', 'cs', 'csb', 'cy', 'da', 'de', 'el', 'en-CA',
|
||||
'en-GB', 'eo', 'es-AR', 'es-CL', 'es-ES', 'es-MX', 'et', 'eu', 'fa', 'ff',
|
||||
'fi', 'fr', 'fy-NL', 'ga-IE', 'gd', 'gl', 'gn', 'gu-IN', 'he', 'hi-IN', 'hr',
|
||||
'hsb', 'hto', 'hu', 'hy-AM', 'ia', 'id', 'is', 'it', 'ja', 'ka', 'kab', 'kk',
|
||||
'km', 'kn', 'ko', 'kok', 'ks', 'ku', 'lg', 'lij', 'lo', 'lt', 'ltg', 'lv',
|
||||
'meh', 'mk', 'mn', 'mr', 'ms', 'my', 'nb-NO', 'ne-NP', 'nl', 'nn-NO', 'nso',
|
||||
'oc', 'pa-IN', 'pl', 'pt-BR', 'pt-PT', 'rm', 'ro', 'ru', 'rw', 'sah', 'sat',
|
||||
'si', 'sk', 'sl', 'son', 'sq', 'sr', 'sv-SE', 'sw', 'ta', 'ta-LK', 'te', 'th',
|
||||
'tl', 'tn', 'tr', 'tsz', 'uk', 'ur', 'uz', 'vi', 'wo', 'xh', 'zam', 'zh-CN',
|
||||
'zh-TW', 'zu'
|
||||
];
|
||||
|
||||
var EXCLUDE_LANG_CODES = ['ca-valencia', 'ja-JP-mac'];
|
||||
|
||||
function normalizeText(s) {
|
||||
return s.replace(/\r\n?/g, '\n').replace(/\uFEFF/g, '');
|
||||
}
|
||||
|
||||
function downloadLanguageFiles(root, langCode, callback) {
|
||||
function downloadLanguageCodes() {
|
||||
console.log('Downloading language codes...\n');
|
||||
|
||||
var ALL_LOCALES = 'https://hg.mozilla.org/mozilla-central/raw-file/tip/browser/locales/all-locales';
|
||||
|
||||
return new Promise(function(resolve) {
|
||||
https.get(ALL_LOCALES, function(response) {
|
||||
if (response.statusCode === 200) {
|
||||
var content = '';
|
||||
response.setEncoding('utf8');
|
||||
response.on('data', function(chunk) {
|
||||
content += chunk;
|
||||
});
|
||||
response.on('end', function() {
|
||||
content = content.trim(); // Remove any leading/trailing white-space.
|
||||
var langCodes = normalizeText(content).split('\n');
|
||||
resolve(langCodes);
|
||||
});
|
||||
} else {
|
||||
resolve([]);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function downloadLanguageFiles(root, langCode) {
|
||||
console.log('Downloading ' + langCode + '...');
|
||||
|
||||
// Constants for constructing the URLs. Translations are taken from the
|
||||
@ -56,48 +70,49 @@ function downloadLanguageFiles(root, langCode, callback) {
|
||||
fs.mkdirSync(outputDir);
|
||||
}
|
||||
|
||||
// Download the necessary files for this language.
|
||||
files.forEach(function(fileName) {
|
||||
var outputPath = path.join(outputDir, fileName);
|
||||
var url = MOZ_CENTRAL_ROOT + langCode + MOZ_CENTRAL_PDFJS_DIR + fileName;
|
||||
return new Promise(function(resolve) {
|
||||
// Download the necessary files for this language.
|
||||
files.forEach(function(fileName) {
|
||||
var outputPath = path.join(outputDir, fileName);
|
||||
var url = MOZ_CENTRAL_ROOT + langCode + MOZ_CENTRAL_PDFJS_DIR + fileName;
|
||||
|
||||
https.get(url, function(response) {
|
||||
// Not all files exist for each language. Files without translations have
|
||||
// been removed (https://bugzilla.mozilla.org/show_bug.cgi?id=1443175).
|
||||
if (response.statusCode === 200) {
|
||||
var content = '';
|
||||
response.setEncoding('utf8');
|
||||
response.on('data', function(chunk) {
|
||||
content += chunk;
|
||||
});
|
||||
response.on('end', function() {
|
||||
fs.writeFileSync(outputPath, normalizeText(content), 'utf8');
|
||||
downloadsLeft--;
|
||||
if (downloadsLeft === 0) {
|
||||
callback();
|
||||
https.get(url, function(response) {
|
||||
// Not all files exist for each language. Files without translations
|
||||
// have been removed (https://bugzilla.mozilla.org/show_bug.cgi?id=1443175).
|
||||
if (response.statusCode === 200) {
|
||||
var content = '';
|
||||
response.setEncoding('utf8');
|
||||
response.on('data', function(chunk) {
|
||||
content += chunk;
|
||||
});
|
||||
response.on('end', function() {
|
||||
fs.writeFileSync(outputPath, normalizeText(content), 'utf8');
|
||||
if (--downloadsLeft === 0) {
|
||||
resolve();
|
||||
}
|
||||
});
|
||||
} else {
|
||||
if (--downloadsLeft === 0) {
|
||||
resolve();
|
||||
}
|
||||
});
|
||||
} else {
|
||||
downloadsLeft--;
|
||||
if (downloadsLeft === 0) {
|
||||
callback();
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function downloadL10n(root, callback) {
|
||||
var i = 0;
|
||||
(function next() {
|
||||
if (i >= langCodes.length) {
|
||||
if (callback) {
|
||||
callback();
|
||||
}
|
||||
return;
|
||||
async function downloadL10n(root, callback) {
|
||||
var langCodes = await downloadLanguageCodes();
|
||||
|
||||
for (var langCode of langCodes) {
|
||||
if (!langCode || EXCLUDE_LANG_CODES.includes(langCode)) {
|
||||
continue;
|
||||
}
|
||||
downloadLanguageFiles(root, langCodes[i++], next);
|
||||
})();
|
||||
await downloadLanguageFiles(root, langCode);
|
||||
}
|
||||
if (callback) {
|
||||
callback();
|
||||
}
|
||||
}
|
||||
|
||||
exports.downloadL10n = downloadL10n;
|
||||
|
Loading…
x
Reference in New Issue
Block a user