about summary refs log tree commit diff
path: root/src/locale/deviceLocales.ts
blob: b72daf68dce83907ce984941d7d19bea1e112884 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import {getLocales as defaultGetLocales, Locale} from 'expo-localization'

import {dedupArray} from '#/lib/functions'

type LocalWithLanguageCode = Locale & {
  languageCode: string
}

/**
 * Normalized locales
 *
 * Handles legacy migration for Java devices.
 *
 * {@link https://github.com/bluesky-social/social-app/pull/4461}
 * {@link https://xml.coverpages.org/iso639a.html}
 *
 * Convert Chinese language tags for Native.
 *
 * {@link https://datatracker.ietf.org/doc/html/rfc5646#appendix-A}
 * {@link https://developer.apple.com/documentation/packagedescription/languagetag}
 * {@link https://gist.github.com/amake/0ac7724681ac1c178c6f95a5b09f03ce#new-locales-vs-old-locales-chinese}
 */
export function getLocales() {
  const locales = defaultGetLocales?.() ?? []
  const output: LocalWithLanguageCode[] = []

  for (const locale of locales) {
    if (typeof locale.languageCode === 'string') {
      if (locale.languageCode === 'in') {
        // indonesian
        locale.languageCode = 'id'
      }
      if (locale.languageCode === 'iw') {
        // hebrew
        locale.languageCode = 'he'
      }
      if (locale.languageCode === 'ji') {
        // yiddish
        locale.languageCode = 'yi'
      }
    }

    if (typeof locale.languageTag === 'string') {
      if (
        locale.languageTag.startsWith('zh-Hans') ||
        locale.languageTag === 'zh-CN'
      ) {
        // Simplified Chinese to zh-Hans-CN
        locale.languageTag = 'zh-Hans-CN'
      }
      if (
        locale.languageTag.startsWith('zh-Hant') ||
        locale.languageTag === 'zh-TW'
      ) {
        // Traditional Chinese to zh-Hant-TW
        locale.languageTag = 'zh-Hant-TW'
      }
    }

    // @ts-ignore checked above
    output.push(locale)
  }

  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(
  deviceLocales.map(l => l.languageCode),
)