Tweaks to Fluent/Transifex JSON processing scripts (#3058)

- Use locale directories for JSON files, since that's where the
  Transifex client will interact with them
- Skip non-locale directories (e.g., don't create an .ftl file for
  .DS_Store)
- Other minor simplification
This commit is contained in:
Dan Stillman 2023-04-13 01:22:29 -04:00 committed by Dan Stillman
parent afaf0b4968
commit 0de1305665
2 changed files with 26 additions and 28 deletions

View file

@ -5,16 +5,15 @@ import { fileURLToPath } from 'url';
import { onError, onSuccess } from './utils.js';
const ROOT = join(dirname(fileURLToPath(import.meta.url)), '..');
const JSONDir = join(ROOT, 'tmp', 'tx');
async function getJSON() {
const t1 = performance.now();
await fs.mkdirp(JSONDir);
const sourcefile = join(ROOT, 'chrome', 'locale', 'en-US', 'zotero', 'zotero.ftl');
const destFile = join(JSONDir, 'zotero_en_US.json');
const ftl = await fs.readFile(sourcefile, 'utf8');
const sourceDir = join(ROOT, 'chrome', 'locale', 'en-US', 'zotero');
const sourceFile = join(sourceDir, 'zotero.ftl');
const destFile = join(sourceDir, 'zotero.json');
const ftl = await fs.readFile(sourceFile, 'utf8');
const json = ftlToJSON(ftl, { transformTerms: false, storeTermsInJSON: false });
await fs.outputJSON(destFile, json);
await fs.outputJSON(destFile, json, { spaces: '\t' });
const t2 = performance.now();
return ({
action: 'ftl->json',

View file

@ -6,20 +6,19 @@ import { onError, onProgress, onSuccess } from './utils.js';
import { exit } from 'process';
const ROOT = join(dirname(fileURLToPath(import.meta.url)), '..');
const JSONDir = join(ROOT, 'tmp', 'tx');
const localesDir = join(ROOT, 'chrome', 'locale');
const sourceDir = join(localesDir, 'en-US', 'zotero');
const termsSourceFTLPath = join(ROOT, 'app', 'assets', 'branding', 'locale', 'brand.ftl');
const fallbackJSONPath = join(sourceDir, 'zotero.json');
// don't override source zotero.ftl
const localeToSkip = ['en-US'];
function getLocaleDir(locale) {
return join(localesDir, locale, 'zotero');
}
async function getFTL() {
const t1 = performance.now();
if (!(await fs.pathExists(fallbackJSONPath))) {
console.error(`File ${fallbackJSONPath} does not exist, please run 'ftl-to-json' first`);
console.error(`File ${fallbackJSONPath} does not exist -- please run 'ftl-to-json' first`);
exit(1);
}
@ -31,44 +30,44 @@ async function getFTL() {
const fallbackJSON = await fs.readJSON(fallbackJSONPath);
const terms = extractTerms(await fs.readFile(termsSourceFTLPath, 'utf-8'));
const expectedLocale = (await fs.readdir(localeDir, { withFileTypes: true }))
const foundLocales = (await fs.readdir(localesDir, { withFileTypes: true }))
.filter(dirent => dirent.isDirectory())
.map(dirent => dirent.name);
.map(dirent => dirent.name)
// Valid locale codes only
.filter(name => /^[a-z]{2}(-[A-Z]{2})?$/.test(name));
const totalCount = expectedLocale.length;
let locale;
let count = 0;
while ((locale = expectedLocale.pop())) {
if (localeToSkip.includes(locale)) {
count++;
while ((locale = foundLocales.pop())) {
// Skip source locale
if (locale == 'en-US') {
continue;
}
const ftlFilePath = join(ROOT, 'chrome', 'locale', locale, 'zotero', 'zotero.ftl');
let JSONFromLocalFTL = {};
const ftlFilePath = join(getLocaleDir(locale), 'zotero.ftl');
let jsonFromLocalFTL = {};
try {
const ftl = await fs.readFile(ftlFilePath, 'utf8');
JSONFromLocalFTL = ftlToJSON(ftl, { transformTerms: false, storeTermsInJSON: false });
jsonFromLocalFTL = ftlToJSON(ftl, { transformTerms: false, storeTermsInJSON: false });
}
catch (e) {
// no local .ftl file
}
const JSONFilePath = join(JSONDir, `zotero_${locale.replace('-', '_')}.json`);
let JSONFromTransifex = {};
const jsonFilePath = join(getLocaleDir(locale), `zotero.json`);
let jsonFromTransifex = {};
try {
const json = await fs.readJSON(JSONFilePath);
JSONFromTransifex = json;
const json = await fs.readJSON(jsonFilePath);
jsonFromTransifex = json;
}
catch (e) {
// no .json file from transifex
}
const mergedJSON = { ...fallbackJSON, ...JSONFromLocalFTL, ...JSONFromTransifex };
const mergedJSON = { ...fallbackJSON, ...jsonFromLocalFTL, ...jsonFromTransifex };
const ftl = JSONToFtl(mergedJSON, { addTermsToFTL: false, storeTermsInJSON: false, transformTerms: false, terms });
const outFtlPath = join(ROOT, 'chrome', 'locale', locale, 'zotero', 'zotero.ftl');
const outFtlPath = join(getLocaleDir(locale), 'zotero.ftl');
await fs.outputFile(outFtlPath, ftl);
onProgress(outFtlPath, outFtlPath, 'ftl');
count++;
@ -78,7 +77,7 @@ async function getFTL() {
return ({
action: 'ftl',
count,
totalCount,
totalCount: count,
processingTime: t2 - t1
});
}