forked from johannesjo/super-productivity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract-i18n.js
40 lines (35 loc) · 919 Bytes
/
extract-i18n.js
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
37
38
39
40
const fs = require('fs');
const TRANSLATION_SRC = __dirname + '/../src/assets/i18n/en.json';
module.exports = () => {
const _tr = fs.readFileSync(TRANSLATION_SRC);
const tr = JSON.parse(_tr);
const parse = (o, pref = '') => {
return Object.keys(o).reduce((acc, key) => {
console.log(key);
if (typeof o[key] === 'object') {
return {
...acc,
[key]: parse(o[key], pref + key + '.'),
};
} else if (typeof o[key] === 'string') {
return {
...acc,
[key]: pref + key,
};
} else {
throw 'Invalid Data';
}
}, {});
};
const parsed = parse(tr);
const string = `const T = ${JSON.stringify(parsed, null, 2).replace(
/"(\w+)":/g,
'$1:',
)};
export { T };
`.replace(/"/g, "'");
fs.writeFileSync(__dirname + '/../src/app/t.const.ts', string, {
overwrite: true,
flag: 'w',
});
};