-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Copy pathtrans-dump.ts
101 lines (86 loc) · 3.04 KB
/
trans-dump.ts
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import { readFileSync, writeFileSync } from 'fs';
import path from 'path';
import { XMLParser } from 'fast-xml-parser';
interface TranslationXml {
'?xml': {
version: string;
encoding: string
};
resources: {
string?: {
name: string;
text: string;
comment?: string;
}[];
plurals?: {
name: string;
text: string ;
comment?: string;
item: {
quantity: 'one' | 'other';
text: string;
}[];
}[];
};
}
interface KeyList {
name: string;
code: string;
}
const lilaDir = path.resolve(__dirname, '..');
const baseDir = path.resolve(lilaDir, 'translation/source');
const dbs = 'site arena emails learn activity coordinates study clas contact appeal patron coach broadcast streamer tfa settings preferences team perfStat search tourname faq lag swiss puzzle puzzleTheme challenge storm ublog insight keyboardMove timeago oauthScope dgt voiceCommands onboarding features'
.split(' ');
function xmlName(name: string) {
const renames = new Map([['clas', 'class']]);
return renames.get(name) || name;
}
function keyListFrom(name: string): KeyList {
const txt = readFileSync(path.resolve(baseDir, `${xmlName(name)}.xml`), 'utf-8');
const parser = new XMLParser({
ignoreAttributes: false,
isArray: tagName => ['string', 'plurals', 'item'].includes(tagName),
attributeNamePrefix: '',
textNodeName: 'text',
});
const xml: TranslationXml = parser.parse(txt);
const keys = [
...(xml.resources.string || []).map(e => e.name),
...(xml.resources.plurals || []).map(e => e.name),
];
const indent = ' '.repeat(4);
return {
name,
code:
keys
.map(k => `${indent}val \`${k}\`: I18nKey = "${name === 'site' ? '' : xmlName(name) + ':'}${k}"`)
.join('\n') + '\n',
};
}
const dbCode = (obj: KeyList) => ` object ${obj.name}:\n${obj.code}`
Promise.all(dbs.map(keyListFrom)).then(objs => {
const code = `// Generated with bin/trans-dump.ts
package lila.core.i18n
opaque type I18nKey = String
object I18nKey:
def apply(key: String): I18nKey = key
import scalatags.Text.RawFrag
extension (key: I18nKey)
def value: String = key
def txt(args: Any*)(using trans: Translate): String =
trans.translator.txt.literal(key, args, trans.lang)
def pluralTxt(count: Count, args: Any*)(using trans: Translate): String =
trans.translator.txt.plural(key, count, args, trans.lang)
def pluralSameTxt(count: Long)(using trans: Translate): String = pluralTxt(count, count)
def apply(args: Matchable*)(using trans: Translate): RawFrag =
trans.translator.frag.literal(key, args, trans.lang)
def plural(count: Count, args: Matchable*)(using trans: Translate): RawFrag =
trans.translator.frag.plural(key, count, args, trans.lang)
def pluralSame(count: Int)(using trans: Translate): RawFrag = plural(count, count)
// format: OFF
${objs.map(dbCode).join('\n')}
`;
const keyFile = path.resolve(lilaDir, 'modules/coreI18n/src/main/key.scala');
writeFileSync(keyFile, code);
console.log(`✅ Wrote ${keyFile}`);
});