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");

View File

@ -78,7 +78,7 @@
"chance": "1.0.16",
"esbuild-loader": "2.19.0",
"http-proxy": "1.18.1",
"i18next-scanner": "^4.1.0",
"i18next-scanner": "4.1.0",
"jsdom": "16.5.0",
"mocha": "5.2.0",
"mocha-webdriver": "0.2.9",

View File

@ -107,11 +107,11 @@
"Clear cell": "Clear cell",
"Clear values": "Clear values",
"Copy anchor link": "Copy anchor link",
"Delete {{count}} columns_one": "Delete {{count}} columns",
"Delete {{count}} columns_one": "Delete column",
"Delete {{count}} columns_other": "Delete {{count}} columns",
"Delete {{count}} rows_one": "Delete {{count}} rows",
"Delete {{count}} rows_one": "Delete row",
"Delete {{count}} rows_other": "Delete {{count}} rows",
"Duplicate rows_one": "Duplicate rows",
"Duplicate rows_one": "Duplicate row",
"Duplicate rows_other": "Duplicate rows",
"Filter by this value": "Filter by this value",
"Insert column to the left": "Insert column to the left",
@ -119,9 +119,9 @@
"Insert row": "Insert row",
"Insert row above": "Insert row above",
"Insert row below": "Insert row below",
"Reset {{count}} columns_one": "Reset {{count}} columns",
"Reset {{count}} columns_one": "Reset column",
"Reset {{count}} columns_other": "Reset {{count}} columns",
"Reset {{count}} entire columns_one": "Reset {{count}} entire columns",
"Reset {{count}} entire columns_one": "Reset entire column",
"Reset {{count}} entire columns_other": "Reset {{count}} entire columns"
},
"ChartView": {
@ -343,9 +343,9 @@
"Delete {{count}} columns_one": "Delete column",
"Delete {{count}} columns_other": "Delete {{count}} columns",
"Filter Data": "Filter Data",
"Freeze {{count}} columns_one": "Freeze {{count}} columns",
"Freeze {{count}} columns_one": "Freeze this column",
"Freeze {{count}} columns_other": "Freeze {{count}} columns",
"Freeze {{count}} more columns_one": "Freeze {{count}} more columns",
"Freeze {{count}} more columns_one": "Freeze one more column",
"Freeze {{count}} more columns_other": "Freeze {{count}} more columns",
"Hide {{count}} columns_one": "Hide column",
"Hide {{count}} columns_other": "Hide {{count}} columns",
@ -361,7 +361,7 @@
"Sorted (#{{count}})_one": "Sorted (#{{count}})",
"Sorted (#{{count}})_other": "Sorted (#{{count}})",
"Unfreeze all columns": "Unfreeze all columns",
"Unfreeze {{count}} columns_one": "Unfreeze {{count}} columns",
"Unfreeze {{count}} columns_one": "Unfreeze this column",
"Unfreeze {{count}} columns_other": "Unfreeze {{count}} columns"
},
"GristDoc": {
@ -467,7 +467,7 @@
"Pages": {
"Delete": "Delete",
"Delete data and this page.": "Delete data and this page.",
"The following tables will no longer be visible_one": "The following tables will no longer be visible",
"The following tables will no longer be visible_one": "The following table will no longer be visible",
"The following tables will no longer be visible_other": "The following tables will no longer be visible"
},
"PermissionsWidget": {
@ -497,14 +497,14 @@
"COLUMN TYPE": "COLUMN TYPE",
"CUSTOM": "CUSTOM",
"Change Widget": "Change Widget",
"Columns_one": "Columns",
"Columns_one": "Column",
"Columns_other": "Columns",
"DATA TABLE": "DATA TABLE",
"DATA TABLE NAME": "DATA TABLE NAME",
"Data": "Data",
"Detach": "Detach",
"Edit Data Selection": "Edit Data Selection",
"Fields_one": "Fields",
"Fields_one": "Field",
"Fields_other": "Fields",
"GROUPED BY": "GROUPED BY",
"ROW STYLE": "ROW STYLE",
@ -514,7 +514,7 @@
"SOURCE DATA": "SOURCE DATA",
"Save": "Save",
"Select Widget": "Select Widget",
"Series_one": "Series",
"Series_one": "Serie",
"Series_other": "Series",
"Sort & Filter": "Sort & Filter",
"TRANSFORM": "TRANSFORM",
@ -526,7 +526,7 @@
"RowContextMenu": {
"Copy anchor link": "Copy anchor link",
"Delete": "Delete",
"Duplicate rows_one": "Duplicate rows",
"Duplicate rows_one": "Duplicate row",
"Duplicate rows_other": "Duplicate rows",
"Insert row": "Insert row",
"Insert row above": "Insert row above",
@ -745,4 +745,4 @@
"false": "false",
"true": "true"
}
}
}