Skip to content

Commit

Permalink
Fixed setLocale() in i18n.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
vthorsteinsson committed Feb 12, 2024
1 parent 27f6c41 commit c7bb16d
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions static/src/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,26 @@ const rex = /{\s*(\w+)\s*}/g;
let messages: FlattenedMessages = {};
let messagesLoaded = false;

function hasAnyTranslation(msgs: Messages, locale: string): boolean {
// Return true if any translation is available for the given locale
for (let key in msgs) {
if (msgs[key][locale] !== undefined)
return true;
}
return false;
}

function setLocale(locale: string, msgs: Messages): void {
// Set the current i18n locale and fallback
currentLocale = locale;
currentFallback = locale.split("_")[0];
// For unsupported locales, fall back to English (U.S.)
if (msgs[currentLocale] === undefined && msgs[currentFallback] === undefined) {
// For unsupported locales, i.e. locales that have no
// translations available for them, fall back to English (U.S.).
if (!hasAnyTranslation(msgs, currentLocale) && !hasAnyTranslation(msgs, currentFallback)) {
currentLocale = "en_US";
currentFallback = "en";
}

// Flatten the Messages structure, enabling long strings
// to be represented as string arrays in the messages.json file
messages = {};
Expand Down

0 comments on commit c7bb16d

Please sign in to comment.