2023-04-03 11:04:32 +00:00
|
|
|
import { extractTerms, ftlToJSON, JSONToFtl } from 'ftl-tx';
|
|
|
|
import fs from 'fs-extra';
|
|
|
|
import { dirname, join } from 'path';
|
|
|
|
import { fileURLToPath } from 'url';
|
2023-05-29 10:02:09 +00:00
|
|
|
import { ftlFileBaseNames as sourceFileBaseNames } from './config.js';
|
2023-04-03 11:04:32 +00:00
|
|
|
import { onError, onProgress, onSuccess } from './utils.js';
|
|
|
|
import { exit } from 'process';
|
|
|
|
|
|
|
|
const ROOT = join(dirname(fileURLToPath(import.meta.url)), '..');
|
|
|
|
const localesDir = join(ROOT, 'chrome', 'locale');
|
|
|
|
const sourceDir = join(localesDir, 'en-US', 'zotero');
|
|
|
|
const termsSourceFTLPath = join(ROOT, 'app', 'assets', 'branding', 'locale', 'brand.ftl');
|
|
|
|
|
2023-04-13 05:22:29 +00:00
|
|
|
function getLocaleDir(locale) {
|
|
|
|
return join(localesDir, locale, 'zotero');
|
|
|
|
}
|
2023-04-03 11:04:32 +00:00
|
|
|
|
|
|
|
async function getFTL() {
|
|
|
|
const t1 = performance.now();
|
2023-05-29 10:02:09 +00:00
|
|
|
|
2023-04-03 11:04:32 +00:00
|
|
|
if (!(await fs.pathExists(termsSourceFTLPath))) {
|
|
|
|
console.error(`Required file ${termsSourceFTLPath} does not exist`);
|
|
|
|
exit(1);
|
|
|
|
}
|
2023-05-29 10:02:09 +00:00
|
|
|
|
2023-04-03 11:04:32 +00:00
|
|
|
const terms = extractTerms(await fs.readFile(termsSourceFTLPath, 'utf-8'));
|
2023-05-29 10:02:09 +00:00
|
|
|
|
2023-04-13 05:22:29 +00:00
|
|
|
const foundLocales = (await fs.readdir(localesDir, { withFileTypes: true }))
|
2023-04-03 11:04:32 +00:00
|
|
|
.filter(dirent => dirent.isDirectory())
|
2023-04-13 05:22:29 +00:00
|
|
|
.map(dirent => dirent.name)
|
|
|
|
// Valid locale codes only
|
|
|
|
.filter(name => /^[a-z]{2}(-[A-Z]{2})?$/.test(name));
|
2023-05-29 10:02:09 +00:00
|
|
|
|
2023-04-03 11:04:32 +00:00
|
|
|
let count = 0;
|
2023-05-29 10:02:09 +00:00
|
|
|
for (let sourceFileBaseName of sourceFileBaseNames) {
|
|
|
|
const fallbackJSONPath = join(sourceDir, sourceFileBaseName + '.json');
|
|
|
|
if (!(await fs.pathExists(fallbackJSONPath))) {
|
|
|
|
console.error(`File ${fallbackJSONPath} does not exist -- please run 'ftl-to-json' first`);
|
|
|
|
exit(1);
|
2023-04-03 11:04:32 +00:00
|
|
|
}
|
2023-05-29 10:02:09 +00:00
|
|
|
|
|
|
|
const fallbackJSON = await fs.readJSON(fallbackJSONPath);
|
|
|
|
|
|
|
|
for (let locale of foundLocales) {
|
|
|
|
// Skip source locale
|
|
|
|
if (locale == 'en-US') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ftlFilePath = join(getLocaleDir(locale), sourceFileBaseName + '.ftl');
|
|
|
|
let jsonFromLocalFTL = {};
|
|
|
|
try {
|
|
|
|
const ftl = await fs.readFile(ftlFilePath, 'utf8');
|
|
|
|
jsonFromLocalFTL = ftlToJSON(ftl, { transformTerms: false, storeTermsInJSON: false });
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
// no local .ftl file
|
|
|
|
}
|
|
|
|
|
|
|
|
const jsonFilePath = join(getLocaleDir(locale), sourceFileBaseName + `.json`);
|
|
|
|
let jsonFromTransifex = {};
|
|
|
|
try {
|
|
|
|
const json = await fs.readJSON(jsonFilePath);
|
|
|
|
jsonFromTransifex = json;
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
// no .json file from transifex
|
|
|
|
}
|
|
|
|
|
|
|
|
const mergedJSON = { ...fallbackJSON, ...jsonFromLocalFTL, ...jsonFromTransifex };
|
|
|
|
const ftl = JSONToFtl(mergedJSON, { addTermsToFTL: false, storeTermsInJSON: false, transformTerms: false, terms });
|
|
|
|
|
|
|
|
const outFtlPath = join(getLocaleDir(locale), sourceFileBaseName + '.ftl');
|
|
|
|
await fs.outputFile(outFtlPath, ftl);
|
|
|
|
onProgress(outFtlPath, outFtlPath, 'ftl');
|
|
|
|
count++;
|
2023-04-03 11:04:32 +00:00
|
|
|
}
|
|
|
|
}
|
2023-05-29 10:02:09 +00:00
|
|
|
|
2023-04-03 11:04:32 +00:00
|
|
|
const t2 = performance.now();
|
|
|
|
return ({
|
|
|
|
action: 'ftl',
|
|
|
|
count,
|
2023-04-13 05:22:29 +00:00
|
|
|
totalCount: count,
|
2023-04-03 11:04:32 +00:00
|
|
|
processingTime: t2 - t1
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (process.argv[1] === fileURLToPath(import.meta.url)) {
|
|
|
|
(async () => {
|
|
|
|
try {
|
|
|
|
onSuccess(await getFTL());
|
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
process.exitCode = 1;
|
|
|
|
global.isError = true;
|
|
|
|
onError(err);
|
|
|
|
}
|
|
|
|
})();
|
|
|
|
}
|