Skip to content

Commit

Permalink
[GENERIC viewer] Try to fixup "incomplete" language codes (issue 13689)
Browse files Browse the repository at this point in the history
While I don't know if it's technically correct to even do this, it could provide a slightly better out-of-the-box behaviour in browsers that specify (from the PDF.js `l10n`-folder perspective) "incomplete" language codes.

Rather than immediately falling back to English, we'll use a white-list to try and re-write a "partial" language code to a (hopefully) suitable one that matches an existing `l10n`-folder. The disadvantage of this solution is that the list needs to be kept *manually* up-to-date with any changes in the `l10n`-folder, however new locales are added infrequently enough that this should be acceptable.

Fixes 13689 (assuming we actually want/care to do so, otherwise we should just WONTFIX the issue).
  • Loading branch information
Snuffleupagus committed Jul 8, 2021
1 parent c33bf0b commit 29fd39a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
4 changes: 2 additions & 2 deletions web/genericl10n.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@
*/

import "../external/webL10n/l10n.js";
import { getL10nFallback } from "./l10n_utils.js";
import { fixupLangCode, getL10nFallback } from "./l10n_utils.js";

const webL10n = document.webL10n;

class GenericL10n {
constructor(lang) {
this._lang = lang;
this._ready = new Promise((resolve, reject) => {
webL10n.setLanguage(lang, () => {
webL10n.setLanguage(fixupLangCode(lang), () => {
resolve(webL10n);
});
});
Expand Down
24 changes: 23 additions & 1 deletion web/l10n_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -125,4 +147,4 @@ const NullL10n = {
async translate(element) {},
};

export { getL10nFallback, NullL10n };
export { fixupLangCode, getL10nFallback, NullL10n };

0 comments on commit 29fd39a

Please sign in to comment.