Simplify languages.json file and grab language names from Intl (#26001)

This commit is contained in:
Michael Telatynski 2023-08-22 15:07:17 +01:00 committed by GitHub
parent c13816dce4
commit 86c563cd29
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -5,58 +5,56 @@ const loaderUtils = require("loader-utils");
// copies the resources into the webapp directory. // copies the resources into the webapp directory.
// //
// Languages are listed manually so we can choose when to include // Languages are listed manually, so we can choose when to include a translation in the app
// a translation in the app (because having a translation with only // (because having a translation with only 3 strings translated is just frustrating)
// 3 strings translated is just frustrating) // This could readily be automated, but it's nice to explicitly control when new languages are available.
// This could readily be automated, but it's nice to explicitly
// control when new languages are available.
const INCLUDE_LANGS = [ const INCLUDE_LANGS = [
{ value: "bg", label: "Български" }, "bg",
{ value: "ca", label: "Català" }, "ca",
{ value: "cs", label: "čeština" }, "cs",
{ value: "da", label: "Dansk" }, "da",
{ value: "de_DE", label: "Deutsch" }, "de_DE",
{ value: "el", label: "Ελληνικά" }, "el",
{ value: "en_EN", label: "English" }, "en_EN",
{ value: "en_US", label: "English (US)" }, "en_US",
{ value: "eo", label: "Esperanto" }, "eo",
{ value: "es", label: "Español" }, "es",
{ value: "et", label: "Eesti" }, "et",
{ value: "eu", label: "Euskara" }, "eu",
{ value: "fi", label: "Suomi" }, "fi",
{ value: "fr", label: "Français" }, "fr",
{ value: "gl", label: "Galego" }, "gl",
{ value: "he", label: "עברית" }, "he",
{ value: "hi", label: "हिन्दी" }, "hi",
{ value: "hu", label: "Magyar" }, "hu",
{ value: "id", label: "Bahasa Indonesia" }, "id",
{ value: "is", label: "íslenska" }, "is",
{ value: "it", label: "Italiano" }, "it",
{ value: "ja", label: "日本語" }, "ja",
{ value: "kab", label: "Taqbaylit" }, "kab",
{ value: "ko", label: "한국어" }, "ko",
{ value: "lo", label: "ລາວ" }, "lo",
{ value: "lt", label: "Lietuvių" }, "lt",
{ value: "lv", label: "Latviešu" }, "lv",
{ value: "nb_NO", label: "Norwegian Bokmål" }, "nb_NO",
{ value: "nl", label: "Nederlands" }, "nl",
{ value: "nn", label: "Norsk Nynorsk" }, "nn",
{ value: "pl", label: "Polski" }, "pl",
{ value: "pt", label: "Português" }, "pt",
{ value: "pt_BR", label: "Português do Brasil" }, "pt_BR",
{ value: "ru", label: "Русский" }, "ru",
{ value: "sk", label: "Slovenčina" }, "sk",
{ value: "sq", label: "Shqip" }, "sq",
{ value: "sr", label: "српски" }, "sr",
{ value: "sv", label: "Svenska" }, "sv",
{ value: "te", label: "తెలుగు" }, "te",
{ value: "th", label: "ไทย" }, "th",
{ value: "tr", label: "Türkçe" }, "tr",
{ value: "uk", label: "українська мова" }, "uk",
{ value: "vi", label: "Tiếng Việt" }, "vi",
{ value: "vls", label: "West-Vlaams" }, "vls",
{ value: "zh_Hans", label: "简体中文" }, // simplified chinese "zh_Hans",
{ value: "zh_Hant", label: "繁體中文" }, // traditional chinese "zh_Hant",
]; ];
// cpx includes globbed parts of the filename in the destination, but excludes // cpx includes globbed parts of the filename in the destination, but excludes
@ -185,12 +183,12 @@ function genLangFile(lang, dest) {
function genLangList(langFileMap) { function genLangList(langFileMap) {
const languages = {}; const languages = {};
INCLUDE_LANGS.forEach(function (lang) { INCLUDE_LANGS.forEach(function (lang) {
const normalizedLanguage = lang.value.toLowerCase().replace("_", "-"); const normalizedLanguage = lang.toLowerCase().replace("_", "-");
const languageParts = normalizedLanguage.split("-"); const languageParts = normalizedLanguage.split("-");
if (languageParts.length == 2 && languageParts[0] == languageParts[1]) { if (languageParts.length == 2 && languageParts[0] == languageParts[1]) {
languages[languageParts[0]] = { fileName: langFileMap[lang.value], label: lang.label }; languages[languageParts[0]] = langFileMap[lang];
} else { } else {
languages[normalizedLanguage] = { fileName: langFileMap[lang.value], label: lang.label }; languages[normalizedLanguage] = langFileMap[lang];
} }
}); });
fs.writeFile("webapp/i18n/languages.json", JSON.stringify(languages, null, 4), function (err) { fs.writeFile("webapp/i18n/languages.json", JSON.stringify(languages, null, 4), function (err) {
@ -204,11 +202,11 @@ function genLangList(langFileMap) {
} }
} }
/** /*
watch the input files for a given language, * watch the input files for a given language,
regenerate the file, adding its content-hashed filename to langFileMap * regenerate the file, adding its content-hashed filename to langFileMap
and regenerating languages.json with the new filename * and regenerating languages.json with the new filename
*/ */
function watchLanguage(lang, dest, langFileMap) { function watchLanguage(lang, dest, langFileMap) {
const reactSdkFile = "node_modules/matrix-react-sdk/src/i18n/strings/" + lang + ".json"; const reactSdkFile = "node_modules/matrix-react-sdk/src/i18n/strings/" + lang + ".json";
const riotWebFile = "src/i18n/strings/" + lang + ".json"; const riotWebFile = "src/i18n/strings/" + lang + ".json";
@ -236,8 +234,8 @@ function watchLanguage(lang, dest, langFileMap) {
// language resources // language resources
const I18N_DEST = "webapp/i18n/"; const I18N_DEST = "webapp/i18n/";
const I18N_FILENAME_MAP = INCLUDE_LANGS.reduce((m, l) => { const I18N_FILENAME_MAP = INCLUDE_LANGS.reduce((m, l) => {
const filename = genLangFile(l.value, I18N_DEST); const filename = genLangFile(l, I18N_DEST);
m[l.value] = filename; m[l] = filename;
return m; return m;
}, {}); }, {});
genLangList(I18N_FILENAME_MAP); genLangList(I18N_FILENAME_MAP);