Support device locale on app startup
This commit is contained in:
@@ -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),
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -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
|
||||||
|
}
|
||||||
|
|||||||
@@ -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,
|
||||||
|
|||||||
Reference in New Issue
Block a user