Refactor
This commit is contained in:
@@ -0,0 +1,51 @@
|
|||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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(
|
||||||
|
getLocales().map(l => l.languageCode),
|
||||||
|
)
|
||||||
@@ -160,8 +160,13 @@ export function sanitizeAppLanguageSetting(appLanguage: string): AppLanguage {
|
|||||||
return AppLanguage.en
|
return AppLanguage.en
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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 fixLegacyLanguageCode(code: string | null): string | null {
|
export function fixLegacyLanguageCode(code: string | null): string | null {
|
||||||
// handle some legacy code conversions, see https://xml.coverpages.org/iso639a.html
|
|
||||||
if (code === 'in') {
|
if (code === 'in') {
|
||||||
// indonesian
|
// indonesian
|
||||||
return 'id'
|
return 'id'
|
||||||
|
|||||||
@@ -1,8 +1,4 @@
|
|||||||
import {Platform} from 'react-native'
|
import {Platform} from 'react-native'
|
||||||
import {getLocales} from 'expo-localization'
|
|
||||||
|
|
||||||
import {fixLegacyLanguageCode} from '#/locale/helpers'
|
|
||||||
import {dedupArray} from 'lib/functions'
|
|
||||||
|
|
||||||
export const isIOS = Platform.OS === 'ios'
|
export const isIOS = Platform.OS === 'ios'
|
||||||
export const isAndroid = Platform.OS === 'android'
|
export const isAndroid = Platform.OS === 'android'
|
||||||
@@ -15,9 +11,3 @@ export const isMobileWeb =
|
|||||||
// @ts-ignore we know window exists -prf
|
// @ts-ignore we know window exists -prf
|
||||||
global.window.matchMedia(isMobileWebMediaQuery)?.matches
|
global.window.matchMedia(isMobileWebMediaQuery)?.matches
|
||||||
export const isIPhoneWeb = isWeb && /iPhone/.test(navigator.userAgent)
|
export const isIPhoneWeb = isWeb && /iPhone/.test(navigator.userAgent)
|
||||||
|
|
||||||
export const deviceLocales = dedupArray(
|
|
||||||
getLocales?.()
|
|
||||||
.map?.(locale => fixLegacyLanguageCode(locale.languageCode))
|
|
||||||
.filter(code => typeof code === 'string'),
|
|
||||||
) as string[]
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import {z} from 'zod'
|
import {z} from 'zod'
|
||||||
|
|
||||||
|
import {deviceLanguageCodes} from '#/locale/deviceLocales'
|
||||||
import {logger} from '#/logger'
|
import {logger} from '#/logger'
|
||||||
import {deviceLocales} from '#/platform/detection'
|
|
||||||
import {PlatformInfo} from '../../../modules/expo-bluesky-swiss-army'
|
import {PlatformInfo} from '../../../modules/expo-bluesky-swiss-army'
|
||||||
|
|
||||||
const externalEmbedOptions = ['show', 'hide'] as const
|
const externalEmbedOptions = ['show', 'hide'] as const
|
||||||
@@ -125,13 +125,13 @@ export const defaults: Schema = {
|
|||||||
lastEmailConfirm: undefined,
|
lastEmailConfirm: undefined,
|
||||||
},
|
},
|
||||||
languagePrefs: {
|
languagePrefs: {
|
||||||
primaryLanguage: deviceLocales[0] || 'en',
|
primaryLanguage: deviceLanguageCodes[0] || 'en',
|
||||||
contentLanguages: deviceLocales || [],
|
contentLanguages: deviceLanguageCodes || [],
|
||||||
postLanguage: deviceLocales[0] || 'en',
|
postLanguage: deviceLanguageCodes[0] || 'en',
|
||||||
postLanguageHistory: (deviceLocales || [])
|
postLanguageHistory: (deviceLanguageCodes || [])
|
||||||
.concat(['en', 'ja', 'pt', 'de'])
|
.concat(['en', 'ja', 'pt', 'de'])
|
||||||
.slice(0, 6),
|
.slice(0, 6),
|
||||||
appLanguage: deviceLocales[0] || 'en',
|
appLanguage: deviceLanguageCodes[0] || 'en',
|
||||||
},
|
},
|
||||||
requireAltTextEnabled: false,
|
requireAltTextEnabled: false,
|
||||||
largeAltBadgeEnabled: false,
|
largeAltBadgeEnabled: false,
|
||||||
|
|||||||
@@ -1,19 +1,20 @@
|
|||||||
import React from 'react'
|
import React from 'react'
|
||||||
import {StyleSheet, View} from 'react-native'
|
import {StyleSheet, View} from 'react-native'
|
||||||
import {ScrollView} from '../util'
|
|
||||||
import {Text} from '../../util/text/Text'
|
|
||||||
import {usePalette} from 'lib/hooks/usePalette'
|
|
||||||
import {useWebMediaQueries} from 'lib/hooks/useWebMediaQueries'
|
|
||||||
import {deviceLocales} from 'platform/detection'
|
|
||||||
import {LANGUAGES, LANGUAGES_MAP_CODE2} from '../../../../locale/languages'
|
|
||||||
import {LanguageToggle} from './LanguageToggle'
|
|
||||||
import {ConfirmLanguagesButton} from './ConfirmLanguagesButton'
|
|
||||||
import {Trans} from '@lingui/macro'
|
import {Trans} from '@lingui/macro'
|
||||||
|
|
||||||
|
import {deviceLanguageCodes} from '#/locale/deviceLocales'
|
||||||
import {useModalControls} from '#/state/modals'
|
import {useModalControls} from '#/state/modals'
|
||||||
import {
|
import {
|
||||||
useLanguagePrefs,
|
useLanguagePrefs,
|
||||||
useLanguagePrefsApi,
|
useLanguagePrefsApi,
|
||||||
} from '#/state/preferences/languages'
|
} from '#/state/preferences/languages'
|
||||||
|
import {usePalette} from 'lib/hooks/usePalette'
|
||||||
|
import {useWebMediaQueries} from 'lib/hooks/useWebMediaQueries'
|
||||||
|
import {LANGUAGES, LANGUAGES_MAP_CODE2} from '../../../../locale/languages'
|
||||||
|
import {Text} from '../../util/text/Text'
|
||||||
|
import {ScrollView} from '../util'
|
||||||
|
import {ConfirmLanguagesButton} from './ConfirmLanguagesButton'
|
||||||
|
import {LanguageToggle} from './LanguageToggle'
|
||||||
|
|
||||||
export const snapPoints = ['100%']
|
export const snapPoints = ['100%']
|
||||||
|
|
||||||
@@ -37,10 +38,10 @@ export function Component({}: {}) {
|
|||||||
langs.sort((a, b) => {
|
langs.sort((a, b) => {
|
||||||
const hasA =
|
const hasA =
|
||||||
langPrefs.contentLanguages.includes(a.code2) ||
|
langPrefs.contentLanguages.includes(a.code2) ||
|
||||||
deviceLocales.includes(a.code2)
|
deviceLanguageCodes.includes(a.code2)
|
||||||
const hasB =
|
const hasB =
|
||||||
langPrefs.contentLanguages.includes(b.code2) ||
|
langPrefs.contentLanguages.includes(b.code2) ||
|
||||||
deviceLocales.includes(b.code2)
|
deviceLanguageCodes.includes(b.code2)
|
||||||
if (hasA === hasB) return a.name.localeCompare(b.name)
|
if (hasA === hasB) return a.name.localeCompare(b.name)
|
||||||
if (hasA) return -1
|
if (hasA) return -1
|
||||||
return 1
|
return 1
|
||||||
|
|||||||
@@ -1,20 +1,21 @@
|
|||||||
import React from 'react'
|
import React from 'react'
|
||||||
import {StyleSheet, View} from 'react-native'
|
import {StyleSheet, View} from 'react-native'
|
||||||
import {ScrollView} from '../util'
|
|
||||||
import {Text} from '../../util/text/Text'
|
|
||||||
import {usePalette} from 'lib/hooks/usePalette'
|
|
||||||
import {useWebMediaQueries} from 'lib/hooks/useWebMediaQueries'
|
|
||||||
import {deviceLocales} from 'platform/detection'
|
|
||||||
import {LANGUAGES, LANGUAGES_MAP_CODE2} from '../../../../locale/languages'
|
|
||||||
import {ConfirmLanguagesButton} from './ConfirmLanguagesButton'
|
|
||||||
import {ToggleButton} from 'view/com/util/forms/ToggleButton'
|
|
||||||
import {Trans} from '@lingui/macro'
|
import {Trans} from '@lingui/macro'
|
||||||
|
|
||||||
|
import {deviceLanguageCodes} from '#/locale/deviceLocales'
|
||||||
import {useModalControls} from '#/state/modals'
|
import {useModalControls} from '#/state/modals'
|
||||||
import {
|
import {
|
||||||
|
hasPostLanguage,
|
||||||
useLanguagePrefs,
|
useLanguagePrefs,
|
||||||
useLanguagePrefsApi,
|
useLanguagePrefsApi,
|
||||||
hasPostLanguage,
|
|
||||||
} from '#/state/preferences/languages'
|
} from '#/state/preferences/languages'
|
||||||
|
import {usePalette} from 'lib/hooks/usePalette'
|
||||||
|
import {useWebMediaQueries} from 'lib/hooks/useWebMediaQueries'
|
||||||
|
import {ToggleButton} from 'view/com/util/forms/ToggleButton'
|
||||||
|
import {LANGUAGES, LANGUAGES_MAP_CODE2} from '../../../../locale/languages'
|
||||||
|
import {Text} from '../../util/text/Text'
|
||||||
|
import {ScrollView} from '../util'
|
||||||
|
import {ConfirmLanguagesButton} from './ConfirmLanguagesButton'
|
||||||
|
|
||||||
export const snapPoints = ['100%']
|
export const snapPoints = ['100%']
|
||||||
|
|
||||||
@@ -38,10 +39,10 @@ export function Component() {
|
|||||||
langs.sort((a, b) => {
|
langs.sort((a, b) => {
|
||||||
const hasA =
|
const hasA =
|
||||||
hasPostLanguage(langPrefs.postLanguage, a.code2) ||
|
hasPostLanguage(langPrefs.postLanguage, a.code2) ||
|
||||||
deviceLocales.includes(a.code2)
|
deviceLanguageCodes.includes(a.code2)
|
||||||
const hasB =
|
const hasB =
|
||||||
hasPostLanguage(langPrefs.postLanguage, b.code2) ||
|
hasPostLanguage(langPrefs.postLanguage, b.code2) ||
|
||||||
deviceLocales.includes(b.code2)
|
deviceLanguageCodes.includes(b.code2)
|
||||||
if (hasA === hasB) return a.name.localeCompare(b.name)
|
if (hasA === hasB) return a.name.localeCompare(b.name)
|
||||||
if (hasA) return -1
|
if (hasA) return -1
|
||||||
return 1
|
return 1
|
||||||
|
|||||||
Reference in New Issue
Block a user