-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathi18n.mjs
36 lines (26 loc) · 1.08 KB
/
i18n.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/usr/bin/env node
import { writeFileSync } from 'node:fs';
/**
* Generate the TypeScript interfaces from the english translation file.
*
* Note: only supports "a one child depth" in the data structure.
*/
const generateTypes = async () => {
// eslint-disable-next-line import/no-relative-parent-imports
const en = await import('../src/frontend/src/lib/i18n/en.json', { with: { type: 'json' } });
const data = Object.keys(en.default).map((key) => {
const properties = Object.keys(en.default[key]).map((prop) => `${prop}: string;`);
return {
key,
name: `I18n${key.charAt(0).toUpperCase()}${key.slice(1)}`,
properties
};
});
const lang = `lang: Languages;`;
const main = `\n\ninterface I18n {${lang}${data.map((i) => `${i.key}: ${i.name};`).join('')}}`;
const interfaces = data.map((i) => `\n\ninterface ${i.name} {${i.properties.join('')}}`).join('');
const comment = `/**\n* Auto-generated definitions file ("npm run i18n")\n*/`;
const output = `${comment}${interfaces}${main}`;
writeFileSync('./src/frontend/src/lib/types/i18n.d.ts', output);
};
await generateTypes();