Skip to content

Commit

Permalink
feat(i18n): add i18n-lokalise-json
Browse files Browse the repository at this point in the history
  • Loading branch information
zyf722 committed Oct 23, 2024
1 parent 5bd4d4d commit 35471c7
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 1 deletion.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@
"i18n-export": "node ./scripts/i18n-export.js",
"i18n-update-push": "node ./scripts/i18n-upload.mjs",
"i18n-update-pull": "node ./scripts/i18n-import.mjs",
"i18n-exclude": "node ./scripts/i18n-exclude.js"
"i18n-exclude": "node ./scripts/i18n-exclude.js",
"i18n-lokalise-json": "node ./scripts/i18n-lokalise-json.mjs"
},
"dependencies": {
"@codemirror/autocomplete": "6.3.0",
Expand Down
66 changes: 66 additions & 0 deletions scripts/i18n-lokalise-json.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Generate Lokalise JSON files from TypeScript src files in src/livecodes/i18n/locales/<lang> directory.

import fs from 'fs';
import path from 'path';
import babel from '@babel/core';
import parser from '@babel/parser';
import { autoGeneratedWarning, sortedJSONify } from './i18n-export.js';

const flatten = (obj, prefix = '') =>
Object.keys(obj).reduce((acc, key) => {
const value = obj[key];
if (typeof value === 'object') {
return { ...acc, ...flatten(value, `${prefix}${key}.`) };
}
return { ...acc, [`${prefix}${key}`]: value };
}, {});

const generateLokaliseJSON = async () => {
const lang = process.argv[2];
const srcDir = path.resolve('src/livecodes/i18n/locales/' + lang);
if (!fs.existsSync(srcDir)) {
console.error(`Language ${srcDir} does not exist.`);
return;
}

const files = fs
.readdirSync(srcDir)
.filter((file) => file.endsWith('.ts'))
.map((file) => path.resolve(srcDir, file));

await Promise.all(
files.map(async (file) => {
try {
console.log(`Generating Lokalise JSON for ${file} in language ${lang}...`);

const data = await fs.promises.readFile(file, 'utf8');
const ast = parser.parse(data, {
sourceType: 'module',
plugins: ['typescript'],
});

// Find first ObjectExpression and load it
let translation;
babel.traverse(ast, {
ObjectExpression(path) {
const code = data.substring(path.node.start, path.node.end);
translation = eval(`(${code})`);
path.stop();
},
});

const result = { $comment: autoGeneratedWarning.substring(3) };
for (const [key, value] of Object.entries(flatten(translation))) {
result[key] = { translation: value };
}

const outFile = path.resolve(srcDir, file.replace('.ts', '.lokalise.json'));
await fs.promises.writeFile(outFile, sortedJSONify(result).replace(/<(\/?)(\d+)>/g, '<$1tag-$2>'));
} catch (err) {
console.error(err);
}
}),
);
};

generateLokaliseJSON();

0 comments on commit 35471c7

Please sign in to comment.