2017-01-06 18:43:13 +08:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
|
|
|
// copies the resources into the webapp directory.
|
|
|
|
//
|
|
|
|
|
2017-05-26 23:48:21 +08:00
|
|
|
// Languages are listed manually so we can choose when to include
|
|
|
|
// a translation in the app (because having a translation with only
|
|
|
|
// 3 strings translated is just frustrating)
|
|
|
|
// This could readily be automated, but it's nice to explicitly
|
2018-03-17 18:27:10 +08:00
|
|
|
// control when new languages are available.
|
2017-05-26 23:48:21 +08:00
|
|
|
const INCLUDE_LANGS = [
|
2018-02-24 16:28:39 +08:00
|
|
|
{'value': 'bg', 'label': 'Български'},
|
2018-01-26 01:25:46 +08:00
|
|
|
{'value': 'ca', 'label': 'Català'},
|
2018-03-17 18:27:10 +08:00
|
|
|
{'value': 'cs', 'label': 'čeština'},
|
2017-08-16 18:14:24 +08:00
|
|
|
{'value': 'da', 'label': 'Dansk'},
|
|
|
|
{'value': 'de_DE', 'label': 'Deutsch'},
|
2018-03-17 23:24:47 +08:00
|
|
|
{'value': 'el', 'label': 'Ελληνικά'},
|
2017-06-01 21:38:36 +08:00
|
|
|
{'value': 'en_EN', 'label': 'English'},
|
2017-06-05 23:05:07 +08:00
|
|
|
{'value': 'en_US', 'label': 'English (US)'},
|
2017-06-19 06:50:13 +08:00
|
|
|
{'value': 'eo', 'label': 'Esperanto'},
|
2017-08-16 18:14:24 +08:00
|
|
|
{'value': 'es', 'label': 'Español'},
|
2018-01-08 03:39:24 +08:00
|
|
|
{'value': 'eu', 'label': 'Euskara'},
|
2017-09-15 19:26:45 +08:00
|
|
|
{'value': 'fi', 'label': 'Suomi'},
|
2017-06-01 21:38:36 +08:00
|
|
|
{'value': 'fr', 'label': 'Français'},
|
2018-03-17 18:27:10 +08:00
|
|
|
{'value': 'gl', 'label': 'Galego'},
|
2017-06-19 06:50:13 +08:00
|
|
|
{'value': 'hu', 'label': 'Magyar'},
|
2018-08-16 22:04:17 +08:00
|
|
|
{'value': 'it', 'label': 'Italiano'},
|
2017-06-16 20:05:56 +08:00
|
|
|
{'value': 'ko', 'label': '한국어'},
|
2017-08-16 18:15:48 +08:00
|
|
|
{'value': 'lv', 'label': 'Latviešu'},
|
2017-06-19 06:50:13 +08:00
|
|
|
{'value': 'nb_NO', 'label': 'Norwegian Bokmål'},
|
2017-08-16 18:14:24 +08:00
|
|
|
{'value': 'nl', 'label': 'Nederlands'},
|
2018-08-16 22:04:17 +08:00
|
|
|
{'value': 'nn', 'label': 'Norsk Nynorsk'},
|
2017-06-19 06:50:13 +08:00
|
|
|
{'value': 'pl', 'label': 'Polski'},
|
2017-06-01 21:38:36 +08:00
|
|
|
{'value': 'pt', 'label': 'Português'},
|
|
|
|
{'value': 'pt_BR', 'label': 'Português do Brasil'},
|
|
|
|
{'value': 'ru', 'label': 'Русский'},
|
2017-11-15 20:14:30 +08:00
|
|
|
{'value': 'sk', 'label': 'Slovenčina'},
|
2018-03-17 23:24:47 +08:00
|
|
|
{'value': 'sr', 'label': 'српски'},
|
2018-03-17 18:27:10 +08:00
|
|
|
{'value': 'sv', 'label': 'Svenska'},
|
2017-08-16 18:15:48 +08:00
|
|
|
{'value': 'te', 'label': 'తెలుగు'},
|
2018-03-17 23:24:47 +08:00
|
|
|
{'value': 'th', 'label': 'ไทย'},
|
2017-06-19 06:50:13 +08:00
|
|
|
{'value': 'tr', 'label': 'Türk'},
|
2017-06-06 23:51:00 +08:00
|
|
|
{'value': 'zh_Hans', 'label': '简体中文'}, // simplified chinese
|
|
|
|
{'value': 'zh_Hant', 'label': '繁體中文'}, // traditional chinese
|
2017-05-26 23:48:21 +08:00
|
|
|
];
|
|
|
|
|
2017-01-06 18:43:13 +08:00
|
|
|
// cpx includes globbed parts of the filename in the destination, but excludes
|
|
|
|
// common parents. Hence, "res/{a,b}/**": the output will be "dest/a/..." and
|
|
|
|
// "dest/b/...".
|
|
|
|
const COPY_LIST = [
|
2017-05-11 20:12:26 +08:00
|
|
|
["res/manifest.json", "webapp"],
|
2017-05-25 00:58:03 +08:00
|
|
|
["res/home.html", "webapp"],
|
2017-10-26 08:38:07 +08:00
|
|
|
["res/home-status.html", "webapp"],
|
2017-05-30 10:58:45 +08:00
|
|
|
["res/home/**", "webapp/home"],
|
2018-04-23 18:28:01 +08:00
|
|
|
["res/vector-icons/**", "webapp/vector-icons"],
|
2018-04-24 00:27:35 +08:00
|
|
|
["node_modules/matrix-react-sdk/res/{fonts,img,themes,media}/**", "webapp"],
|
2018-04-13 08:49:52 +08:00
|
|
|
["res/themes/**", "webapp/themes"],
|
2017-01-06 18:43:13 +08:00
|
|
|
["node_modules/emojione/assets/svg/*", "webapp/emojione/svg/"],
|
2017-04-22 00:36:48 +08:00
|
|
|
["node_modules/emojione/assets/png/*", "webapp/emojione/png/"],
|
2017-05-23 21:12:53 +08:00
|
|
|
["./config.json", "webapp", { directwatch: 1 }],
|
2017-01-06 18:43:13 +08:00
|
|
|
];
|
|
|
|
|
2017-05-26 23:48:21 +08:00
|
|
|
INCLUDE_LANGS.forEach(function(l) {
|
|
|
|
COPY_LIST.push([
|
2017-06-01 21:38:36 +08:00
|
|
|
l.value, "webapp/i18n/", { lang: 1 },
|
2017-05-26 23:48:21 +08:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
2017-01-06 18:43:13 +08:00
|
|
|
const parseArgs = require('minimist');
|
|
|
|
const Cpx = require('cpx');
|
|
|
|
const chokidar = require('chokidar');
|
2017-05-23 21:12:53 +08:00
|
|
|
const fs = require('fs');
|
|
|
|
const rimraf = require('rimraf');
|
|
|
|
|
2017-01-06 18:43:13 +08:00
|
|
|
const argv = parseArgs(
|
|
|
|
process.argv.slice(2), {}
|
|
|
|
);
|
|
|
|
|
|
|
|
var watch = argv.w;
|
|
|
|
var verbose = argv.v;
|
|
|
|
|
|
|
|
function errCheck(err) {
|
|
|
|
if (err) {
|
|
|
|
console.error(err.message);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-26 23:48:21 +08:00
|
|
|
// Check if webapp exists
|
|
|
|
if (!fs.existsSync('webapp')) {
|
|
|
|
fs.mkdirSync('webapp');
|
|
|
|
}
|
|
|
|
// Check if i18n exists
|
|
|
|
if (!fs.existsSync('webapp/i18n/')) {
|
|
|
|
fs.mkdirSync('webapp/i18n/');
|
|
|
|
}
|
|
|
|
|
2017-01-06 18:43:13 +08:00
|
|
|
function next(i, err) {
|
|
|
|
errCheck(err);
|
|
|
|
|
|
|
|
if (i >= COPY_LIST.length) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ent = COPY_LIST[i];
|
|
|
|
const source = ent[0];
|
|
|
|
const dest = ent[1];
|
|
|
|
const opts = ent[2] || {};
|
2017-05-23 21:12:53 +08:00
|
|
|
let cpx = undefined;
|
2017-01-06 18:43:13 +08:00
|
|
|
|
2017-05-26 23:48:21 +08:00
|
|
|
if (!opts.lang) {
|
2017-05-23 21:12:53 +08:00
|
|
|
cpx = new Cpx.Cpx(source, dest);
|
|
|
|
}
|
2017-01-06 18:43:13 +08:00
|
|
|
|
2017-05-26 23:48:21 +08:00
|
|
|
if (verbose && cpx) {
|
2017-01-06 18:43:13 +08:00
|
|
|
cpx.on("copy", (event) => {
|
|
|
|
console.log(`Copied: ${event.srcPath} --> ${event.dstPath}`);
|
|
|
|
});
|
|
|
|
cpx.on("remove", (event) => {
|
|
|
|
console.log(`Removed: ${event.path}`);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-05-23 21:12:53 +08:00
|
|
|
const cb = (err) => { next(i + 1, err) };
|
2017-01-06 18:43:13 +08:00
|
|
|
|
|
|
|
if (watch) {
|
|
|
|
if (opts.directwatch) {
|
|
|
|
// cpx -w creates a watcher for the parent of any files specified,
|
|
|
|
// which in the case of config.json is '.', which inevitably takes
|
|
|
|
// ages to crawl. So we create our own watcher on the files
|
|
|
|
// instead.
|
2017-05-23 21:12:53 +08:00
|
|
|
const copy = () => { cpx.copy(errCheck) };
|
2017-01-06 18:43:13 +08:00
|
|
|
chokidar.watch(source)
|
|
|
|
.on('add', copy)
|
|
|
|
.on('change', copy)
|
|
|
|
.on('ready', cb)
|
|
|
|
.on('error', errCheck);
|
2017-05-26 23:48:21 +08:00
|
|
|
} else if (opts.lang) {
|
|
|
|
const reactSdkFile = 'node_modules/matrix-react-sdk/src/i18n/strings/' + source + '.json';
|
|
|
|
const riotWebFile = 'src/i18n/strings/' + source + '.json';
|
|
|
|
|
2018-03-02 23:30:06 +08:00
|
|
|
// XXX: Use a debounce because for some reason if we read the language
|
|
|
|
// file immediately after the FS event is received, the file contents
|
|
|
|
// appears empty. Possibly https://github.com/nodejs/node/issues/6112
|
|
|
|
let makeLangDebouncer;
|
|
|
|
const makeLang = () => {
|
|
|
|
if (makeLangDebouncer) {
|
|
|
|
clearTimeout(makeLangDebouncer);
|
|
|
|
}
|
|
|
|
makeLangDebouncer = setTimeout(() => {
|
|
|
|
genLangFile(source, dest);
|
|
|
|
}, 500);
|
|
|
|
};
|
|
|
|
|
2017-05-26 23:48:21 +08:00
|
|
|
[reactSdkFile, riotWebFile].forEach(function(f) {
|
|
|
|
chokidar.watch(f)
|
|
|
|
.on('add', makeLang)
|
|
|
|
.on('change', makeLang)
|
|
|
|
//.on('ready', cb) We'd have to do this when both files are ready
|
|
|
|
.on('error', errCheck);
|
|
|
|
});
|
2017-05-23 21:12:53 +08:00
|
|
|
next(i + 1, err);
|
2017-01-06 18:43:13 +08:00
|
|
|
} else {
|
|
|
|
cpx.on('watch-ready', cb);
|
|
|
|
cpx.on("watch-error", cb);
|
|
|
|
cpx.watch();
|
|
|
|
}
|
2017-05-26 23:48:21 +08:00
|
|
|
} else if (opts.lang) {
|
|
|
|
genLangFile(source, dest);
|
2017-05-23 21:12:53 +08:00
|
|
|
next(i + 1, err);
|
2017-01-06 18:43:13 +08:00
|
|
|
} else {
|
|
|
|
cpx.copy(cb);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-26 23:48:21 +08:00
|
|
|
function genLangFile(lang, dest) {
|
|
|
|
const reactSdkFile = 'node_modules/matrix-react-sdk/src/i18n/strings/' + lang + '.json';
|
|
|
|
const riotWebFile = 'src/i18n/strings/' + lang + '.json';
|
|
|
|
|
2017-09-05 00:12:13 +08:00
|
|
|
let translations = {};
|
2017-05-26 23:48:21 +08:00
|
|
|
[reactSdkFile, riotWebFile].forEach(function(f) {
|
|
|
|
if (fs.existsSync(f)) {
|
2017-10-11 16:56:38 +08:00
|
|
|
try {
|
|
|
|
Object.assign(
|
|
|
|
translations,
|
|
|
|
JSON.parse(fs.readFileSync(f).toString())
|
|
|
|
);
|
|
|
|
} catch (e) {
|
2018-03-02 23:30:06 +08:00
|
|
|
console.error("Failed: " + f, e);
|
2017-10-11 16:56:38 +08:00
|
|
|
throw e;
|
|
|
|
}
|
2017-05-26 23:48:21 +08:00
|
|
|
}
|
|
|
|
});
|
2017-09-05 00:12:13 +08:00
|
|
|
|
2018-03-02 23:30:06 +08:00
|
|
|
translations = weblateToCounterpart(translations);
|
2017-09-05 00:12:13 +08:00
|
|
|
|
2017-05-26 23:48:21 +08:00
|
|
|
fs.writeFileSync(dest + lang + '.json', JSON.stringify(translations, null, 4));
|
|
|
|
if (verbose) {
|
|
|
|
console.log("Generated language file: " + lang);
|
|
|
|
}
|
2017-05-23 21:12:53 +08:00
|
|
|
}
|
|
|
|
|
2017-05-26 23:48:21 +08:00
|
|
|
function genLangList() {
|
|
|
|
const languages = {};
|
|
|
|
INCLUDE_LANGS.forEach(function(lang) {
|
2017-06-01 21:38:36 +08:00
|
|
|
const normalizedLanguage = lang.value.toLowerCase().replace("_", "-");
|
2017-05-26 23:48:21 +08:00
|
|
|
const languageParts = normalizedLanguage.split('-');
|
|
|
|
if (languageParts.length == 2 && languageParts[0] == languageParts[1]) {
|
2017-06-01 21:38:36 +08:00
|
|
|
languages[languageParts[0]] = {'fileName': lang.value + '.json', 'label': lang.label};
|
2017-05-26 23:48:21 +08:00
|
|
|
} else {
|
2017-06-01 21:38:36 +08:00
|
|
|
languages[normalizedLanguage] = {'fileName': lang.value + '.json', 'label': lang.label};
|
2017-05-23 21:12:53 +08:00
|
|
|
}
|
|
|
|
});
|
2017-06-04 18:36:14 +08:00
|
|
|
fs.writeFile('webapp/i18n/languages.json', JSON.stringify(languages, null, 4), function(err) {
|
|
|
|
if (err) {
|
|
|
|
console.error("Copy Error occured: " + err);
|
|
|
|
throw new Error("Failed to generate languages.json");
|
|
|
|
}
|
|
|
|
});
|
2017-05-26 23:48:21 +08:00
|
|
|
if (verbose) {
|
2017-06-04 18:36:14 +08:00
|
|
|
console.log("Generated languages.json");
|
2017-05-26 23:48:21 +08:00
|
|
|
}
|
|
|
|
}
|
2017-05-23 21:12:53 +08:00
|
|
|
|
2017-09-05 00:12:13 +08:00
|
|
|
/**
|
|
|
|
* Convert translation key from weblate format
|
|
|
|
* (which only supports a single level) to counterpart
|
|
|
|
* which requires object values for 'count' translations.
|
|
|
|
*
|
|
|
|
* eg.
|
|
|
|
* "there are %(count)s badgers|one": "a badger",
|
|
|
|
* "there are %(count)s badgers|other": "%(count)s badgers"
|
|
|
|
* becomes
|
|
|
|
* "there are %(count)s badgers": {
|
|
|
|
* "one": "a badger",
|
|
|
|
* "other": "%(count)s badgers"
|
|
|
|
* }
|
|
|
|
*/
|
|
|
|
function weblateToCounterpart(inTrs) {
|
|
|
|
const outTrs = {};
|
|
|
|
|
|
|
|
for (const key of Object.keys(inTrs)) {
|
|
|
|
const keyParts = key.split('|', 2);
|
|
|
|
if (keyParts.length === 2) {
|
|
|
|
let obj = outTrs[keyParts[0]];
|
|
|
|
if (obj === undefined) {
|
|
|
|
obj = {};
|
|
|
|
outTrs[keyParts[0]] = obj;
|
|
|
|
}
|
|
|
|
obj[keyParts[1]] = inTrs[key];
|
|
|
|
} else {
|
|
|
|
outTrs[key] = inTrs[key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return outTrs;
|
|
|
|
}
|
|
|
|
|
2017-05-26 23:48:21 +08:00
|
|
|
genLangList();
|
|
|
|
next(0);
|