mirror of
https://github.com/cupcakearmy/svelte-i18n.git
synced 2024-11-16 09:59:58 +01:00
refactor: 💡 rename fallback locale methods
This commit is contained in:
parent
64c8e55f80
commit
49e9562890
@ -16,12 +16,12 @@ export function lower(str: string) {
|
||||
return str.toLocaleLowerCase()
|
||||
}
|
||||
|
||||
export function getGenericLocaleFrom(locale: string) {
|
||||
export function getFallbackLocale(locale: string) {
|
||||
const index = locale.lastIndexOf('-')
|
||||
return index > 0 ? locale.slice(0, index) : null
|
||||
}
|
||||
|
||||
export function getLocalesFrom(locale: string) {
|
||||
export function getAllFallbackLocales(locale: string) {
|
||||
return locale.split('-').map((_, i, arr) => arr.slice(0, i + 1).join('-'))
|
||||
}
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
import { writable } from 'svelte/store'
|
||||
|
||||
import { getGenericLocaleFrom, getLocalesFrom } from '../includes/utils'
|
||||
import { getFallbackLocale, getAllFallbackLocales } from '../includes/utils'
|
||||
import { flushQueue, hasLocaleQueue } from '../includes/loaderQueue'
|
||||
|
||||
import { getDictionary } from './dictionary'
|
||||
@ -14,12 +14,12 @@ function getCurrentLocale() {
|
||||
|
||||
function getAvailableLocale(locale: string): string | null {
|
||||
if (locale in getDictionary() || locale == null) return locale
|
||||
return getAvailableLocale(getGenericLocaleFrom(locale))
|
||||
return getAvailableLocale(getFallbackLocale(locale))
|
||||
}
|
||||
|
||||
function loadLocale(localeToLoad: string) {
|
||||
return Promise.all(
|
||||
getLocalesFrom(localeToLoad).map(localeItem =>
|
||||
getAllFallbackLocales(localeToLoad).map(localeItem =>
|
||||
flushQueue(localeItem)
|
||||
.then(() => [localeItem, { err: undefined }])
|
||||
.catch(e => [localeItem, { err: e }])
|
||||
|
Loading…
Reference in New Issue
Block a user