diff --git a/src/locale/deviceLocales.ts b/src/locale/deviceLocales.ts index 586553b66a..9e19e372b8 100644 --- a/src/locale/deviceLocales.ts +++ b/src/locale/deviceLocales.ts @@ -41,11 +41,13 @@ export function getLocales() { return output } +export const deviceLocales = getLocales() + /** * BCP-47 language tag without region e.g. array of 2-char lang codes * * {@link https://docs.expo.dev/versions/latest/sdk/localization/#locale} */ export const deviceLanguageCodes = dedupArray( - getLocales().map(l => l.languageCode), + deviceLocales.map(l => l.languageCode), ) diff --git a/src/locale/helpers.ts b/src/locale/helpers.ts index 507e1f98b7..de84c98dd8 100644 --- a/src/locale/helpers.ts +++ b/src/locale/helpers.ts @@ -181,3 +181,19 @@ export function fixLegacyLanguageCode(code: string | null): string | null { } return code } + +/** + * Find the first language supported by our translation infra. Values should be + * in order of preference, and match the values of {@link AppLanguage}. + * + * If no match, returns `en`. + */ +export function findSupportedAppLanguage(languageTags: string[]) { + const supported = new Set(Object.values(AppLanguage)) + for (const tag of languageTags) { + if (supported.has(tag as AppLanguage)) { + return tag + } + } + return AppLanguage.en +} diff --git a/src/state/persisted/schema.ts b/src/state/persisted/schema.ts index 8189f72b86..5daad25416 100644 --- a/src/state/persisted/schema.ts +++ b/src/state/persisted/schema.ts @@ -1,6 +1,7 @@ import {z} from 'zod' -import {deviceLanguageCodes} from '#/locale/deviceLocales' +import {deviceLanguageCodes, deviceLocales} from '#/locale/deviceLocales' +import {findSupportedAppLanguage} from '#/locale/helpers' import {logger} from '#/logger' import {PlatformInfo} from '../../../modules/expo-bluesky-swiss-army' @@ -131,7 +132,10 @@ export const defaults: Schema = { postLanguageHistory: (deviceLanguageCodes || []) .concat(['en', 'ja', 'pt', 'de']) .slice(0, 6), - appLanguage: deviceLanguageCodes[0] || 'en', + appLanguage: findSupportedAppLanguage([ + deviceLocales[0].languageTag, + deviceLanguageCodes[0], + ]), }, requireAltTextEnabled: false, largeAltBadgeEnabled: false,