Skip to content

Commit

Permalink
Fix for Node environments with multiple languages
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenwf committed Feb 12, 2024
1 parent c3d3498 commit bf52b33
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions src/i18n.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { InternationalString } from '@iiif/presentation-3';

export function getClosestLanguage(
i18nLanguage: string,
i18nLanguage: string | undefined,
languages: string[],
i18nLanguages: string[] = [],
strictFallback = false,
Expand All @@ -20,19 +20,22 @@ export function getClosestLanguage(
return languages[0];
}

if (!i18nLanguage && languages.indexOf('none') !== -1) {
return 'none';
} else {
// Exact match.
if (languages.indexOf(i18nLanguage) !== -1) {
return i18nLanguage;
if (!i18nLanguage) {
if (languages.indexOf('none') !== -1) {
return 'none';
}
return languages[0];
}

// Root match (en-us === en)
const root = i18nLanguage.indexOf('-') !== -1 ? i18nLanguage.slice(0, i18nLanguage.indexOf('-')) : null;
if (root && languages.indexOf(root) !== -1) {
return root;
}
// Exact match.
if (languages.indexOf(i18nLanguage) !== -1) {
return i18nLanguage;
}

// Root match (en-us === en)
const root = i18nLanguage.indexOf('-') !== -1 ? i18nLanguage.slice(0, i18nLanguage.indexOf('-')) : null;
if (root && languages.indexOf(root) !== -1) {
return root;
}

// All of the fall backs.
Expand Down Expand Up @@ -96,7 +99,7 @@ export function buildLocaleString(
const languages = Object.keys(inputText || {});
const language = closest
? i18nLanguage
: getClosestLanguage(i18nLanguage as any, languages, fallbackLanguages, strictFallback, skipLanguages);
: getClosestLanguage(i18nLanguage, languages, fallbackLanguages, strictFallback, skipLanguages);

if (!inputText) {
return defaultText;
Expand Down

0 comments on commit bf52b33

Please sign in to comment.