forked from lichess-org/lila
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrans-dump
executable file
·71 lines (61 loc) · 2.56 KB
/
trans-dump
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
#!/usr/bin/env node
const { readFile, writeFile } = require('fs/promises');
const { parseString } = require('xml2js');
const path = require('path');
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) {
return name === 'clas' ? 'class' : name;
}
function keyListFrom(name) {
return readFile(path.resolve(baseDir, `${xmlName(name)}.xml`), { encoding: 'utf8' }).then(txt => {
return new Promise((resolve, reject) =>
parseString(txt, (_, xml) => {
const strings = (xml.resources.string || []).map(e => e['$'].name);
const plurals = (xml.resources.plurals || []).map(e => e['$'].name);
const keys = strings.concat(plurals);
const spaces = ' ';
resolve({
name,
code:
keys
.map(
k => `${spaces}val \`${k}\`: I18nKey = "${name === 'site' ? '' : xmlName(name) + ':'}${k}"`,
)
.join('\n') + '\n',
});
}),
);
});
}
Promise.all(dbs.map(keyListFrom)).then(objs => {
function dbCode(obj) {
return ` object ${obj.name}:\n${obj.code}`;
}
const code = `// Generated with bin/trans-dump.js
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')}
`;
return writeFile(path.resolve(lilaDir, 'modules/coreI18n/src/main/key.scala'), code);
});