Handle multiple Fluent source files

With list specified in js-build/config.js
This commit is contained in:
Dan Stillman 2023-05-29 06:02:09 -04:00 committed by Dan Stillman
parent 0de1305665
commit 086399da33
3 changed files with 78 additions and 56 deletions

View file

@ -129,8 +129,22 @@ const scssFiles = [
'chrome/skin/default/zotero/**/*.scss'
];
const ftlFileBaseNames = [
'zotero',
'preferences',
];
const buildsURL = 'https://zotero-download.s3.amazonaws.com/ci/';
module.exports = {
dirs, symlinkDirs, copyDirs, symlinkFiles, browserifyConfigs, jsFiles, scssFiles, ignoreMask, buildsURL
dirs,
symlinkDirs,
copyDirs,
symlinkFiles,
browserifyConfigs,
jsFiles,
scssFiles,
ignoreMask,
ftlFileBaseNames,
buildsURL,
};

View file

@ -2,25 +2,29 @@ import { ftlToJSON } from "ftl-tx";
import fs from 'fs-extra';
import { dirname, join } from 'path';
import { fileURLToPath } from 'url';
import { onError, onSuccess } from './utils.js';
import { ftlFileBaseNames as sourceFileBaseNames } from './config.js';
import { onError, onProgress, onSuccess } from './utils.js';
const ROOT = join(dirname(fileURLToPath(import.meta.url)), '..');
async function getJSON() {
const t1 = performance.now();
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, { spaces: '\t' });
for (let sourceFileBaseName of sourceFileBaseNames) {
const sourceFile = join(sourceDir, sourceFileBaseName + '.ftl');
const destFile = join(sourceDir, sourceFileBaseName + '.json');
const ftl = await fs.readFile(sourceFile, 'utf8');
const json = ftlToJSON(ftl, { transformTerms: false, storeTermsInJSON: false });
await fs.outputJSON(destFile, json, { spaces: '\t' });
onProgress(destFile, destFile, 'json');
}
const t2 = performance.now();
return ({
action: 'ftl->json',
count: 1,
totalCount: 1,
processingTime: t2 - t1
});
return ({
action: 'ftl->json',
count: sourceFileBaseNames.length,
totalCount: sourceFileBaseNames.length,
processingTime: t2 - t1
});
}
if (process.argv[1] === fileURLToPath(import.meta.url)) {

View file

@ -2,6 +2,7 @@ import { extractTerms, ftlToJSON, JSONToFtl } from 'ftl-tx';
import fs from 'fs-extra';
import { dirname, join } from 'path';
import { fileURLToPath } from 'url';
import { ftlFileBaseNames as sourceFileBaseNames } from './config.js';
import { onError, onProgress, onSuccess } from './utils.js';
import { exit } from 'process';
@ -9,7 +10,6 @@ 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');
const fallbackJSONPath = join(sourceDir, 'zotero.json');
function getLocaleDir(locale) {
return join(localesDir, locale, 'zotero');
@ -17,62 +17,66 @@ function getLocaleDir(locale) {
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`);
exit(1);
}
if (!(await fs.pathExists(termsSourceFTLPath))) {
console.error(`Required file ${termsSourceFTLPath} does not exist`);
exit(1);
}
const fallbackJSON = await fs.readJSON(fallbackJSONPath);
const terms = extractTerms(await fs.readFile(termsSourceFTLPath, 'utf-8'));
const foundLocales = (await fs.readdir(localesDir, { withFileTypes: true }))
.filter(dirent => dirent.isDirectory())
.map(dirent => dirent.name)
// Valid locale codes only
.filter(name => /^[a-z]{2}(-[A-Z]{2})?$/.test(name));
let locale;
let count = 0;
while ((locale = foundLocales.pop())) {
// Skip source locale
if (locale == 'en-US') {
continue;
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);
}
const ftlFilePath = join(getLocaleDir(locale), 'zotero.ftl');
let jsonFromLocalFTL = {};
try {
const ftl = await fs.readFile(ftlFilePath, 'utf8');
jsonFromLocalFTL = ftlToJSON(ftl, { transformTerms: false, storeTermsInJSON: false });
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++;
}
catch (e) {
// no local .ftl file
}
const jsonFilePath = join(getLocaleDir(locale), `zotero.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), 'zotero.ftl');
await fs.outputFile(outFtlPath, ftl);
onProgress(outFtlPath, outFtlPath, 'ftl');
count++;
}
const t2 = performance.now();
return ({
action: 'ftl',