From 0de130566552f56a637c8065ec52ea93a3e5cbb3 Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Thu, 13 Apr 2023 01:22:29 -0400 Subject: [PATCH] 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 --- js-build/ftl-to-json.mjs | 11 +++++----- js-build/localize-ftl.mjs | 43 +++++++++++++++++++-------------------- 2 files changed, 26 insertions(+), 28 deletions(-) diff --git a/js-build/ftl-to-json.mjs b/js-build/ftl-to-json.mjs index db812d8603..e2e06cfee4 100644 --- a/js-build/ftl-to-json.mjs +++ b/js-build/ftl-to-json.mjs @@ -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', diff --git a/js-build/localize-ftl.mjs b/js-build/localize-ftl.mjs index 3c06bfd430..5185cb74cb 100644 --- a/js-build/localize-ftl.mjs +++ b/js-build/localize-ftl.mjs @@ -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 }); }