about summary refs log tree commit diff
path: root/src/locale/deviceLocales.ts
diff options
context:
space:
mode:
authorEric Bailey <git@esb.lol>2024-09-20 10:50:33 -0500
committerGitHub <noreply@github.com>2024-09-20 10:50:33 -0500
commitfa6f6f9e473a0dd731ea95210fbd66e0b8c0c283 (patch)
tree1c5166f9b31d3b4967fcf8cd8cdb969d2efa92cb /src/locale/deviceLocales.ts
parentcd88cbeab83169410fff3245505b53122dfe28aa (diff)
downloadvoidsky-fa6f6f9e473a0dd731ea95210fbd66e0b8c0c283.tar.zst
Language fixes (#5384)
* Add some comments

* Decouple language settings

* Normalize on read/write

* Refactor

* Support device locale on app startup

* Cleanup, port to web

* Clean up comments

* Comment

* Try not to mutate

* Protect util handling, update test

* Dedupe array values
Diffstat (limited to 'src/locale/deviceLocales.ts')
-rw-r--r--src/locale/deviceLocales.ts53
1 files changed, 53 insertions, 0 deletions
diff --git a/src/locale/deviceLocales.ts b/src/locale/deviceLocales.ts
new file mode 100644
index 000000000..9e19e372b
--- /dev/null
+++ b/src/locale/deviceLocales.ts
@@ -0,0 +1,53 @@
+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}
+ */
+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'
+      }
+
+      // @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),
+)