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