Use the Fetch API to download the l10n files
Given that the Fetch API is supported since Node.js 18 we should be able to use it when downloading l10n files, which allows us to simplify the code and to make it fully `async`.
This commit is contained in:
parent
fb9e438442
commit
2b407990e2
74
external/importL10n/locales.mjs
vendored
74
external/importL10n/locales.mjs
vendored
@ -14,7 +14,6 @@
|
||||
*/
|
||||
|
||||
import fs from "fs";
|
||||
import https from "https";
|
||||
import path from "path";
|
||||
|
||||
// Fetches all languages that have an *active* translation in mozilla-central.
|
||||
@ -22,47 +21,34 @@ import path from "path";
|
||||
|
||||
const DEFAULT_LOCALE = "en-US";
|
||||
|
||||
const EXCLUDE_LANG_CODES = ["ca-valencia", "ja-JP-mac"];
|
||||
const EXCLUDE_LANG_CODES = new Set(["ca-valencia", "ja-JP-mac"]);
|
||||
|
||||
function normalizeText(s) {
|
||||
return s.replaceAll(/\r\n?/g, "\n").replaceAll("\uFEFF", "");
|
||||
}
|
||||
|
||||
function downloadLanguageCodes() {
|
||||
async function downloadLanguageCodes() {
|
||||
console.log("Downloading language codes...\n");
|
||||
|
||||
const 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) {
|
||||
let content = "";
|
||||
response.setEncoding("utf8");
|
||||
response.on("data", function (chunk) {
|
||||
content += chunk;
|
||||
});
|
||||
response.on("end", function () {
|
||||
content = content.trim(); // Remove any leading/trailing white-space.
|
||||
const langCodes = normalizeText(content).split("\n");
|
||||
const response = await fetch(ALL_LOCALES);
|
||||
if (!response.ok) {
|
||||
throw new Error(response.statusText);
|
||||
}
|
||||
const content = await response.text();
|
||||
|
||||
// Remove any leading/trailing white-space.
|
||||
const langCodes = normalizeText(content.trim()).split("\n");
|
||||
// Remove all locales that we don't want to download below.
|
||||
for (const langCode of [DEFAULT_LOCALE, ...EXCLUDE_LANG_CODES]) {
|
||||
const i = langCodes.indexOf(langCode);
|
||||
if (i > -1) {
|
||||
langCodes.splice(i, 1);
|
||||
}
|
||||
}
|
||||
resolve(langCodes);
|
||||
});
|
||||
} else {
|
||||
resolve([]);
|
||||
}
|
||||
});
|
||||
});
|
||||
return langCodes.filter(
|
||||
langCode => langCode !== DEFAULT_LOCALE && !EXCLUDE_LANG_CODES.has(langCode)
|
||||
);
|
||||
}
|
||||
|
||||
function downloadLanguageFiles(root, langCode) {
|
||||
console.log("Downloading " + langCode + "...");
|
||||
async function downloadLanguageFiles(root, langCode) {
|
||||
console.log(`Downloading ${langCode}...`);
|
||||
|
||||
// Constants for constructing the URLs. Translations are taken from the
|
||||
// Nightly channel as those are the most recent ones.
|
||||
@ -71,41 +57,27 @@ function downloadLanguageFiles(root, langCode) {
|
||||
|
||||
// Defines which files to download for each language.
|
||||
const files = ["viewer.ftl"];
|
||||
let downloadsLeft = files.length;
|
||||
|
||||
const outputDir = path.join(root, langCode);
|
||||
if (!fs.existsSync(outputDir)) {
|
||||
fs.mkdirSync(outputDir);
|
||||
}
|
||||
|
||||
return new Promise(function (resolve) {
|
||||
// Download the necessary files for this language.
|
||||
files.forEach(function (fileName) {
|
||||
for (const fileName of files) {
|
||||
const outputPath = path.join(outputDir, fileName);
|
||||
const url =
|
||||
MOZ_CENTRAL_ROOT + langCode + MOZ_CENTRAL_PDFJS_DIR + fileName;
|
||||
const url = MOZ_CENTRAL_ROOT + langCode + MOZ_CENTRAL_PDFJS_DIR + fileName;
|
||||
|
||||
https.get(url, function (response) {
|
||||
const response = await fetch(url);
|
||||
if (!response.ok) {
|
||||
// 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) {
|
||||
let content = "";
|
||||
response.setEncoding("utf8");
|
||||
response.on("data", function (chunk) {
|
||||
content += chunk;
|
||||
});
|
||||
response.on("end", function () {
|
||||
continue;
|
||||
}
|
||||
const content = await response.text();
|
||||
|
||||
fs.writeFileSync(outputPath, normalizeText(content), "utf8");
|
||||
if (--downloadsLeft === 0) {
|
||||
resolve();
|
||||
}
|
||||
});
|
||||
} else if (--downloadsLeft === 0) {
|
||||
resolve();
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
async function downloadL10n(root) {
|
||||
|
Loading…
Reference in New Issue
Block a user