mirror of
https://github.com/vector-im/element-web.git
synced 2025-12-25 03:01:10 +01:00
Merge element web & shared component translations
for the built app
This commit is contained in:
parent
945626770a
commit
e11d416fd3
@ -9,8 +9,10 @@ import _ from "lodash";
|
||||
import webpack from "webpack";
|
||||
import type { Translations } from "matrix-web-i18n";
|
||||
|
||||
const I18N_BASE_PATH = "src/i18n/strings/";
|
||||
const INCLUDE_LANGS = [...new Set([...fs.readdirSync(I18N_BASE_PATH)])]
|
||||
const EW_I18N_BASE_PATH = "src/i18n/strings/";
|
||||
const SC_I18N_BASE_PATH = "packages/shared-components/src/i18n/strings/";
|
||||
|
||||
const INCLUDE_LANGS = [...new Set([...fs.readdirSync(EW_I18N_BASE_PATH)])]
|
||||
.filter((fn) => fn.endsWith(".json"))
|
||||
.map((f) => f.slice(0, -5));
|
||||
|
||||
@ -41,11 +43,17 @@ const logWatch = (path: string) => {
|
||||
}
|
||||
};
|
||||
|
||||
function prepareLangFile(lang: string, dest: string): [filename: string, json: string] {
|
||||
const path = I18N_BASE_PATH + lang + ".json";
|
||||
/*
|
||||
* Make a JSON language file for the given language by merging all translations
|
||||
* into a single file (ie. element-web and shared-components).
|
||||
* Returns the filename (including hash) and JSON content.
|
||||
*/
|
||||
function prepareLangFile(lang: string): [filename: string, json: string] {
|
||||
const ewTranslationsPath = EW_I18N_BASE_PATH + lang + ".json";
|
||||
const scTranslationsPath = SC_I18N_BASE_PATH + lang + ".json";
|
||||
|
||||
let translations: Translations = {};
|
||||
[path].forEach(function (f) {
|
||||
[ewTranslationsPath, scTranslationsPath].forEach(function (f) {
|
||||
if (fs.existsSync(f)) {
|
||||
try {
|
||||
translations = _.merge(translations, JSON.parse(fs.readFileSync(f).toString()));
|
||||
@ -99,7 +107,7 @@ function genLangList(langFileMap: Record<string, string>): void {
|
||||
* and regenerating languages.json with the new filename
|
||||
*/
|
||||
function watchLanguage(lang: string, dest: string, langFileMap: Record<string, string>): void {
|
||||
const path = I18N_BASE_PATH + lang + ".json";
|
||||
const path = EW_I18N_BASE_PATH + 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
|
||||
@ -110,7 +118,7 @@ function watchLanguage(lang: string, dest: string, langFileMap: Record<string, s
|
||||
clearTimeout(makeLangDebouncer);
|
||||
}
|
||||
makeLangDebouncer = setTimeout(() => {
|
||||
const [filename, json] = prepareLangFile(lang, dest);
|
||||
const [filename, json] = prepareLangFile(lang);
|
||||
genLangFile(dest, filename, json);
|
||||
langFileMap[lang] = filename;
|
||||
genLangList(langFileMap);
|
||||
@ -132,7 +140,7 @@ function watchLanguage(lang: string, dest: string, langFileMap: Record<string, s
|
||||
// language resources
|
||||
const I18N_DEST = "webapp/i18n/";
|
||||
const I18N_FILENAME_MAP = INCLUDE_LANGS.reduce<Record<string, string>>((m, l) => {
|
||||
const [filename, json] = prepareLangFile(l, I18N_DEST);
|
||||
const [filename, json] = prepareLangFile(l);
|
||||
if (!watch) {
|
||||
genLangFile(I18N_DEST, filename, json);
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user