Improve type safety
This commit is contained in:
+27
-5
@@ -2,7 +2,11 @@ import {AppBskyFeedDefs, AppBskyFeedPost} from '@atproto/api'
|
|||||||
import lande from 'lande'
|
import lande from 'lande'
|
||||||
import {hasProp} from 'lib/type-guards'
|
import {hasProp} from 'lib/type-guards'
|
||||||
import * as bcp47Match from 'bcp-47-match'
|
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 {
|
export function code2ToCode3(lang: string): string {
|
||||||
if (lang.length === 2) {
|
if (lang.length === 2) {
|
||||||
@@ -85,14 +89,32 @@ 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 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.
|
||||||
|
*/
|
||||||
|
export function sanitizeAppLanguageSetting(
|
||||||
|
appLanguage: AppLanguage | string,
|
||||||
|
): AppLanguage {
|
||||||
const langs = appLanguage.split(',').filter(Boolean)
|
const langs = appLanguage.split(',').filter(Boolean)
|
||||||
|
|
||||||
for (const lang of langs) {
|
for (const lang of langs) {
|
||||||
if (['en', 'hi'].includes(lang)) {
|
switch (lang) {
|
||||||
return lang
|
case 'en':
|
||||||
|
return AppLanguage.en
|
||||||
|
case 'hi':
|
||||||
|
return AppLanguage.hi
|
||||||
|
default:
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return 'en'
|
return AppLanguage.en
|
||||||
}
|
}
|
||||||
|
|||||||
+11
-12
@@ -5,22 +5,21 @@ import {useLanguagePrefs} from '#/state/preferences'
|
|||||||
import {messages as messagesEn} from '#/locale/locales/en/messages'
|
import {messages as messagesEn} from '#/locale/locales/en/messages'
|
||||||
import {messages as messagesHi} from '#/locale/locales/hi/messages'
|
import {messages as messagesHi} from '#/locale/locales/hi/messages'
|
||||||
import {sanitizeAppLanguageSetting} from '#/locale/helpers'
|
import {sanitizeAppLanguageSetting} from '#/locale/helpers'
|
||||||
|
import {AppLanguage} from '#/locale/languages'
|
||||||
export const locales = {
|
|
||||||
en: 'English',
|
|
||||||
hi: 'हिंदी',
|
|
||||||
}
|
|
||||||
export const defaultLocale = 'en'
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* We do a dynamic import of just the catalog that we need
|
* We do a dynamic import of just the catalog that we need
|
||||||
* @param locale any locale string
|
|
||||||
*/
|
*/
|
||||||
export async function dynamicActivate(locale: string) {
|
export async function dynamicActivate(locale: AppLanguage) {
|
||||||
if (locale === 'hi') {
|
switch (locale) {
|
||||||
i18n.loadAndActivate({locale, messages: messagesHi})
|
case AppLanguage.hi: {
|
||||||
} else {
|
i18n.loadAndActivate({locale, messages: messagesHi})
|
||||||
i18n.loadAndActivate({locale, messages: messagesEn})
|
break
|
||||||
|
}
|
||||||
|
default: {
|
||||||
|
i18n.loadAndActivate({locale, messages: messagesEn})
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+11
-12
@@ -3,24 +3,23 @@ import {i18n} from '@lingui/core'
|
|||||||
|
|
||||||
import {useLanguagePrefs} from '#/state/preferences'
|
import {useLanguagePrefs} from '#/state/preferences'
|
||||||
import {sanitizeAppLanguageSetting} from '#/locale/helpers'
|
import {sanitizeAppLanguageSetting} from '#/locale/helpers'
|
||||||
|
import {AppLanguage} from '#/locale/languages'
|
||||||
export const locales = {
|
|
||||||
en: 'English',
|
|
||||||
hi: 'हिंदी',
|
|
||||||
}
|
|
||||||
export const defaultLocale = 'en'
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* We do a dynamic import of just the catalog that we need
|
* We do a dynamic import of just the catalog that we need
|
||||||
* @param locale any locale string
|
|
||||||
*/
|
*/
|
||||||
export async function dynamicActivate(locale: string) {
|
export async function dynamicActivate(locale: AppLanguage) {
|
||||||
let mod: any
|
let mod: any
|
||||||
|
|
||||||
if (locale === 'hi') {
|
switch (locale) {
|
||||||
mod = await import(`./locales/hi/messages`)
|
case AppLanguage.hi: {
|
||||||
} else {
|
mod = await import(`./locales/hi/messages`)
|
||||||
mod = await import(`./locales/en/messages`)
|
break
|
||||||
|
}
|
||||||
|
default: {
|
||||||
|
mod = await import(`./locales/en/messages`)
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
i18n.load(locale, mod.messages)
|
i18n.load(locale, mod.messages)
|
||||||
|
|||||||
@@ -4,12 +4,17 @@ interface Language {
|
|||||||
name: string
|
name: string
|
||||||
}
|
}
|
||||||
|
|
||||||
interface AppLanguage {
|
export enum AppLanguage {
|
||||||
|
en = 'en',
|
||||||
|
hi = 'hi',
|
||||||
|
}
|
||||||
|
|
||||||
|
interface AppLanguageConfig {
|
||||||
code2: string
|
code2: string
|
||||||
name: string
|
name: string
|
||||||
}
|
}
|
||||||
|
|
||||||
export const APP_LANGUAGES: AppLanguage[] = [
|
export const APP_LANGUAGES: AppLanguageConfig[] = [
|
||||||
{code2: 'en', name: 'English'},
|
{code2: 'en', name: 'English'},
|
||||||
{code2: 'hi', name: 'हिंदी'},
|
{code2: 'hi', name: 'हिंदी'},
|
||||||
]
|
]
|
||||||
|
|||||||
Reference in New Issue
Block a user