Skip to content

Commit

Permalink
Merge pull request #3224 from Bilb/fix-invalid-locales-fallback-en
Browse files Browse the repository at this point in the history
fix: fallback to en if supportedLocalesOf throws
  • Loading branch information
Bilb authored Sep 26, 2024
2 parents a92dbfe + 2a18535 commit e0d971b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "session-desktop",
"productName": "Session",
"description": "Private messaging from your desktop",
"version": "1.14.0",
"version": "1.14.1",
"license": "GPL-3.0",
"author": {
"name": "Oxen Labs",
Expand Down
40 changes: 31 additions & 9 deletions ts/util/i18n/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,38 @@ export function getBrowserLocale() {
// supportedLocalesOf will throw if the locales has a '_' instead of a '-' in it.
const userLocaleDashed = browserLocale.replaceAll('_', '-');

const matchingLocales = Intl.DateTimeFormat.supportedLocalesOf(userLocaleDashed);

const mappingTo = matchingLocales?.[0] || 'en';

if (!mappedBrowserLocaleDisplayed) {
mappedBrowserLocaleDisplayed = true;
i18nLog(`userLocaleDashed: '${userLocaleDashed}', mapping to browser locale: ${mappingTo}`);
try {
let matchingLocales: Array<string> = [];
try {
matchingLocales = Intl.DateTimeFormat.supportedLocalesOf(userLocaleDashed);
} catch (innerError) {
// some users have a locale setup with a ':' in it.
// see https://github.com/oxen-io/session-desktop/issues/3221
const semiColonIndex = userLocaleDashed.indexOf(':');
if (semiColonIndex > -1) {
matchingLocales = Intl.DateTimeFormat.supportedLocalesOf(
userLocaleDashed.substring(0, semiColonIndex)
);
}
}

const mappingTo = matchingLocales?.[0] || 'en';

if (!mappedBrowserLocaleDisplayed) {
mappedBrowserLocaleDisplayed = true;
i18nLog(`userLocaleDashed: '${userLocaleDashed}', mapping to browser locale: ${mappingTo}`);
}

return mappingTo;
} catch (e) {
if (!mappedBrowserLocaleDisplayed) {
mappedBrowserLocaleDisplayed = true;
i18nLog(
`userLocaleDashed: '${userLocaleDashed}' was an invalid locale for supportedLocalesOf(). Falling back to 'en'. Error:${e.message} `
);
}
return 'en';
}

return mappingTo;
}

export function setInitialLocale(crowdinLocaleArg: CrowdinLocale, dictionary: LocalizerDictionary) {
Expand Down

0 comments on commit e0d971b

Please sign in to comment.