Clean generating script and english translation values

This commit is contained in:
Louis Delbosc
2022-12-16 18:10:19 +01:00
parent 4165c35dd3
commit c18c6cb264
3 changed files with 48 additions and 51 deletions

View File

@@ -1,33 +1,26 @@
/**
* Stratégie de traduction :
* - valider sur la convention proposée par Yohan sur https://github.com/gristlabs/grist-core/issues/336
* - migrer les anciennes clefs vers la nouvelle convention
* - soit à la main
* - soit en utilisant un script (en itérant sur le json)
* - pour les nouvelles clefs, utiliser la nouvelle convention
* - de cette manière l'anglais devrait fonctionner "tout seul", modulo éventuellement une amélioration de makeT notamment dans le cas des interpolations
* - pour les autres langues, il faudra extraire toutes les clefs dans les en.*.json
* - ci-dessous un tout début de script pour le faire sur un fichier unique
* - il faudra ensuite itérer sur tous les fichiers, et merger l'extraction avec les en.*.json existants
* Generating translations keys:
*
* This code walk through all the files in client directory and its children
* Get the all keys called by our makeT utils function
* And add only the new one on our en.client.json file
*
*/
const fs = require('fs');
const path = require('path');
const Parser = require('i18next-scanner').Parser;
const englishKeys = require('../static/locales/en.client.json');
const _ = require('lodash');
const fs = require("fs");
const path = require("path");
const Parser = require("i18next-scanner").Parser;
const englishKeys = require("../static/locales/en.client.json");
const _ = require("lodash");
const parser = new Parser({
keySeparator: '/',
keySeparator: "/",
nsSeparator: null,
});
async function* walk(dir) {
for await (const d of await fs.promises.opendir(dir)) {
const entry = path.join(dir, d.name);
// d.isDirectory() && console.log(d.name);
if (d.isDirectory()) yield* walk(entry);
else if (d.isFile()) yield entry;
}
@@ -35,8 +28,7 @@ async function* walk(dir) {
const customHandler = (fileName) => (key, options) => {
const keyWithFile = `${fileName}/${key}`;
// console.log({key, options});
if (Object.keys(options).includes('count') === true) {
if (Object.keys(options).includes("count") === true) {
const keyOne = `${keyWithFile}_one`;
const keyOther = `${keyWithFile}_other`;
parser.set(keyOne, key);
@@ -47,29 +39,34 @@ const customHandler = (fileName) => (key, options) => {
};
const getKeysFromFile = (filePath, fileName) => {
const content = fs.readFileSync(filePath, 'utf-8');
parser.parseFuncFromString(content, { list: [
'i18next.t',
't' // To match the file-level t function created with makeT
]}, customHandler(fileName))
const content = fs.readFileSync(filePath, "utf-8");
parser.parseFuncFromString(
content,
{
list: [
"i18next.t",
"t", // To match the file-level t function created with makeT
],
},
customHandler(fileName)
);
const keys = parser.get({ sort: true });
return keys
}
return keys;
};
async function walkTranslation(dirPath) {
for await (const p of walk(dirPath)) {
const { name } = path.parse(p);
getKeysFromFile(p, name);
}
const keys = parser.get({sort: true});
const keys = parser.get({ sort: true });
const newTranslations = _.merge(keys.en.translation, englishKeys);
await fs.promises.writeFile('static/locales/en.client.json', JSON.stringify(newTranslations, null, 2), 'utf-8');
await fs.promises.writeFile(
"static/locales/en.client.json",
JSON.stringify(newTranslations, null, 2),
"utf-8"
);
return keys;
}
const keys = walkTranslation("app/client")
// console.log({englishKeys});
// const keys = getKeysFromFile('app/client/ui/errorPages.ts', 'errorPages');
// console.log(JSON.stringify(keys, null, 2));
walkTranslation("app/client");