mirror of
https://github.com/cupcakearmy/svelte-i18n.git
synced 2024-11-16 18:10:43 +01:00
fix: 🐛 allow to wait for initial locale load
This commit is contained in:
parent
59289c8443
commit
0b7f61c49a
@ -46,7 +46,7 @@ export function hasLocaleQueue(locale: string) {
|
||||
}
|
||||
|
||||
const activeLocaleFlushes: { [key: string]: Promise<void> } = {}
|
||||
export function flushQueue(locale: string = getCurrentLocale()) {
|
||||
export function flush(locale: string) {
|
||||
if (!hasLocaleQueue(locale)) return
|
||||
if (locale in activeLocaleFlushes) return activeLocaleFlushes[locale]
|
||||
|
||||
|
@ -1,10 +1,22 @@
|
||||
import { MessageObject } from './types'
|
||||
import { getCurrentLocale } from './stores/locale'
|
||||
import { getOptions } from './configs'
|
||||
import { flush } from './includes/loaderQueue'
|
||||
|
||||
// defineMessages allow us to define and extract dynamic message ids
|
||||
export function defineMessages(i: Record<string, MessageObject>) {
|
||||
return i
|
||||
}
|
||||
|
||||
export function waitLocale(locale: string) {
|
||||
return flush(
|
||||
locale ||
|
||||
getCurrentLocale() ||
|
||||
getOptions().initialLocale ||
|
||||
getOptions().fallbackLocale
|
||||
)
|
||||
}
|
||||
|
||||
export { init } from './configs'
|
||||
export { $locale as locale } from './stores/locale'
|
||||
export {
|
||||
@ -16,7 +28,4 @@ export { $isLoading as isLoading } from './stores/loading'
|
||||
export { $format as format, $format as _, $format as t } from './stores/format'
|
||||
|
||||
// utilities
|
||||
export {
|
||||
flushQueue as waitLocale,
|
||||
registerLocaleLoader as register,
|
||||
} from './includes/loaderQueue'
|
||||
export { registerLocaleLoader as register } from './includes/loaderQueue'
|
||||
|
@ -1,6 +1,6 @@
|
||||
import { writable } from 'svelte/store'
|
||||
|
||||
import { flushQueue, hasLocaleQueue } from '../includes/loaderQueue'
|
||||
import { flush, hasLocaleQueue } from '../includes/loaderQueue'
|
||||
import { getOptions } from '../configs'
|
||||
|
||||
import { getClosestAvailableLocale } from './dictionary'
|
||||
@ -58,7 +58,7 @@ $locale.subscribe((newLocale: string) => {
|
||||
const localeSet = $locale.set
|
||||
$locale.set = (newLocale: string): void | Promise<void> => {
|
||||
if (getClosestAvailableLocale(newLocale) && hasLocaleQueue(newLocale)) {
|
||||
return flushQueue(newLocale).then(() => localeSet(newLocale))
|
||||
return flush(newLocale).then(() => localeSet(newLocale))
|
||||
}
|
||||
return localeSet(newLocale)
|
||||
}
|
||||
|
@ -2,7 +2,7 @@ import { get } from 'svelte/store'
|
||||
|
||||
import {
|
||||
hasLocaleQueue,
|
||||
flushQueue,
|
||||
flush,
|
||||
registerLocaleLoader,
|
||||
resetQueues,
|
||||
} from '../../../src/client/includes/loaderQueue'
|
||||
@ -28,14 +28,14 @@ test('checks if exist queues of locale and its fallbacks', () => {
|
||||
})
|
||||
|
||||
test("does nothing if there's no queue for a locale", () => {
|
||||
expect(flushQueue('foo')).toBe(undefined)
|
||||
expect(flush('foo')).toBe(undefined)
|
||||
})
|
||||
|
||||
test('flushes the queue of a locale and its fallbacks and merge the result with the dictionary', async () => {
|
||||
registerLocaleLoader('en', loader({ field: 'Name' }))
|
||||
registerLocaleLoader('en-US', loader({ field_2: 'Lastname' }))
|
||||
|
||||
await flushQueue('en-US')
|
||||
await flush('en-US')
|
||||
|
||||
expect(getMessageFromDictionary('en', 'field')).toBe('Name')
|
||||
expect(getMessageFromDictionary('en-US', 'field_2')).toBe('Lastname')
|
||||
@ -47,9 +47,9 @@ test('flushes the queue of a locale and its fallbacks and merge the result with
|
||||
test('consecutive flushes return the same promise', async () => {
|
||||
registerLocaleLoader('en', async () => ({}))
|
||||
|
||||
const flushA = flushQueue('en')
|
||||
const flushB = flushQueue('en')
|
||||
const flushC = flushQueue('en')
|
||||
const flushA = flush('en')
|
||||
const flushB = flush('en')
|
||||
const flushC = flush('en')
|
||||
|
||||
expect(flushB).toStrictEqual(flushA)
|
||||
expect(flushC).toStrictEqual(flushA)
|
||||
@ -64,7 +64,7 @@ test('should set loading to true if passed min delay and false after loading', (
|
||||
)
|
||||
)
|
||||
|
||||
const flush = flushQueue('en')
|
||||
const flush = flush('en')
|
||||
|
||||
return new Promise((res, rej) => {
|
||||
setTimeout(() => {
|
||||
|
Loading…
Reference in New Issue
Block a user