Prevent invalid locale from being loaded (#2099)

This commit is contained in:
Eric Bailey 2023-12-05 19:51:50 -06:00 committed by GitHub
parent fab9f839d0
commit a915a57b10
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 14 deletions

View file

@ -1,8 +1,9 @@
import {useLanguagePrefs} from '#/state/preferences'
import {i18n} from '@lingui/core'
import {useEffect} from 'react' import {useEffect} from 'react'
import {messages as messagesEn} from './locales/en/messages' import {i18n} from '@lingui/core'
import {messages as messagesHi} from './locales/hi/messages'
import {useLanguagePrefs} from '#/state/preferences'
import {messages as messagesEn} from '#/locale/locales/en/messages'
import {messages as messagesHi} from '#/locale/locales/hi/messages'
export const locales = { export const locales = {
en: 'English', en: 'English',
@ -18,15 +19,10 @@ export const defaultLocale = 'en'
* @param locale any locale string * @param locale any locale string
*/ */
export async function dynamicActivate(locale: string) { export async function dynamicActivate(locale: string) {
if (locale === 'en') { if (locale === 'hi') {
i18n.loadAndActivate({locale, messages: messagesEn})
return
} else if (locale === 'hi') {
i18n.loadAndActivate({locale, messages: messagesHi}) i18n.loadAndActivate({locale, messages: messagesHi})
return
} else { } else {
i18n.loadAndActivate({locale, messages: messagesEn}) i18n.loadAndActivate({locale, messages: messagesEn})
return
} }
} }

View file

@ -1,6 +1,7 @@
import {useLanguagePrefs} from '#/state/preferences'
import {i18n} from '@lingui/core'
import {useEffect} from 'react' import {useEffect} from 'react'
import {i18n} from '@lingui/core'
import {useLanguagePrefs} from '#/state/preferences'
export const locales = { export const locales = {
en: 'English', en: 'English',
@ -16,8 +17,15 @@ export const defaultLocale = 'en'
* @param locale any locale string * @param locale any locale string
*/ */
export async function dynamicActivate(locale: string) { export async function dynamicActivate(locale: string) {
const {messages} = await import(`./locales/${locale}/messages`) let mod: any
i18n.load(locale, messages)
if (locale === 'hi') {
mod = await import(`./locales/hi/messages`)
} else {
mod = await import(`./locales/en/messages`)
}
i18n.load(locale, mod.messages)
i18n.activate(locale) i18n.activate(locale)
} }

View file

@ -48,6 +48,7 @@ export function LanguageSettingsScreen(_props: Props) {
const onChangePrimaryLanguage = React.useCallback( const onChangePrimaryLanguage = React.useCallback(
(value: Parameters<PickerSelectProps['onValueChange']>[0]) => { (value: Parameters<PickerSelectProps['onValueChange']>[0]) => {
if (!value) return
if (langPrefs.primaryLanguage !== value) { if (langPrefs.primaryLanguage !== value) {
setLangPrefs.setPrimaryLanguage(value) setLangPrefs.setPrimaryLanguage(value)
} }
@ -57,6 +58,7 @@ export function LanguageSettingsScreen(_props: Props) {
const onChangeAppLanguage = React.useCallback( const onChangeAppLanguage = React.useCallback(
(value: Parameters<PickerSelectProps['onValueChange']>[0]) => { (value: Parameters<PickerSelectProps['onValueChange']>[0]) => {
if (!value) return
if (langPrefs.appLanguage !== value) { if (langPrefs.appLanguage !== value) {
setLangPrefs.setAppLanguage(value) setLangPrefs.setAppLanguage(value)
} }
@ -100,6 +102,7 @@ export function LanguageSettingsScreen(_props: Props) {
<View style={{position: 'relative'}}> <View style={{position: 'relative'}}>
<RNPickerSelect <RNPickerSelect
placeholder={{}}
value={langPrefs.appLanguage} value={langPrefs.appLanguage}
onValueChange={onChangeAppLanguage} onValueChange={onChangeAppLanguage}
items={APP_LANGUAGES.filter(l => Boolean(l.code2)).map(l => ({ items={APP_LANGUAGES.filter(l => Boolean(l.code2)).map(l => ({
@ -190,6 +193,7 @@ export function LanguageSettingsScreen(_props: Props) {
<View style={{position: 'relative'}}> <View style={{position: 'relative'}}>
<RNPickerSelect <RNPickerSelect
placeholder={{}}
value={langPrefs.primaryLanguage} value={langPrefs.primaryLanguage}
onValueChange={onChangePrimaryLanguage} onValueChange={onChangePrimaryLanguage}
items={LANGUAGES.filter(l => Boolean(l.code2)).map(l => ({ items={LANGUAGES.filter(l => Boolean(l.code2)).map(l => ({