2019-11-26 03:04:10 +01:00
|
|
|
import { get } from 'svelte/store'
|
|
|
|
|
2020-01-23 14:11:04 +01:00
|
|
|
import { lookup } from '../../../src/runtime/includes/lookup'
|
2019-11-25 21:16:38 +01:00
|
|
|
import {
|
|
|
|
isFallbackLocaleOf,
|
|
|
|
getFallbackOf,
|
2019-11-26 03:04:10 +01:00
|
|
|
getRelatedLocalesOf,
|
2019-11-25 21:16:38 +01:00
|
|
|
getCurrentLocale,
|
|
|
|
$locale,
|
|
|
|
isRelatedLocale,
|
2020-01-23 14:11:04 +01:00
|
|
|
} from '../../../src/runtime/stores/locale'
|
|
|
|
import { getOptions, init } from '../../../src/runtime/configs'
|
|
|
|
import { register } from '../../../src/runtime'
|
|
|
|
import { hasLocaleQueue } from '../../../src/runtime/includes/loaderQueue'
|
2019-11-25 21:16:38 +01:00
|
|
|
|
|
|
|
beforeEach(() => {
|
2019-11-27 04:58:53 +01:00
|
|
|
init({ fallbackLocale: undefined })
|
2019-11-26 01:41:05 +01:00
|
|
|
$locale.set(undefined)
|
2019-11-25 21:16:38 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
test('sets and gets the fallback locale', () => {
|
2019-11-27 04:58:53 +01:00
|
|
|
init({ fallbackLocale: 'en' })
|
2019-11-26 17:05:17 +01:00
|
|
|
expect(getOptions().fallbackLocale).toBe('en')
|
2019-11-25 21:16:38 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
test('checks if a locale is a fallback locale of another locale', () => {
|
|
|
|
expect(isFallbackLocaleOf('en', 'en-US')).toBe(true)
|
|
|
|
expect(isFallbackLocaleOf('en', 'en')).toBe(false)
|
|
|
|
expect(isFallbackLocaleOf('it', 'en-US')).toBe(false)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('checks if a locale is a fallback locale of another locale', () => {
|
|
|
|
expect(isRelatedLocale('en', 'en-US')).toBe(true)
|
|
|
|
expect(isRelatedLocale('pt-BR', 'pt')).toBe(true)
|
|
|
|
expect(isRelatedLocale('en', 'en')).toBe(true)
|
|
|
|
expect(isRelatedLocale('en', 'it-IT')).toBe(false)
|
|
|
|
expect(isRelatedLocale('en-US', 'it')).toBe(false)
|
|
|
|
})
|
|
|
|
|
2019-11-26 01:41:05 +01:00
|
|
|
test('gets the next fallback locale of a locale', () => {
|
2019-11-25 21:16:38 +01:00
|
|
|
expect(getFallbackOf('az-Cyrl-AZ')).toBe('az-Cyrl')
|
|
|
|
expect(getFallbackOf('en-US')).toBe('en')
|
|
|
|
expect(getFallbackOf('en')).toBe(null)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('gets the global fallback locale if set', () => {
|
2019-11-27 04:58:53 +01:00
|
|
|
init({ fallbackLocale: 'en' })
|
2019-11-25 21:16:38 +01:00
|
|
|
expect(getFallbackOf('it')).toBe('en')
|
|
|
|
})
|
|
|
|
|
|
|
|
test('should not get the global fallback as the fallback of itself', () => {
|
2019-11-27 04:58:53 +01:00
|
|
|
init({ fallbackLocale: 'en' })
|
2019-11-25 21:16:38 +01:00
|
|
|
expect(getFallbackOf('en')).toBe(null)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('if global fallback locale has a fallback, it should return it', () => {
|
2019-11-27 04:58:53 +01:00
|
|
|
init({ fallbackLocale: 'en-US' })
|
2019-11-25 21:16:38 +01:00
|
|
|
expect(getFallbackOf('en-US')).toBe('en')
|
|
|
|
})
|
|
|
|
|
2019-11-26 01:41:05 +01:00
|
|
|
test('gets all fallback locales of a locale', () => {
|
2019-11-26 03:04:10 +01:00
|
|
|
expect(getRelatedLocalesOf('en-US')).toEqual(['en', 'en-US'])
|
|
|
|
expect(getRelatedLocalesOf('en-US')).toEqual(['en', 'en-US'])
|
|
|
|
expect(getRelatedLocalesOf('az-Cyrl-AZ')).toEqual([
|
|
|
|
'az',
|
|
|
|
'az-Cyrl',
|
|
|
|
'az-Cyrl-AZ',
|
|
|
|
])
|
2019-11-25 21:16:38 +01:00
|
|
|
})
|
|
|
|
|
2019-11-26 01:41:05 +01:00
|
|
|
test('gets all fallback locales of a locale including the global fallback locale', () => {
|
2019-11-27 04:58:53 +01:00
|
|
|
init({ fallbackLocale: 'pt' })
|
2019-11-26 03:04:10 +01:00
|
|
|
expect(getRelatedLocalesOf('en-US')).toEqual(['en', 'en-US', 'pt'])
|
|
|
|
expect(getRelatedLocalesOf('en-US')).toEqual(['en', 'en-US', 'pt'])
|
|
|
|
expect(getRelatedLocalesOf('az-Cyrl-AZ')).toEqual([
|
2019-11-25 21:16:38 +01:00
|
|
|
'az',
|
|
|
|
'az-Cyrl',
|
|
|
|
'az-Cyrl-AZ',
|
|
|
|
'pt',
|
|
|
|
])
|
|
|
|
})
|
2019-11-26 01:41:05 +01:00
|
|
|
test('gets all fallback locales of a locale including the global fallback locale and its fallbacks', () => {
|
2019-11-27 04:58:53 +01:00
|
|
|
init({ fallbackLocale: 'pt-BR' })
|
2019-11-26 03:04:10 +01:00
|
|
|
expect(getRelatedLocalesOf('en-US')).toEqual(['en', 'en-US', 'pt', 'pt-BR'])
|
|
|
|
expect(getRelatedLocalesOf('en-US')).toEqual(['en', 'en-US', 'pt', 'pt-BR'])
|
|
|
|
expect(getRelatedLocalesOf('az-Cyrl-AZ')).toEqual([
|
2019-11-26 01:41:05 +01:00
|
|
|
'az',
|
|
|
|
'az-Cyrl',
|
|
|
|
'az-Cyrl-AZ',
|
|
|
|
'pt',
|
|
|
|
'pt-BR',
|
|
|
|
])
|
|
|
|
})
|
2019-11-25 21:16:38 +01:00
|
|
|
|
2019-11-26 01:41:05 +01:00
|
|
|
test("don't list fallback locale twice", () => {
|
2019-11-27 04:58:53 +01:00
|
|
|
init({ fallbackLocale: 'pt-BR' })
|
2019-11-26 03:04:10 +01:00
|
|
|
expect(getRelatedLocalesOf('pt-BR')).toEqual(['pt', 'pt-BR'])
|
|
|
|
expect(getRelatedLocalesOf('pt')).toEqual(['pt'])
|
2019-11-25 21:16:38 +01:00
|
|
|
})
|
2019-11-26 01:41:05 +01:00
|
|
|
|
|
|
|
test('gets the current locale', () => {
|
|
|
|
expect(getCurrentLocale()).toBe(undefined)
|
|
|
|
$locale.set('es-ES')
|
|
|
|
expect(getCurrentLocale()).toBe('es-ES')
|
|
|
|
})
|
|
|
|
|
2019-11-26 03:04:10 +01:00
|
|
|
test('if no initial locale is set, set the locale to the fallback', () => {
|
2019-11-27 04:58:53 +01:00
|
|
|
init({ fallbackLocale: 'pt' })
|
2019-11-26 01:41:05 +01:00
|
|
|
expect(get($locale)).toBe('pt')
|
2019-11-26 17:05:17 +01:00
|
|
|
expect(getOptions().fallbackLocale).toBe('pt')
|
2019-11-26 01:41:05 +01:00
|
|
|
})
|
2019-11-26 03:04:10 +01:00
|
|
|
|
|
|
|
test('if no initial locale was found, set to the fallback locale', () => {
|
2019-11-27 04:58:53 +01:00
|
|
|
init({
|
2019-11-26 03:04:10 +01:00
|
|
|
fallbackLocale: 'en',
|
|
|
|
initialLocale: {
|
|
|
|
hash: 'lang',
|
|
|
|
},
|
|
|
|
})
|
|
|
|
expect(get($locale)).toBe('en')
|
2019-11-26 17:05:17 +01:00
|
|
|
expect(getOptions().fallbackLocale).toBe('en')
|
2019-11-26 03:04:10 +01:00
|
|
|
})
|
2019-11-27 02:25:33 +01:00
|
|
|
|
|
|
|
test('should flush the queue of the locale when changing the store value', async () => {
|
|
|
|
register(
|
|
|
|
'en',
|
|
|
|
() => new Promise(res => setTimeout(() => res({ foo: 'Foo' }), 50))
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(hasLocaleQueue('en')).toBe(true)
|
|
|
|
|
|
|
|
await $locale.set('en')
|
|
|
|
|
|
|
|
expect(hasLocaleQueue('en')).toBe(false)
|
2020-01-08 15:28:44 +01:00
|
|
|
expect(lookup('foo', 'en')).toBe('Foo')
|
2019-11-27 02:25:33 +01:00
|
|
|
})
|