Skip to content

Commit

Permalink
fix(app): do not crash when you get a "C" locale
Browse files Browse the repository at this point in the history
Some linux systems are configured with one of the POSIX default locales,
like "C" or "POSIX", and for some reason this will be returned by
electron's getSystemPreferredLanguages() api directly. Unfortunately,
passing this to the browser js standard Intl.Locale() will make it
throw, and that would whitescreen the app. Instead, catch the error and
treat it as an unmatched system language.
  • Loading branch information
sfoster1 committed Nov 6, 2024
1 parent 7ce9d4b commit 856cda6
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions app/src/organisms/Desktop/SystemLanguagePreferenceModal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,28 @@ export function SystemLanguagePreferenceModal(): JSX.Element | null {
useEffect(() => {
if (systemLanguage != null) {
// prefer match entire locale, then match just language e.g. zh-Hant and zh-CN
const matchedSystemLanguageOption =
LANGUAGES.find(lng => lng.value === systemLanguage) ??
LANGUAGES.find(
lng =>
new Intl.Locale(lng.value).language ===
new Intl.Locale(systemLanguage).language
)
const matchSystemLanguage: () => string | null = () => {

Check failure on line 86 in app/src/organisms/Desktop/SystemLanguagePreferenceModal/index.tsx

View workflow job for this annotation

GitHub Actions / js checks

Type '() => { name: string; value: Language; } | null | undefined' is not assignable to type '() => string | null'.

Check failure on line 86 in app/src/organisms/Desktop/SystemLanguagePreferenceModal/index.tsx

View workflow job for this annotation

GitHub Actions / js checks

Type '() => { name: string; value: Language; } | null | undefined' is not assignable to type '() => string | null'.
try {
return (
LANGUAGES.find(lng => lng.value === systemLanguage) ??
LANGUAGES.find(
lng =>
new Intl.Locale(lng.value).language ===
new Intl.Locale(systemLanguage).language
)
)
} catch (error: unknown) {
// Sometimes the language that we get from the shell will not be something
// js i18n can understand. Specifically, some linux systems will have their
// locale set to "C" (https://www.gnu.org/software/libc/manual/html_node/Standard-Locales.html)
// and that will cause Intl.Locale to throw. In this case, we'll treat it as
// unset and fall back to our default.
console.log(`Failed to search languages: ${error}`)
return null
}
}
const matchedSystemLanguageOption = matchSystemLanguage()

if (matchedSystemLanguageOption != null) {
// initial current option: set to detected system language
setCurrentOption(matchedSystemLanguageOption)

Check failure on line 110 in app/src/organisms/Desktop/SystemLanguagePreferenceModal/index.tsx

View workflow job for this annotation

GitHub Actions / js checks

Argument of type 'string' is not assignable to parameter of type 'SetStateAction<DropdownOption>'.

Check failure on line 110 in app/src/organisms/Desktop/SystemLanguagePreferenceModal/index.tsx

View workflow job for this annotation

GitHub Actions / js checks

Argument of type 'string' is not assignable to parameter of type 'SetStateAction<DropdownOption>'.
Expand Down

0 comments on commit 856cda6

Please sign in to comment.