2017-05-27 03:28:28 -06:00
|
|
|
const fs = require('fs-extra');
|
|
|
|
const parseString = require('xml2js').parseString;
|
|
|
|
|
2017-07-06 06:40:09 -06:00
|
|
|
const baseDir = 'translation/source';
|
2017-07-15 14:59:14 -06:00
|
|
|
const dbs = ['site', 'arena', 'emails', 'learn'];
|
2017-07-16 03:29:15 -06:00
|
|
|
const needsKeys = ['learn'];
|
2017-05-27 03:28:28 -06:00
|
|
|
|
2017-07-06 06:46:53 -06:00
|
|
|
function ucfirst(s) {
|
|
|
|
return s.charAt(0).toUpperCase() + s.slice(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
function keyListFrom(name) {
|
|
|
|
return fs.readFile(`${baseDir}/${name}.xml`, { encoding: 'utf8' }).then(txt => {
|
2017-07-06 06:40:09 -06:00
|
|
|
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);
|
2017-07-06 06:52:04 -06:00
|
|
|
resolve({
|
|
|
|
name: name,
|
2017-07-15 16:42:50 -06:00
|
|
|
code: keys.map(k => 'val `' + k + '` = new Translated("' + k + '", ' + ucfirst(name) + ')').join('\n'),
|
2017-07-16 03:29:15 -06:00
|
|
|
all: needsKeys.includes(name) && 'val allKeys = List(' + keys.map(k => '`' + k + '`').join(', ') + ')'
|
2017-07-06 06:52:04 -06:00
|
|
|
});
|
2017-07-06 06:40:09 -06:00
|
|
|
}));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-07-06 06:52:04 -06:00
|
|
|
Promise.all(dbs.map(keyListFrom)).then(objs => {
|
|
|
|
function dbCode(obj) {
|
|
|
|
return obj.name === 'site' ?
|
|
|
|
`${obj.code}\n` :
|
2017-07-16 03:29:15 -06:00
|
|
|
`object ${obj.name} {\n${obj.code}\n${obj.all || ''}\n}\n`;
|
2017-07-06 06:52:04 -06:00
|
|
|
}
|
2017-07-06 06:40:09 -06:00
|
|
|
const code = `// Generated with bin/trans-dump.js
|
2017-05-27 03:28:28 -06:00
|
|
|
package lila.i18n
|
|
|
|
|
2017-07-06 06:46:53 -06:00
|
|
|
import I18nDb.{ ${dbs.map(ucfirst).join(', ')} }
|
2017-07-06 06:12:34 -06:00
|
|
|
|
2017-05-27 15:31:31 -06:00
|
|
|
// format: OFF
|
2017-05-27 03:28:28 -06:00
|
|
|
object I18nKeys {
|
|
|
|
|
|
|
|
def untranslated(message: String) = new Untranslated(message)
|
|
|
|
|
2017-07-06 06:52:04 -06:00
|
|
|
${objs.map(dbCode).join('\n')}
|
2017-07-06 06:40:09 -06:00
|
|
|
}`;
|
2017-07-06 06:52:04 -06:00
|
|
|
|
2017-07-06 06:40:09 -06:00
|
|
|
fs.writeFile('modules/i18n/src/main/I18nKeys.scala', code);
|
2017-05-27 03:28:28 -06:00
|
|
|
});
|