···11+import {test, expect} from '@jest/globals'
22+33+import {sanitizeAppLanguageSetting} from '#/locale/helpers'
44+import {AppLanguage} from '#/locale/languages'
55+66+test('sanitizeAppLanguageSetting', () => {
77+ expect(sanitizeAppLanguageSetting('en')).toBe(AppLanguage.en)
88+ expect(sanitizeAppLanguageSetting('hi')).toBe(AppLanguage.hi)
99+ expect(sanitizeAppLanguageSetting('foo')).toBe(AppLanguage.en)
1010+ expect(sanitizeAppLanguageSetting('en,fr')).toBe(AppLanguage.en)
1111+ expect(sanitizeAppLanguageSetting('fr,en')).toBe(AppLanguage.en)
1212+})
+28-5
src/locale/helpers.ts
···22import lande from 'lande'
33import {hasProp} from 'lib/type-guards'
44import * as bcp47Match from 'bcp-47-match'
55-import {LANGUAGES_MAP_CODE2, LANGUAGES_MAP_CODE3} from './languages'
55+import {
66+ AppLanguage,
77+ LANGUAGES_MAP_CODE2,
88+ LANGUAGES_MAP_CODE3,
99+} from './languages'
610711export function code2ToCode3(lang: string): string {
812 if (lang.length === 2) {
···8589 )}`
8690}
87918888-export function sanitizeAppLanguageSetting(appLanguage: string) {
9292+/**
9393+ * Returns a valid `appLanguage` value from an arbitrary string.
9494+ *
9595+ * Contenxt: post-refactor, we populated some user's `appLanguage` setting with
9696+ * `postLanguage`, which can be a comma-separated list of values. This breaks
9797+ * `appLanguage` handling in the app, so we introduced this util to parse out a
9898+ * valid `appLanguage` from the pre-populated `postLanguage` values.
9999+ *
100100+ * The `appLanguage` will continue to be incorrect until the user returns to
101101+ * language settings and selects a new option, at which point we'll re-save
102102+ * their choice, which should then be a valid option. Since we don't know when
103103+ * this will happen, we should leave this here until we feel it's safe to
104104+ * remove, or we re-migrate their storage.
105105+ */
106106+export function sanitizeAppLanguageSetting(appLanguage: string): AppLanguage {
89107 const langs = appLanguage.split(',').filter(Boolean)
9010891109 for (const lang of langs) {
9292- if (['en', 'hi'].includes(lang)) {
9393- return lang
110110+ switch (lang) {
111111+ case 'en':
112112+ return AppLanguage.en
113113+ case 'hi':
114114+ return AppLanguage.hi
115115+ default:
116116+ continue
94117 }
95118 }
961199797- return 'en'
120120+ return AppLanguage.en
98121}
+11-12
src/locale/i18n.ts
···55import {messages as messagesEn} from '#/locale/locales/en/messages'
66import {messages as messagesHi} from '#/locale/locales/hi/messages'
77import {sanitizeAppLanguageSetting} from '#/locale/helpers'
88-99-export const locales = {
1010- en: 'English',
1111- hi: 'हिंदी',
1212-}
1313-export const defaultLocale = 'en'
88+import {AppLanguage} from '#/locale/languages'
1491510/**
1611 * We do a dynamic import of just the catalog that we need
1717- * @param locale any locale string
1812 */
1919-export async function dynamicActivate(locale: string) {
2020- if (locale === 'hi') {
2121- i18n.loadAndActivate({locale, messages: messagesHi})
2222- } else {
2323- i18n.loadAndActivate({locale, messages: messagesEn})
1313+export async function dynamicActivate(locale: AppLanguage) {
1414+ switch (locale) {
1515+ case AppLanguage.hi: {
1616+ i18n.loadAndActivate({locale, messages: messagesHi})
1717+ break
1818+ }
1919+ default: {
2020+ i18n.loadAndActivate({locale, messages: messagesEn})
2121+ break
2222+ }
2423 }
2524}
2625
+11-12
src/locale/i18n.web.ts
···3344import {useLanguagePrefs} from '#/state/preferences'
55import {sanitizeAppLanguageSetting} from '#/locale/helpers'
66-77-export const locales = {
88- en: 'English',
99- hi: 'हिंदी',
1010-}
1111-export const defaultLocale = 'en'
66+import {AppLanguage} from '#/locale/languages'
127138/**
149 * We do a dynamic import of just the catalog that we need
1515- * @param locale any locale string
1610 */
1717-export async function dynamicActivate(locale: string) {
1111+export async function dynamicActivate(locale: AppLanguage) {
1812 let mod: any
19132020- if (locale === 'hi') {
2121- mod = await import(`./locales/hi/messages`)
2222- } else {
2323- mod = await import(`./locales/en/messages`)
1414+ switch (locale) {
1515+ case AppLanguage.hi: {
1616+ mod = await import(`./locales/hi/messages`)
1717+ break
1818+ }
1919+ default: {
2020+ mod = await import(`./locales/en/messages`)
2121+ break
2222+ }
2423 }
25242625 i18n.load(locale, mod.messages)