2017-01-06 05:43:13 -05:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
2019-02-18 10:11:41 -05:00
|
|
|
const loaderUtils = require("loader-utils");
|
|
|
|
|
2017-01-06 05:43:13 -05:00
|
|
|
// copies the resources into the webapp directory.
|
|
|
|
//
|
|
|
|
|
2017-05-26 11:48:21 -04: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 06:27:10 -04:00
|
|
|
// control when new languages are available.
|
2017-05-26 11:48:21 -04:00
|
|
|
const INCLUDE_LANGS = [
|
2018-02-24 03:28:39 -05:00
|
|
|
{ value: "bg", label: "Български" },
|
2018-01-25 12:25:46 -05:00
|
|
|
{ value: "ca", label: "Català" },
|
2018-03-17 06:27:10 -04:00
|
|
|
{ value: "cs", label: "čeština" },
|
2017-08-16 06:14:24 -04:00
|
|
|
{ value: "da", label: "Dansk" },
|
|
|
|
{ value: "de_DE", label: "Deutsch" },
|
2018-03-17 11:24:47 -04:00
|
|
|
{ value: "el", label: "Ελληνικά" },
|
2017-06-01 09:38:36 -04:00
|
|
|
{ value: "en_EN", label: "English" },
|
2017-06-05 11:05:07 -04:00
|
|
|
{ value: "en_US", label: "English (US)" },
|
2017-06-18 18:50:13 -04:00
|
|
|
{ value: "eo", label: "Esperanto" },
|
2017-08-16 06:14:24 -04:00
|
|
|
{ value: "es", label: "Español" },
|
2020-09-10 05:03:13 -04:00
|
|
|
{ value: "et", label: "Eesti" },
|
2018-01-07 14:39:24 -05:00
|
|
|
{ value: "eu", label: "Euskara" },
|
2017-09-15 07:26:45 -04:00
|
|
|
{ value: "fi", label: "Suomi" },
|
2017-06-01 09:38:36 -04:00
|
|
|
{ value: "fr", label: "Français" },
|
2018-03-17 06:27:10 -04:00
|
|
|
{ value: "gl", label: "Galego" },
|
2021-01-19 06:16:56 -05:00
|
|
|
{ value: "he", label: "עברית" },
|
2019-12-02 22:54:50 -05:00
|
|
|
{ value: "hi", label: "हिन्दी" },
|
2017-06-18 18:50:13 -04:00
|
|
|
{ value: "hu", label: "Magyar" },
|
2021-12-03 07:40:56 -05:00
|
|
|
{ value: "id", label: "Bahasa Indonesia" },
|
2019-12-03 11:52:10 -05:00
|
|
|
{ value: "is", label: "íslenska" },
|
2018-08-16 10:04:17 -04:00
|
|
|
{ value: "it", label: "Italiano" },
|
2018-11-08 14:30:45 -05:00
|
|
|
{ value: "ja", label: "日本語" },
|
2020-08-22 16:16:55 -04:00
|
|
|
{ value: "kab", label: "Taqbaylit" },
|
2017-06-16 08:05:56 -04:00
|
|
|
{ value: "ko", label: "한국어" },
|
2022-05-26 08:49:58 -04:00
|
|
|
{ value: "lo", label: "ລາວ" },
|
2019-12-04 01:12:18 -05:00
|
|
|
{ value: "lt", label: "Lietuvių" },
|
2017-08-16 06:15:48 -04:00
|
|
|
{ value: "lv", label: "Latviešu" },
|
2017-06-18 18:50:13 -04:00
|
|
|
{ value: "nb_NO", label: "Norwegian Bokmål" },
|
2017-08-16 06:14:24 -04:00
|
|
|
{ value: "nl", label: "Nederlands" },
|
2018-08-16 10:04:17 -04:00
|
|
|
{ value: "nn", label: "Norsk Nynorsk" },
|
2017-06-18 18:50:13 -04:00
|
|
|
{ value: "pl", label: "Polski" },
|
2017-06-01 09:38:36 -04:00
|
|
|
{ value: "pt", label: "Português" },
|
|
|
|
{ value: "pt_BR", label: "Português do Brasil" },
|
|
|
|
{ value: "ru", label: "Русский" },
|
2017-11-15 07:14:30 -05:00
|
|
|
{ value: "sk", label: "Slovenčina" },
|
2019-06-04 12:45:59 -04:00
|
|
|
{ value: "sq", label: "Shqip" },
|
2018-03-17 11:24:47 -04:00
|
|
|
{ value: "sr", label: "српски" },
|
2018-03-17 06:27:10 -04:00
|
|
|
{ value: "sv", label: "Svenska" },
|
2017-08-16 06:15:48 -04:00
|
|
|
{ value: "te", label: "తెలుగు" },
|
2018-03-17 11:24:47 -04:00
|
|
|
{ value: "th", label: "ไทย" },
|
2019-10-11 07:56:53 -04:00
|
|
|
{ value: "tr", label: "Türkçe" },
|
2019-12-03 11:52:10 -05:00
|
|
|
{ value: "uk", label: "українська мова" },
|
2021-12-14 05:16:24 -05:00
|
|
|
{ value: "vi", label: "Tiếng Việt" },
|
2019-06-04 12:43:28 -04:00
|
|
|
{ value: "vls", label: "West-Vlaams" },
|
2017-06-06 11:51:00 -04:00
|
|
|
{ value: "zh_Hans", label: "简体中文" }, // simplified chinese
|
|
|
|
{ value: "zh_Hant", label: "繁體中文" }, // traditional chinese
|
2017-05-26 11:48:21 -04:00
|
|
|
];
|
|
|
|
|
2017-01-06 05:43:13 -05: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 = [
|
2022-11-08 08:27:20 -05:00
|
|
|
["res/apple-app-site-association", "webapp"],
|
2017-05-11 08:12:26 -04:00
|
|
|
["res/manifest.json", "webapp"],
|
2020-10-01 09:05:07 -04:00
|
|
|
["res/sw.js", "webapp"],
|
2019-02-07 12:00:01 -05:00
|
|
|
["res/welcome.html", "webapp"],
|
|
|
|
["res/welcome/**", "webapp/welcome"],
|
2018-04-12 20:49:52 -04:00
|
|
|
["res/themes/**", "webapp/themes"],
|
2018-12-31 18:22:47 -05:00
|
|
|
["res/vector-icons/**", "webapp/vector-icons"],
|
2020-04-30 08:51:50 -04:00
|
|
|
["res/decoder-ring/**", "webapp/decoder-ring"],
|
2018-12-31 18:22:47 -05:00
|
|
|
["node_modules/matrix-react-sdk/res/media/**", "webapp/media"],
|
2021-05-30 00:48:11 -04:00
|
|
|
["node_modules/@matrix-org/olm/olm_legacy.js", "webapp", { directwatch: 1 }],
|
2017-05-23 09:12:53 -04:00
|
|
|
["./config.json", "webapp", { directwatch: 1 }],
|
2020-02-05 09:28:44 -05:00
|
|
|
["contribute.json", "webapp"],
|
2017-01-06 05:43:13 -05:00
|
|
|
];
|
|
|
|
|
|
|
|
const parseArgs = require("minimist");
|
|
|
|
const Cpx = require("cpx");
|
|
|
|
const chokidar = require("chokidar");
|
2017-05-23 09:12:53 -04:00
|
|
|
const fs = require("fs");
|
|
|
|
const rimraf = require("rimraf");
|
|
|
|
|
2017-01-06 05:43:13 -05:00
|
|
|
const argv = parseArgs(process.argv.slice(2), {});
|
|
|
|
|
2019-02-18 10:11:41 -05:00
|
|
|
const watch = argv.w;
|
|
|
|
const verbose = argv.v;
|
2017-01-06 05:43:13 -05:00
|
|
|
|
|
|
|
function errCheck(err) {
|
|
|
|
if (err) {
|
|
|
|
console.error(err.message);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-26 11:48:21 -04: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 05:43:13 -05: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 09:12:53 -04:00
|
|
|
let cpx = undefined;
|
2017-01-06 05:43:13 -05:00
|
|
|
|
2017-05-26 11:48:21 -04:00
|
|
|
if (!opts.lang) {
|
2017-05-23 09:12:53 -04:00
|
|
|
cpx = new Cpx.Cpx(source, dest);
|
|
|
|
}
|
2017-01-06 05:43:13 -05:00
|
|
|
|
2017-05-26 11:48:21 -04:00
|
|
|
if (verbose && cpx) {
|
2017-01-06 05:43:13 -05:00
|
|
|
cpx.on("copy", (event) => {
|
|
|
|
console.log(`Copied: ${event.srcPath} --> ${event.dstPath}`);
|
|
|
|
});
|
|
|
|
cpx.on("remove", (event) => {
|
|
|
|
console.log(`Removed: ${event.path}`);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-05-23 09:12:53 -04:00
|
|
|
const cb = (err) => {
|
|
|
|
next(i + 1, err);
|
|
|
|
};
|
2017-01-06 05:43:13 -05: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 09:12:53 -04:00
|
|
|
const copy = () => {
|
|
|
|
cpx.copy(errCheck);
|
|
|
|
};
|
2017-01-06 05:43:13 -05:00
|
|
|
chokidar.watch(source).on("add", copy).on("change", copy).on("ready", cb).on("error", errCheck);
|
|
|
|
} else {
|
|
|
|
cpx.on("watch-ready", cb);
|
|
|
|
cpx.on("watch-error", cb);
|
|
|
|
cpx.watch();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
cpx.copy(cb);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-26 11:48:21 -04: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-04 12:12:13 -04:00
|
|
|
let translations = {};
|
2017-05-26 11:48:21 -04:00
|
|
|
[reactSdkFile, riotWebFile].forEach(function (f) {
|
|
|
|
if (fs.existsSync(f)) {
|
2017-10-11 04:56:38 -04:00
|
|
|
try {
|
|
|
|
Object.assign(translations, JSON.parse(fs.readFileSync(f).toString()));
|
|
|
|
} catch (e) {
|
2018-03-02 10:30:06 -05:00
|
|
|
console.error("Failed: " + f, e);
|
2017-10-11 04:56:38 -04:00
|
|
|
throw e;
|
|
|
|
}
|
2017-05-26 11:48:21 -04:00
|
|
|
}
|
|
|
|
});
|
2017-09-04 12:12:13 -04:00
|
|
|
|
2018-03-02 10:30:06 -05:00
|
|
|
translations = weblateToCounterpart(translations);
|
2017-09-04 12:12:13 -04:00
|
|
|
|
2019-02-18 10:11:41 -05:00
|
|
|
const json = JSON.stringify(translations, null, 4);
|
|
|
|
const jsonBuffer = Buffer.from(json);
|
2019-02-20 03:39:27 -05:00
|
|
|
const digest = loaderUtils.getHashDigest(jsonBuffer, null, null, 7);
|
2019-02-18 10:11:41 -05:00
|
|
|
const filename = `${lang}.${digest}.json`;
|
|
|
|
|
|
|
|
fs.writeFileSync(dest + filename, json);
|
2017-05-26 11:48:21 -04:00
|
|
|
if (verbose) {
|
2019-02-18 10:11:41 -05:00
|
|
|
console.log("Generated language file: " + filename);
|
2017-05-26 11:48:21 -04:00
|
|
|
}
|
2019-02-18 10:11:41 -05:00
|
|
|
|
|
|
|
return filename;
|
2017-05-23 09:12:53 -04:00
|
|
|
}
|
|
|
|
|
2019-02-18 10:11:41 -05:00
|
|
|
function genLangList(langFileMap) {
|
2017-05-26 11:48:21 -04:00
|
|
|
const languages = {};
|
|
|
|
INCLUDE_LANGS.forEach(function (lang) {
|
2017-06-01 09:38:36 -04:00
|
|
|
const normalizedLanguage = lang.value.toLowerCase().replace("_", "-");
|
2017-05-26 11:48:21 -04:00
|
|
|
const languageParts = normalizedLanguage.split("-");
|
|
|
|
if (languageParts.length == 2 && languageParts[0] == languageParts[1]) {
|
2019-02-18 10:11:41 -05:00
|
|
|
languages[languageParts[0]] = { fileName: langFileMap[lang.value], label: lang.label };
|
2017-05-26 11:48:21 -04:00
|
|
|
} else {
|
2019-02-18 10:11:41 -05:00
|
|
|
languages[normalizedLanguage] = { fileName: langFileMap[lang.value], label: lang.label };
|
2017-05-23 09:12:53 -04:00
|
|
|
}
|
|
|
|
});
|
2017-06-04 06:36:14 -04: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 11:48:21 -04:00
|
|
|
if (verbose) {
|
2017-06-04 06:36:14 -04:00
|
|
|
console.log("Generated languages.json");
|
2017-05-26 11:48:21 -04:00
|
|
|
}
|
|
|
|
}
|
2017-05-23 09:12:53 -04:00
|
|
|
|
2017-09-04 12:12:13 -04: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) {
|
2022-05-17 12:44:30 -04:00
|
|
|
obj = outTrs[keyParts[0]] = {};
|
|
|
|
} else if (typeof obj === "string") {
|
|
|
|
// This is a transitional edge case if a string went from singular to pluralised and both still remain
|
|
|
|
// in the translation json file. Use the singular translation as `other` and merge pluralisation atop.
|
|
|
|
obj = outTrs[keyParts[0]] = {
|
|
|
|
other: inTrs[key],
|
|
|
|
};
|
|
|
|
console.warn("Found entry in i18n file in both singular and pluralised form", keyParts[0]);
|
2017-09-04 12:12:13 -04:00
|
|
|
}
|
|
|
|
obj[keyParts[1]] = inTrs[key];
|
|
|
|
} else {
|
|
|
|
outTrs[key] = inTrs[key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return outTrs;
|
|
|
|
}
|
|
|
|
|
2019-02-18 10:11:41 -05:00
|
|
|
/**
|
|
|
|
watch the input files for a given language,
|
|
|
|
regenerate the file, adding its content-hashed filename to langFileMap
|
|
|
|
and regenerating languages.json with the new filename
|
|
|
|
*/
|
|
|
|
function watchLanguage(lang, dest, langFileMap) {
|
|
|
|
const reactSdkFile = "node_modules/matrix-react-sdk/src/i18n/strings/" + lang + ".json";
|
|
|
|
const riotWebFile = "src/i18n/strings/" + lang + ".json";
|
|
|
|
|
|
|
|
// 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(() => {
|
|
|
|
const filename = genLangFile(lang, dest);
|
|
|
|
langFileMap[lang] = filename;
|
|
|
|
genLangList(langFileMap);
|
|
|
|
}, 500);
|
|
|
|
};
|
|
|
|
|
|
|
|
[reactSdkFile, riotWebFile].forEach(function (f) {
|
|
|
|
chokidar.watch(f).on("add", makeLang).on("change", makeLang).on("error", errCheck);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// language resources
|
|
|
|
const I18N_DEST = "webapp/i18n/";
|
|
|
|
const I18N_FILENAME_MAP = INCLUDE_LANGS.reduce((m, l) => {
|
|
|
|
const filename = genLangFile(l.value, I18N_DEST);
|
|
|
|
m[l.value] = filename;
|
|
|
|
return m;
|
|
|
|
}, {});
|
|
|
|
genLangList(I18N_FILENAME_MAP);
|
|
|
|
|
|
|
|
if (watch) {
|
|
|
|
INCLUDE_LANGS.forEach((l) => watchLanguage(l.value, I18N_DEST, I18N_FILENAME_MAP));
|
|
|
|
}
|
|
|
|
|
|
|
|
// non-language resources
|
2017-05-26 11:48:21 -04:00
|
|
|
next(0);
|