my fork of the bluesky client
1import {getLocales as defaultGetLocales, Locale} from 'expo-localization'
2
3import {dedupArray} from '#/lib/functions'
4
5type LocalWithLanguageCode = Locale & {
6 languageCode: string
7}
8
9/**
10 * Normalized locales
11 *
12 * Handles legacy migration for Java devices.
13 *
14 * {@link https://github.com/bluesky-social/social-app/pull/4461}
15 * {@link https://xml.coverpages.org/iso639a.html}
16 *
17 * Convert Chinese language tags for Native.
18 *
19 * {@link https://datatracker.ietf.org/doc/html/rfc5646#appendix-A}
20 * {@link https://developer.apple.com/documentation/packagedescription/languagetag}
21 * {@link https://gist.github.com/amake/0ac7724681ac1c178c6f95a5b09f03ce#new-locales-vs-old-locales-chinese}
22 */
23export function getLocales() {
24 const locales = defaultGetLocales?.() ?? []
25 const output: LocalWithLanguageCode[] = []
26
27 for (const locale of locales) {
28 if (typeof locale.languageCode === 'string') {
29 if (locale.languageCode === 'in') {
30 // indonesian
31 locale.languageCode = 'id'
32 }
33 if (locale.languageCode === 'iw') {
34 // hebrew
35 locale.languageCode = 'he'
36 }
37 if (locale.languageCode === 'ji') {
38 // yiddish
39 locale.languageCode = 'yi'
40 }
41 }
42
43 if (typeof locale.languageTag === 'string') {
44 if (locale.languageTag.startsWith('zh-Hans')) {
45 // Simplified Chinese to zh-CN
46 locale.languageTag = 'zh-CN'
47 }
48 if (locale.languageTag.startsWith('zh-Hant')) {
49 // Traditional Chinese to zh-TW
50 locale.languageTag = 'zh-TW'
51 }
52 if (locale.languageTag.startsWith('yue')) {
53 // Cantonese (Yue) to zh-HK
54 locale.languageTag = 'zh-HK'
55 }
56 }
57
58 // @ts-ignore checked above
59 output.push(locale)
60 }
61
62 return output
63}
64
65export const deviceLocales = getLocales()
66
67/**
68 * BCP-47 language tag without region e.g. array of 2-char lang codes
69 *
70 * {@link https://docs.expo.dev/versions/latest/sdk/localization/#locale}
71 */
72export const deviceLanguageCodes = dedupArray(
73 deviceLocales.map(l => l.languageCode),
74)