forked from
jollywhoppers.com/witchsky.app
Bluesky app fork with some witchin' additions 馃挮
1import {getLocales as defaultGetLocales, type 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 (
45 locale.languageTag.startsWith('zh-Hans') ||
46 locale.languageTag === 'zh-CN'
47 ) {
48 // Simplified Chinese to zh-Hans-CN
49 locale.languageTag = 'zh-Hans-CN'
50 }
51 if (
52 locale.languageTag.startsWith('zh-Hant') ||
53 locale.languageTag === 'zh-TW'
54 ) {
55 // Traditional Chinese to zh-Hant-TW
56 locale.languageTag = 'zh-Hant-TW'
57 }
58 }
59
60 // @ts-ignore checked above
61 output.push(locale)
62 }
63
64 return output
65}
66
67export const deviceLocales = getLocales()
68
69/**
70 * BCP-47 language tag without region e.g. array of 2-char lang codes
71 *
72 * {@link https://docs.expo.dev/versions/latest/sdk/localization/#locale}
73 */
74export const deviceLanguageCodes = dedupArray(
75 deviceLocales.map(l => l.languageCode),
76)