mirror of
https://github.com/cupcakearmy/svelte-i18n.git
synced 2024-06-30 18:54:49 +02: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> } = {}
|
const activeLocaleFlushes: { [key: string]: Promise<void> } = {}
|
||||||
export function flushQueue(locale: string = getCurrentLocale()) {
|
export function flush(locale: string) {
|
||||||
if (!hasLocaleQueue(locale)) return
|
if (!hasLocaleQueue(locale)) return
|
||||||
if (locale in activeLocaleFlushes) return activeLocaleFlushes[locale]
|
if (locale in activeLocaleFlushes) return activeLocaleFlushes[locale]
|
||||||
|
|
||||||
|
|
|
@ -1,10 +1,22 @@
|
||||||
import { MessageObject } from './types'
|
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
|
// defineMessages allow us to define and extract dynamic message ids
|
||||||
export function defineMessages(i: Record<string, MessageObject>) {
|
export function defineMessages(i: Record<string, MessageObject>) {
|
||||||
return i
|
return i
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function waitLocale(locale: string) {
|
||||||
|
return flush(
|
||||||
|
locale ||
|
||||||
|
getCurrentLocale() ||
|
||||||
|
getOptions().initialLocale ||
|
||||||
|
getOptions().fallbackLocale
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
export { init } from './configs'
|
export { init } from './configs'
|
||||||
export { $locale as locale } from './stores/locale'
|
export { $locale as locale } from './stores/locale'
|
||||||
export {
|
export {
|
||||||
|
@ -16,7 +28,4 @@ export { $isLoading as isLoading } from './stores/loading'
|
||||||
export { $format as format, $format as _, $format as t } from './stores/format'
|
export { $format as format, $format as _, $format as t } from './stores/format'
|
||||||
|
|
||||||
// utilities
|
// utilities
|
||||||
export {
|
export { registerLocaleLoader as register } from './includes/loaderQueue'
|
||||||
flushQueue as waitLocale,
|
|
||||||
registerLocaleLoader as register,
|
|
||||||
} from './includes/loaderQueue'
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import { writable } from 'svelte/store'
|
import { writable } from 'svelte/store'
|
||||||
|
|
||||||
import { flushQueue, hasLocaleQueue } from '../includes/loaderQueue'
|
import { flush, hasLocaleQueue } from '../includes/loaderQueue'
|
||||||
import { getOptions } from '../configs'
|
import { getOptions } from '../configs'
|
||||||
|
|
||||||
import { getClosestAvailableLocale } from './dictionary'
|
import { getClosestAvailableLocale } from './dictionary'
|
||||||
|
@ -58,7 +58,7 @@ $locale.subscribe((newLocale: string) => {
|
||||||
const localeSet = $locale.set
|
const localeSet = $locale.set
|
||||||
$locale.set = (newLocale: string): void | Promise<void> => {
|
$locale.set = (newLocale: string): void | Promise<void> => {
|
||||||
if (getClosestAvailableLocale(newLocale) && hasLocaleQueue(newLocale)) {
|
if (getClosestAvailableLocale(newLocale) && hasLocaleQueue(newLocale)) {
|
||||||
return flushQueue(newLocale).then(() => localeSet(newLocale))
|
return flush(newLocale).then(() => localeSet(newLocale))
|
||||||
}
|
}
|
||||||
return localeSet(newLocale)
|
return localeSet(newLocale)
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,7 @@ import { get } from 'svelte/store'
|
||||||
|
|
||||||
import {
|
import {
|
||||||
hasLocaleQueue,
|
hasLocaleQueue,
|
||||||
flushQueue,
|
flush,
|
||||||
registerLocaleLoader,
|
registerLocaleLoader,
|
||||||
resetQueues,
|
resetQueues,
|
||||||
} from '../../../src/client/includes/loaderQueue'
|
} 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", () => {
|
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 () => {
|
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', loader({ field: 'Name' }))
|
||||||
registerLocaleLoader('en-US', loader({ field_2: 'Lastname' }))
|
registerLocaleLoader('en-US', loader({ field_2: 'Lastname' }))
|
||||||
|
|
||||||
await flushQueue('en-US')
|
await flush('en-US')
|
||||||
|
|
||||||
expect(getMessageFromDictionary('en', 'field')).toBe('Name')
|
expect(getMessageFromDictionary('en', 'field')).toBe('Name')
|
||||||
expect(getMessageFromDictionary('en-US', 'field_2')).toBe('Lastname')
|
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 () => {
|
test('consecutive flushes return the same promise', async () => {
|
||||||
registerLocaleLoader('en', async () => ({}))
|
registerLocaleLoader('en', async () => ({}))
|
||||||
|
|
||||||
const flushA = flushQueue('en')
|
const flushA = flush('en')
|
||||||
const flushB = flushQueue('en')
|
const flushB = flush('en')
|
||||||
const flushC = flushQueue('en')
|
const flushC = flush('en')
|
||||||
|
|
||||||
expect(flushB).toStrictEqual(flushA)
|
expect(flushB).toStrictEqual(flushA)
|
||||||
expect(flushC).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) => {
|
return new Promise((res, rej) => {
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user