-
-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
68 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |