Merge pull request #13696 from Snuffleupagus/issue-13689

[GENERIC viewer] Try to fixup "incomplete" language codes (issue 13689)
This commit is contained in:
Tim van der Meij 2021-07-09 23:01:19 +02:00 committed by GitHub
commit 62b5dc9eb7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 3 deletions

View File

@ -14,7 +14,7 @@
*/
import "../external/webL10n/l10n.js";
import { getL10nFallback } from "./l10n_utils.js";
import { fixupLangCode, getL10nFallback } from "./l10n_utils.js";
const webL10n = document.webL10n;
@ -22,7 +22,7 @@ class GenericL10n {
constructor(lang) {
this._lang = lang;
this._ready = new Promise((resolve, reject) => {
webL10n.setLanguage(lang, () => {
webL10n.setLanguage(fixupLangCode(lang), () => {
resolve(webL10n);
});
});

View File

@ -95,6 +95,28 @@ function getL10nFallback(key, args) {
return DEFAULT_L10N_STRINGS[key] || "";
}
const PARTIAL_LANG_CODES = {
en: "en-US",
es: "es-ES",
fy: "fy-NL",
ga: "ga-IE",
gu: "gu-IN",
hi: "hi-IN",
hy: "hy-AM",
nb: "nb-NO",
ne: "ne-NP",
nn: "nn-NO",
pa: "pa-IN",
pt: "pt-PT",
sv: "sv-SE",
zh: "zh-CN",
};
// Try to support "incompletely" specified language codes (see issue 13689).
function fixupLangCode(langCode) {
return PARTIAL_LANG_CODES[langCode?.toLowerCase()] || langCode;
}
// Replaces {{arguments}} with their values.
function formatL10nValue(text, args) {
if (!args) {
@ -125,4 +147,4 @@ const NullL10n = {
async translate(element) {},
};
export { getL10nFallback, NullL10n };
export { fixupLangCode, getL10nFallback, NullL10n };