Support device locale on app startup

This commit is contained in:
Eric Bailey
2024-09-17 09:09:38 -05:00
parent b74263e93d
commit 9d726d2b1e
3 changed files with 25 additions and 3 deletions
+3 -1
View File
@@ -41,11 +41,13 @@ export function getLocales() {
return output return output
} }
export const deviceLocales = getLocales()
/** /**
* BCP-47 language tag without region e.g. array of 2-char lang codes * BCP-47 language tag without region e.g. array of 2-char lang codes
* *
* {@link https://docs.expo.dev/versions/latest/sdk/localization/#locale} * {@link https://docs.expo.dev/versions/latest/sdk/localization/#locale}
*/ */
export const deviceLanguageCodes = dedupArray( export const deviceLanguageCodes = dedupArray(
getLocales().map(l => l.languageCode), deviceLocales.map(l => l.languageCode),
) )
+16
View File
@@ -181,3 +181,19 @@ export function fixLegacyLanguageCode(code: string | null): string | null {
} }
return code 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
}
+6 -2
View File
@@ -1,6 +1,7 @@
import {z} from 'zod' 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 {logger} from '#/logger'
import {PlatformInfo} from '../../../modules/expo-bluesky-swiss-army' import {PlatformInfo} from '../../../modules/expo-bluesky-swiss-army'
@@ -131,7 +132,10 @@ export const defaults: Schema = {
postLanguageHistory: (deviceLanguageCodes || []) postLanguageHistory: (deviceLanguageCodes || [])
.concat(['en', 'ja', 'pt', 'de']) .concat(['en', 'ja', 'pt', 'de'])
.slice(0, 6), .slice(0, 6),
appLanguage: deviceLanguageCodes[0] || 'en', appLanguage: findSupportedAppLanguage([
deviceLocales[0].languageTag,
deviceLanguageCodes[0],
]),
}, },
requireAltTextEnabled: false, requireAltTextEnabled: false,
largeAltBadgeEnabled: false, largeAltBadgeEnabled: false,