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 : * Generating translations keys:
* - 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 * This code walk through all the files in client directory and its children
* - soit à la main * Get the all keys called by our makeT utils function
* - soit en utilisant un script (en itérant sur le json) * And add only the new one on our en.client.json file
* - 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
*/ */
const fs = require("fs");
const fs = require('fs'); const path = require("path");
const path = require('path'); const Parser = require("i18next-scanner").Parser;
const Parser = require('i18next-scanner').Parser; const englishKeys = require("../static/locales/en.client.json");
const englishKeys = require('../static/locales/en.client.json'); const _ = require("lodash");
const _ = require('lodash');
const parser = new Parser({ const parser = new Parser({
keySeparator: '/', keySeparator: "/",
nsSeparator: null, nsSeparator: null,
}); });
async function* walk(dir) { async function* walk(dir) {
for await (const d of await fs.promises.opendir(dir)) { for await (const d of await fs.promises.opendir(dir)) {
const entry = path.join(dir, d.name); const entry = path.join(dir, d.name);
// d.isDirectory() && console.log(d.name);
if (d.isDirectory()) yield* walk(entry); if (d.isDirectory()) yield* walk(entry);
else if (d.isFile()) yield entry; else if (d.isFile()) yield entry;
} }
@ -35,8 +28,7 @@ async function* walk(dir) {
const customHandler = (fileName) => (key, options) => { const customHandler = (fileName) => (key, options) => {
const keyWithFile = `${fileName}/${key}`; 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 keyOne = `${keyWithFile}_one`;
const keyOther = `${keyWithFile}_other`; const keyOther = `${keyWithFile}_other`;
parser.set(keyOne, key); parser.set(keyOne, key);
@ -47,29 +39,34 @@ const customHandler = (fileName) => (key, options) => {
}; };
const getKeysFromFile = (filePath, fileName) => { const getKeysFromFile = (filePath, fileName) => {
const content = fs.readFileSync(filePath, 'utf-8'); const content = fs.readFileSync(filePath, "utf-8");
parser.parseFuncFromString(content, { list: [ parser.parseFuncFromString(
'i18next.t', content,
't' // To match the file-level t function created with makeT {
]}, customHandler(fileName)) list: [
"i18next.t",
"t", // To match the file-level t function created with makeT
],
},
customHandler(fileName)
);
const keys = parser.get({ sort: true }); const keys = parser.get({ sort: true });
return keys return keys;
} };
async function walkTranslation(dirPath) { async function walkTranslation(dirPath) {
for await (const p of walk(dirPath)) { for await (const p of walk(dirPath)) {
const { name } = path.parse(p); const { name } = path.parse(p);
getKeysFromFile(p, name); getKeysFromFile(p, name);
} }
const keys = parser.get({sort: true}); const keys = parser.get({ sort: true });
const newTranslations = _.merge(keys.en.translation, englishKeys); 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; return keys;
} }
const keys = walkTranslation("app/client") walkTranslation("app/client");
// console.log({englishKeys});
// const keys = getKeysFromFile('app/client/ui/errorPages.ts', 'errorPages');
// console.log(JSON.stringify(keys, null, 2));

View File

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

View File

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