Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(app): do not crash when you get a "C" locale #16716

Merged
merged 4 commits into from
Nov 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ import { getSystemLanguage } from '/app/redux/shell'
import type { DropdownOption } from '@opentrons/components'
import type { Dispatch } from '/app/redux/types'

type ArrayElement<
ArrayType extends readonly unknown[]
> = ArrayType extends ReadonlyArray<infer ElementType> ? ElementType : never

export function SystemLanguagePreferenceModal(): JSX.Element | null {
const { i18n, t } = useTranslation(['app_settings', 'shared', 'branded'])
const enableLocalization = useFeatureFlag('enableLocalization')
Expand Down Expand Up @@ -83,13 +87,31 @@ 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: () => ArrayElement<
typeof LANGUAGES
> | null = () => {
try {
return (
LANGUAGES.find(lng => lng.value === systemLanguage) ??
LANGUAGES.find(
lng =>
new Intl.Locale(lng.value).language ===
new Intl.Locale(systemLanguage).language
) ??
null
)
} 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)
Expand Down
Loading