about summary refs log tree commit diff
path: root/src/locale/helpers.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/locale/helpers.ts')
-rw-r--r--src/locale/helpers.ts33
1 files changed, 28 insertions, 5 deletions
diff --git a/src/locale/helpers.ts b/src/locale/helpers.ts
index 4d01459ab..d80cb5032 100644
--- a/src/locale/helpers.ts
+++ b/src/locale/helpers.ts
@@ -2,7 +2,11 @@ import {AppBskyFeedDefs, AppBskyFeedPost} from '@atproto/api'
 import lande from 'lande'
 import {hasProp} from 'lib/type-guards'
 import * as bcp47Match from 'bcp-47-match'
-import {LANGUAGES_MAP_CODE2, LANGUAGES_MAP_CODE3} from './languages'
+import {
+  AppLanguage,
+  LANGUAGES_MAP_CODE2,
+  LANGUAGES_MAP_CODE3,
+} from './languages'
 
 export function code2ToCode3(lang: string): string {
   if (lang.length === 2) {
@@ -85,14 +89,33 @@ export function getTranslatorLink(text: string, lang: string): string {
   )}`
 }
 
-export function sanitizeAppLanguageSetting(appLanguage: string) {
+/**
+ * Returns a valid `appLanguage` value from an arbitrary string.
+ *
+ * Contenxt: post-refactor, we populated some user's `appLanguage` setting with
+ * `postLanguage`, which can be a comma-separated list of values. This breaks
+ * `appLanguage` handling in the app, so we introduced this util to parse out a
+ * valid `appLanguage` from the pre-populated `postLanguage` values.
+ *
+ * The `appLanguage` will continue to be incorrect until the user returns to
+ * language settings and selects a new option, at which point we'll re-save
+ * their choice, which should then be a valid option. Since we don't know when
+ * this will happen, we should leave this here until we feel it's safe to
+ * remove, or we re-migrate their storage.
+ */
+export function sanitizeAppLanguageSetting(appLanguage: string): AppLanguage {
   const langs = appLanguage.split(',').filter(Boolean)
 
   for (const lang of langs) {
-    if (['en', 'hi'].includes(lang)) {
-      return lang
+    switch (lang) {
+      case 'en':
+        return AppLanguage.en
+      case 'hi':
+        return AppLanguage.hi
+      default:
+        continue
     }
   }
 
-  return 'en'
+  return AppLanguage.en
 }