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

View file

@ -6,20 +6,19 @@ import { onError, onProgress, onSuccess } from './utils.js';
import { exit } from 'process'; import { exit } from 'process';
const ROOT = join(dirname(fileURLToPath(import.meta.url)), '..'); const ROOT = join(dirname(fileURLToPath(import.meta.url)), '..');
const JSONDir = join(ROOT, 'tmp', 'tx');
const localesDir = join(ROOT, 'chrome', 'locale'); const localesDir = join(ROOT, 'chrome', 'locale');
const sourceDir = join(localesDir, 'en-US', 'zotero'); const sourceDir = join(localesDir, 'en-US', 'zotero');
const termsSourceFTLPath = join(ROOT, 'app', 'assets', 'branding', 'locale', 'brand.ftl'); const termsSourceFTLPath = join(ROOT, 'app', 'assets', 'branding', 'locale', 'brand.ftl');
const fallbackJSONPath = join(sourceDir, 'zotero.json'); const fallbackJSONPath = join(sourceDir, 'zotero.json');
// don't override source zotero.ftl function getLocaleDir(locale) {
const localeToSkip = ['en-US']; return join(localesDir, locale, 'zotero');
}
async function getFTL() { async function getFTL() {
const t1 = performance.now(); const t1 = performance.now();
if (!(await fs.pathExists(fallbackJSONPath))) { 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); exit(1);
} }
@ -31,44 +30,44 @@ async function getFTL() {
const fallbackJSON = await fs.readJSON(fallbackJSONPath); const fallbackJSON = await fs.readJSON(fallbackJSONPath);
const terms = extractTerms(await fs.readFile(termsSourceFTLPath, 'utf-8')); 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()) .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 locale;
let count = 0; let count = 0;
while ((locale = foundLocales.pop())) {
while ((locale = expectedLocale.pop())) { // Skip source locale
if (localeToSkip.includes(locale)) { if (locale == 'en-US') {
count++;
continue; continue;
} }
const ftlFilePath = join(ROOT, 'chrome', 'locale', locale, 'zotero', 'zotero.ftl'); const ftlFilePath = join(getLocaleDir(locale), 'zotero.ftl');
let JSONFromLocalFTL = {}; let jsonFromLocalFTL = {};
try { try {
const ftl = await fs.readFile(ftlFilePath, 'utf8'); const ftl = await fs.readFile(ftlFilePath, 'utf8');
JSONFromLocalFTL = ftlToJSON(ftl, { transformTerms: false, storeTermsInJSON: false }); jsonFromLocalFTL = ftlToJSON(ftl, { transformTerms: false, storeTermsInJSON: false });
} }
catch (e) { catch (e) {
// no local .ftl file // no local .ftl file
} }
const JSONFilePath = join(JSONDir, `zotero_${locale.replace('-', '_')}.json`); const jsonFilePath = join(getLocaleDir(locale), `zotero.json`);
let JSONFromTransifex = {}; let jsonFromTransifex = {};
try { try {
const json = await fs.readJSON(JSONFilePath); const json = await fs.readJSON(jsonFilePath);
JSONFromTransifex = json; jsonFromTransifex = json;
} }
catch (e) { catch (e) {
// no .json file from transifex // 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 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); await fs.outputFile(outFtlPath, ftl);
onProgress(outFtlPath, outFtlPath, 'ftl'); onProgress(outFtlPath, outFtlPath, 'ftl');
count++; count++;
@ -78,7 +77,7 @@ async function getFTL() {
return ({ return ({
action: 'ftl', action: 'ftl',
count, count,
totalCount, totalCount: count,
processingTime: t2 - t1 processingTime: t2 - t1
}); });
} }