copy-res.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. #!/usr/bin/env node
  2. const loaderUtils = require("loader-utils");
  3. // copies the resources into the webapp directory.
  4. //
  5. // Languages are listed manually so we can choose when to include
  6. // a translation in the app (because having a translation with only
  7. // 3 strings translated is just frustrating)
  8. // This could readily be automated, but it's nice to explicitly
  9. // control when new languages are available.
  10. const INCLUDE_LANGS = [
  11. { value: "bg", label: "Български" },
  12. { value: "ca", label: "Català" },
  13. { value: "cs", label: "čeština" },
  14. { value: "da", label: "Dansk" },
  15. { value: "de_DE", label: "Deutsch" },
  16. { value: "el", label: "Ελληνικά" },
  17. { value: "en_EN", label: "English" },
  18. { value: "en_US", label: "English (US)" },
  19. { value: "eo", label: "Esperanto" },
  20. { value: "es", label: "Español" },
  21. { value: "et", label: "Eesti" },
  22. { value: "eu", label: "Euskara" },
  23. { value: "fi", label: "Suomi" },
  24. { value: "fr", label: "Français" },
  25. { value: "gl", label: "Galego" },
  26. { value: "he", label: "עברית" },
  27. { value: "hi", label: "हिन्दी" },
  28. { value: "hu", label: "Magyar" },
  29. { value: "id", label: "Bahasa Indonesia" },
  30. { value: "is", label: "íslenska" },
  31. { value: "it", label: "Italiano" },
  32. { value: "ja", label: "日本語" },
  33. { value: "kab", label: "Taqbaylit" },
  34. { value: "ko", label: "한국어" },
  35. { value: "lo", label: "ລາວ" },
  36. { value: "lt", label: "Lietuvių" },
  37. { value: "lv", label: "Latviešu" },
  38. { value: "nb_NO", label: "Norwegian Bokmål" },
  39. { value: "nl", label: "Nederlands" },
  40. { value: "nn", label: "Norsk Nynorsk" },
  41. { value: "pl", label: "Polski" },
  42. { value: "pt", label: "Português" },
  43. { value: "pt_BR", label: "Português do Brasil" },
  44. { value: "ru", label: "Русский" },
  45. { value: "sk", label: "Slovenčina" },
  46. { value: "sq", label: "Shqip" },
  47. { value: "sr", label: "српски" },
  48. { value: "sv", label: "Svenska" },
  49. { value: "te", label: "తెలుగు" },
  50. { value: "th", label: "ไทย" },
  51. { value: "tr", label: "Türkçe" },
  52. { value: "uk", label: "українська мова" },
  53. { value: "vi", label: "Tiếng Việt" },
  54. { value: "vls", label: "West-Vlaams" },
  55. { value: "zh_Hans", label: "简体中文" }, // simplified chinese
  56. { value: "zh_Hant", label: "繁體中文" }, // traditional chinese
  57. ];
  58. // cpx includes globbed parts of the filename in the destination, but excludes
  59. // common parents. Hence, "res/{a,b}/**": the output will be "dest/a/..." and
  60. // "dest/b/...".
  61. const COPY_LIST = [
  62. ["res/apple-app-site-association", "webapp"],
  63. ["res/manifest.json", "webapp"],
  64. ["res/sw.js", "webapp"],
  65. ["res/welcome.html", "webapp"],
  66. ["res/welcome/**", "webapp/welcome"],
  67. ["res/themes/**", "webapp/themes"],
  68. ["res/vector-icons/**", "webapp/vector-icons"],
  69. ["res/decoder-ring/**", "webapp/decoder-ring"],
  70. ["node_modules/matrix-react-sdk/res/media/**", "webapp/media"],
  71. ["node_modules/@matrix-org/olm/olm_legacy.js", "webapp", { directwatch: 1 }],
  72. ["./config.json", "webapp", { directwatch: 1 }],
  73. ["contribute.json", "webapp"],
  74. ];
  75. const parseArgs = require("minimist");
  76. const Cpx = require("cpx");
  77. const chokidar = require("chokidar");
  78. const fs = require("fs");
  79. const rimraf = require("rimraf");
  80. const argv = parseArgs(process.argv.slice(2), {});
  81. const watch = argv.w;
  82. const verbose = argv.v;
  83. function errCheck(err) {
  84. if (err) {
  85. console.error(err.message);
  86. process.exit(1);
  87. }
  88. }
  89. // Check if webapp exists
  90. if (!fs.existsSync("webapp")) {
  91. fs.mkdirSync("webapp");
  92. }
  93. // Check if i18n exists
  94. if (!fs.existsSync("webapp/i18n/")) {
  95. fs.mkdirSync("webapp/i18n/");
  96. }
  97. function next(i, err) {
  98. errCheck(err);
  99. if (i >= COPY_LIST.length) {
  100. return;
  101. }
  102. const ent = COPY_LIST[i];
  103. const source = ent[0];
  104. const dest = ent[1];
  105. const opts = ent[2] || {};
  106. let cpx = undefined;
  107. if (!opts.lang) {
  108. cpx = new Cpx.Cpx(source, dest);
  109. }
  110. if (verbose && cpx) {
  111. cpx.on("copy", (event) => {
  112. console.log(`Copied: ${event.srcPath} --> ${event.dstPath}`);
  113. });
  114. cpx.on("remove", (event) => {
  115. console.log(`Removed: ${event.path}`);
  116. });
  117. }
  118. const cb = (err) => {
  119. next(i + 1, err);
  120. };
  121. if (watch) {
  122. if (opts.directwatch) {
  123. // cpx -w creates a watcher for the parent of any files specified,
  124. // which in the case of config.json is '.', which inevitably takes
  125. // ages to crawl. So we create our own watcher on the files
  126. // instead.
  127. const copy = () => {
  128. cpx.copy(errCheck);
  129. };
  130. chokidar.watch(source).on("add", copy).on("change", copy).on("ready", cb).on("error", errCheck);
  131. } else {
  132. cpx.on("watch-ready", cb);
  133. cpx.on("watch-error", cb);
  134. cpx.watch();
  135. }
  136. } else {
  137. cpx.copy(cb);
  138. }
  139. }
  140. function genLangFile(lang, dest) {
  141. const reactSdkFile = "node_modules/matrix-react-sdk/src/i18n/strings/" + lang + ".json";
  142. const riotWebFile = "src/i18n/strings/" + lang + ".json";
  143. let translations = {};
  144. [reactSdkFile, riotWebFile].forEach(function (f) {
  145. if (fs.existsSync(f)) {
  146. try {
  147. Object.assign(translations, JSON.parse(fs.readFileSync(f).toString()));
  148. } catch (e) {
  149. console.error("Failed: " + f, e);
  150. throw e;
  151. }
  152. }
  153. });
  154. translations = weblateToCounterpart(translations);
  155. const json = JSON.stringify(translations, null, 4);
  156. const jsonBuffer = Buffer.from(json);
  157. const digest = loaderUtils.getHashDigest(jsonBuffer, null, null, 7);
  158. const filename = `${lang}.${digest}.json`;
  159. fs.writeFileSync(dest + filename, json);
  160. if (verbose) {
  161. console.log("Generated language file: " + filename);
  162. }
  163. return filename;
  164. }
  165. function genLangList(langFileMap) {
  166. const languages = {};
  167. INCLUDE_LANGS.forEach(function (lang) {
  168. const normalizedLanguage = lang.value.toLowerCase().replace("_", "-");
  169. const languageParts = normalizedLanguage.split("-");
  170. if (languageParts.length == 2 && languageParts[0] == languageParts[1]) {
  171. languages[languageParts[0]] = { fileName: langFileMap[lang.value], label: lang.label };
  172. } else {
  173. languages[normalizedLanguage] = { fileName: langFileMap[lang.value], label: lang.label };
  174. }
  175. });
  176. fs.writeFile("webapp/i18n/languages.json", JSON.stringify(languages, null, 4), function (err) {
  177. if (err) {
  178. console.error("Copy Error occured: " + err);
  179. throw new Error("Failed to generate languages.json");
  180. }
  181. });
  182. if (verbose) {
  183. console.log("Generated languages.json");
  184. }
  185. }
  186. /**
  187. * Convert translation key from weblate format
  188. * (which only supports a single level) to counterpart
  189. * which requires object values for 'count' translations.
  190. *
  191. * eg.
  192. * "there are %(count)s badgers|one": "a badger",
  193. * "there are %(count)s badgers|other": "%(count)s badgers"
  194. * becomes
  195. * "there are %(count)s badgers": {
  196. * "one": "a badger",
  197. * "other": "%(count)s badgers"
  198. * }
  199. */
  200. function weblateToCounterpart(inTrs) {
  201. const outTrs = {};
  202. for (const key of Object.keys(inTrs)) {
  203. const keyParts = key.split("|", 2);
  204. if (keyParts.length === 2) {
  205. let obj = outTrs[keyParts[0]];
  206. if (obj === undefined) {
  207. obj = outTrs[keyParts[0]] = {};
  208. } else if (typeof obj === "string") {
  209. // This is a transitional edge case if a string went from singular to pluralised and both still remain
  210. // in the translation json file. Use the singular translation as `other` and merge pluralisation atop.
  211. obj = outTrs[keyParts[0]] = {
  212. other: inTrs[key],
  213. };
  214. console.warn("Found entry in i18n file in both singular and pluralised form", keyParts[0]);
  215. }
  216. obj[keyParts[1]] = inTrs[key];
  217. } else {
  218. outTrs[key] = inTrs[key];
  219. }
  220. }
  221. return outTrs;
  222. }
  223. /**
  224. watch the input files for a given language,
  225. regenerate the file, adding its content-hashed filename to langFileMap
  226. and regenerating languages.json with the new filename
  227. */
  228. function watchLanguage(lang, dest, langFileMap) {
  229. const reactSdkFile = "node_modules/matrix-react-sdk/src/i18n/strings/" + lang + ".json";
  230. const riotWebFile = "src/i18n/strings/" + lang + ".json";
  231. // XXX: Use a debounce because for some reason if we read the language
  232. // file immediately after the FS event is received, the file contents
  233. // appears empty. Possibly https://github.com/nodejs/node/issues/6112
  234. let makeLangDebouncer;
  235. const makeLang = () => {
  236. if (makeLangDebouncer) {
  237. clearTimeout(makeLangDebouncer);
  238. }
  239. makeLangDebouncer = setTimeout(() => {
  240. const filename = genLangFile(lang, dest);
  241. langFileMap[lang] = filename;
  242. genLangList(langFileMap);
  243. }, 500);
  244. };
  245. [reactSdkFile, riotWebFile].forEach(function (f) {
  246. chokidar.watch(f).on("add", makeLang).on("change", makeLang).on("error", errCheck);
  247. });
  248. }
  249. // language resources
  250. const I18N_DEST = "webapp/i18n/";
  251. const I18N_FILENAME_MAP = INCLUDE_LANGS.reduce((m, l) => {
  252. const filename = genLangFile(l.value, I18N_DEST);
  253. m[l.value] = filename;
  254. return m;
  255. }, {});
  256. genLangList(I18N_FILENAME_MAP);
  257. if (watch) {
  258. INCLUDE_LANGS.forEach((l) => watchLanguage(l.value, I18N_DEST, I18N_FILENAME_MAP));
  259. }
  260. // non-language resources
  261. next(0);