Skip to content

Commit

Permalink
fix: 🐛 consider generic locales when registering loaders
Browse files Browse the repository at this point in the history
  • Loading branch information
kaisermann committed Nov 22, 2019
1 parent b19b690 commit 1b0138c
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 16 deletions.
42 changes: 34 additions & 8 deletions src/client/includes/loaderQueue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,10 @@ import { getCurrentLocale } from '../stores/locale'
import { $loading } from '../stores/loading'

import { removeFromLookupCache } from './lookup'
import { getLocalesFrom } from './utils'

const loaderQueue: Record<string, Set<LocaleLoader>> = {}

function getLocaleQueue(locale: string) {
return loaderQueue[locale]
}

function createLocaleQueue(locale: string) {
loaderQueue[locale] = new Set()
}
Expand All @@ -21,15 +18,44 @@ function removeLocaleFromQueue(locale: string) {
delete loaderQueue[locale]
}

function getLocaleQueue(locale: string) {
return loaderQueue[locale]
}

function getLocalesQueue(locale: string) {
return getLocalesFrom(locale)
.reverse()
.reduce(
(acc, localeItem) =>
getLocaleQueue(localeItem)
? acc.concat([...getLocaleQueue(localeItem)])
: acc,
[]
)
}

export function hasLocaleQueue(locale: string) {
return getLocalesFrom(locale)
.reverse()
.some(getLocaleQueue)
}

export function addLoaderToQueue(locale: string, loader: LocaleLoader) {
loaderQueue[locale].add(loader)
}

export async function flushQueue(locale: string = getCurrentLocale()) {
if (!getLocaleQueue(locale)) return

const queue = [...getLocaleQueue(locale)]
if (locale == null) {
throw new Error(
`[svelte-i18n] Invalid locale into "waitLocale": ${JSON.stringify(
locale
)}`
)
}
if (!hasLocaleQueue(locale)) return

// get queue of XX-YY and XX locales
const queue = getLocalesQueue(locale)
if (queue.length === 0) return

removeLocaleFromQueue(locale)
Expand All @@ -54,7 +80,7 @@ export function registerLocaleLoader(locale: string, loader: LocaleLoader) {
if (!getLocaleQueue(locale)) createLocaleQueue(locale)

const queue = getLocaleQueue(locale)
if (queue.has(loader)) return
if (getLocaleQueue(locale).has(loader)) return

if (!hasLocaleDictionary(locale)) {
$dictionary.update(d => {
Expand Down
11 changes: 5 additions & 6 deletions src/client/includes/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,12 @@ const getFromURL = (urlPart: string, key: string) => {
}
}

const getMatch = (base: string, pattern: RegExp) => {
const getFirstMatch = (base: string, pattern: RegExp) => {
const match = pattern.exec(base)
if (!match) return null
return match[1] || null
}

// todo add a urlPattern method/regexp
export const getClientLocale = ({
navigator,
hash,
Expand All @@ -64,12 +63,12 @@ export const getClientLocale = ({
}

if (hostname) {
locale = getMatch(window.location.hostname, hostname)
locale = getFirstMatch(window.location.hostname, hostname)
if (locale) return locale
}

if (pathname) {
locale = getMatch(window.location.pathname, pathname)
locale = getFirstMatch(window.location.pathname, pathname)
if (locale) return locale
}

Expand All @@ -83,15 +82,15 @@ export const getClientLocale = ({
locale =
typeof search === 'string'
? getFromURL(window.location.search, search)
: getMatch(window.location.search, search)
: getFirstMatch(window.location.search, search)
if (locale) return locale
}

if (hash) {
locale =
typeof hash === 'string'
? getFromURL(window.location.hash, hash)
: getMatch(window.location.hash, hash)
: getFirstMatch(window.location.hash, hash)
if (locale) return locale
}

Expand Down
7 changes: 5 additions & 2 deletions src/client/stores/locale.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { writable } from 'svelte/store'

import { getGenericLocaleFrom, getLocalesFrom } from '../includes/utils'
import { flushQueue } from '../includes/loaderQueue'
import { flushQueue, hasLocaleQueue } from '../includes/loaderQueue'

import { getDictionary } from './dictionary'

Expand Down Expand Up @@ -34,7 +34,10 @@ $locale.subscribe((newLocale: string) => {
const localeSet = $locale.set
$locale.set = (newLocale: string): void | Promise<void> => {
if (getAvailableLocale(newLocale)) {
return flushQueue(newLocale).then(() => localeSet(newLocale))
if (hasLocaleQueue(newLocale)) {
return flushQueue(newLocale).then(() => localeSet(newLocale))
}
return localeSet(newLocale)
}

throw Error(`[svelte-i18n] Locale "${newLocale}" not found.`)
Expand Down

0 comments on commit 1b0138c

Please sign in to comment.