refactor: 💡 rename fallback locale methods

This commit is contained in:
Christian Kaisermann 2019-11-21 23:30:59 -03:00
parent 64c8e55f80
commit 49e9562890
2 changed files with 5 additions and 5 deletions

View File

@ -16,12 +16,12 @@ export function lower(str: string) {
return str.toLocaleLowerCase() return str.toLocaleLowerCase()
} }
export function getGenericLocaleFrom(locale: string) { export function getFallbackLocale(locale: string) {
const index = locale.lastIndexOf('-') const index = locale.lastIndexOf('-')
return index > 0 ? locale.slice(0, index) : null 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('-')) return locale.split('-').map((_, i, arr) => arr.slice(0, i + 1).join('-'))
} }

View File

@ -1,6 +1,6 @@
import { writable } from 'svelte/store' import { writable } from 'svelte/store'
import { getGenericLocaleFrom, getLocalesFrom } from '../includes/utils' import { getFallbackLocale, getAllFallbackLocales } from '../includes/utils'
import { flushQueue, hasLocaleQueue } from '../includes/loaderQueue' import { flushQueue, hasLocaleQueue } from '../includes/loaderQueue'
import { getDictionary } from './dictionary' import { getDictionary } from './dictionary'
@ -14,12 +14,12 @@ function getCurrentLocale() {
function getAvailableLocale(locale: string): string | null { function getAvailableLocale(locale: string): string | null {
if (locale in getDictionary() || locale == null) return locale if (locale in getDictionary() || locale == null) return locale
return getAvailableLocale(getGenericLocaleFrom(locale)) return getAvailableLocale(getFallbackLocale(locale))
} }
function loadLocale(localeToLoad: string) { function loadLocale(localeToLoad: string) {
return Promise.all( return Promise.all(
getLocalesFrom(localeToLoad).map(localeItem => getAllFallbackLocales(localeToLoad).map(localeItem =>
flushQueue(localeItem) flushQueue(localeItem)
.then(() => [localeItem, { err: undefined }]) .then(() => [localeItem, { err: undefined }])
.catch(e => [localeItem, { err: e }]) .catch(e => [localeItem, { err: e }])