mirror of
https://github.com/cupcakearmy/svelte-i18n.git
synced 2024-06-30 18:54:49 +02:00
refactor: 💡 configure to init
This commit is contained in:
parent
cf8d32f0a6
commit
300f965a87
|
@ -59,7 +59,7 @@ export function getOptions() {
|
||||||
return options
|
return options
|
||||||
}
|
}
|
||||||
|
|
||||||
export function configure(opts: ConfigureOptions) {
|
export function init(opts: ConfigureOptions) {
|
||||||
const fallbackLocale = (options.fallbackLocale = opts.fallbackLocale)
|
const fallbackLocale = (options.fallbackLocale = opts.fallbackLocale)
|
||||||
|
|
||||||
const initialLocale = opts.initialLocale
|
const initialLocale = opts.initialLocale
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import { get } from 'svelte/store'
|
import { get } from 'svelte/store'
|
||||||
|
|
||||||
import {
|
import {
|
||||||
configure,
|
init,
|
||||||
getOptions,
|
getOptions,
|
||||||
defaultOptions,
|
defaultOptions,
|
||||||
defaultFormats,
|
defaultFormats,
|
||||||
|
@ -9,19 +9,19 @@ import {
|
||||||
import { $locale } from '../../src/client/stores/locale'
|
import { $locale } from '../../src/client/stores/locale'
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
configure(defaultOptions)
|
init(defaultOptions)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('configures the fallback locale', () => {
|
test('inits the fallback locale', () => {
|
||||||
expect(getOptions().fallbackLocale).toBe(null)
|
expect(getOptions().fallbackLocale).toBe(null)
|
||||||
configure({
|
init({
|
||||||
fallbackLocale: 'en',
|
fallbackLocale: 'en',
|
||||||
})
|
})
|
||||||
expect(getOptions().fallbackLocale).toBe('en')
|
expect(getOptions().fallbackLocale).toBe('en')
|
||||||
})
|
})
|
||||||
|
|
||||||
test('configures the initial locale by string', () => {
|
test('inits the initial locale by string', () => {
|
||||||
configure({
|
init({
|
||||||
fallbackLocale: 'pt',
|
fallbackLocale: 'pt',
|
||||||
initialLocale: 'en',
|
initialLocale: 'en',
|
||||||
})
|
})
|
||||||
|
@ -29,7 +29,7 @@ test('configures the initial locale by string', () => {
|
||||||
expect(get($locale)).toBe('en')
|
expect(get($locale)).toBe('en')
|
||||||
})
|
})
|
||||||
|
|
||||||
test('configures the initial locale by client heuristics', () => {
|
test('inits the initial locale by client heuristics', () => {
|
||||||
delete window.location
|
delete window.location
|
||||||
window.location = {
|
window.location = {
|
||||||
search: '?lang=en-US&foo',
|
search: '?lang=en-US&foo',
|
||||||
|
@ -38,7 +38,7 @@ test('configures the initial locale by client heuristics', () => {
|
||||||
hash: '',
|
hash: '',
|
||||||
} as any
|
} as any
|
||||||
|
|
||||||
configure({
|
init({
|
||||||
fallbackLocale: 'pt',
|
fallbackLocale: 'pt',
|
||||||
initialLocale: {
|
initialLocale: {
|
||||||
search: 'lang',
|
search: 'lang',
|
||||||
|
@ -51,7 +51,7 @@ test('configures the initial locale by client heuristics', () => {
|
||||||
test('adds custom formats for time, date and number values', () => {
|
test('adds custom formats for time, date and number values', () => {
|
||||||
const customFormats = require('../fixtures/formats.json')
|
const customFormats = require('../fixtures/formats.json')
|
||||||
|
|
||||||
configure({
|
init({
|
||||||
fallbackLocale: 'en',
|
fallbackLocale: 'en',
|
||||||
formats: customFormats,
|
formats: customFormats,
|
||||||
})
|
})
|
||||||
|
@ -60,6 +60,6 @@ test('adds custom formats for time, date and number values', () => {
|
||||||
})
|
})
|
||||||
|
|
||||||
test('sets the minimum delay to set the loading store value', () => {
|
test('sets the minimum delay to set the loading store value', () => {
|
||||||
configure({ fallbackLocale: 'en', loadingDelay: 300 })
|
init({ fallbackLocale: 'en', loadingDelay: 300 })
|
||||||
expect(getOptions().loadingDelay).toBe(300)
|
expect(getOptions().loadingDelay).toBe(300)
|
||||||
})
|
})
|
||||||
|
|
|
@ -9,17 +9,17 @@ import {
|
||||||
$locale,
|
$locale,
|
||||||
isRelatedLocale,
|
isRelatedLocale,
|
||||||
} from '../../../src/client/stores/locale'
|
} from '../../../src/client/stores/locale'
|
||||||
import { getOptions, configure } from '../../../src/client/configs'
|
import { getOptions, init } from '../../../src/client/configs'
|
||||||
import { register } from '../../../src/client'
|
import { register } from '../../../src/client'
|
||||||
import { hasLocaleQueue } from '../../../src/client/includes/loaderQueue'
|
import { hasLocaleQueue } from '../../../src/client/includes/loaderQueue'
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
configure({ fallbackLocale: undefined })
|
init({ fallbackLocale: undefined })
|
||||||
$locale.set(undefined)
|
$locale.set(undefined)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('sets and gets the fallback locale', () => {
|
test('sets and gets the fallback locale', () => {
|
||||||
configure({ fallbackLocale: 'en' })
|
init({ fallbackLocale: 'en' })
|
||||||
expect(getOptions().fallbackLocale).toBe('en')
|
expect(getOptions().fallbackLocale).toBe('en')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -44,17 +44,17 @@ test('gets the next fallback locale of a locale', () => {
|
||||||
})
|
})
|
||||||
|
|
||||||
test('gets the global fallback locale if set', () => {
|
test('gets the global fallback locale if set', () => {
|
||||||
configure({ fallbackLocale: 'en' })
|
init({ fallbackLocale: 'en' })
|
||||||
expect(getFallbackOf('it')).toBe('en')
|
expect(getFallbackOf('it')).toBe('en')
|
||||||
})
|
})
|
||||||
|
|
||||||
test('should not get the global fallback as the fallback of itself', () => {
|
test('should not get the global fallback as the fallback of itself', () => {
|
||||||
configure({ fallbackLocale: 'en' })
|
init({ fallbackLocale: 'en' })
|
||||||
expect(getFallbackOf('en')).toBe(null)
|
expect(getFallbackOf('en')).toBe(null)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('if global fallback locale has a fallback, it should return it', () => {
|
test('if global fallback locale has a fallback, it should return it', () => {
|
||||||
configure({ fallbackLocale: 'en-US' })
|
init({ fallbackLocale: 'en-US' })
|
||||||
expect(getFallbackOf('en-US')).toBe('en')
|
expect(getFallbackOf('en-US')).toBe('en')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -69,7 +69,7 @@ test('gets all fallback locales of a locale', () => {
|
||||||
})
|
})
|
||||||
|
|
||||||
test('gets all fallback locales of a locale including the global fallback locale', () => {
|
test('gets all fallback locales of a locale including the global fallback locale', () => {
|
||||||
configure({ fallbackLocale: 'pt' })
|
init({ fallbackLocale: 'pt' })
|
||||||
expect(getRelatedLocalesOf('en-US')).toEqual(['en', 'en-US', 'pt'])
|
expect(getRelatedLocalesOf('en-US')).toEqual(['en', 'en-US', 'pt'])
|
||||||
expect(getRelatedLocalesOf('en-US')).toEqual(['en', 'en-US', 'pt'])
|
expect(getRelatedLocalesOf('en-US')).toEqual(['en', 'en-US', 'pt'])
|
||||||
expect(getRelatedLocalesOf('az-Cyrl-AZ')).toEqual([
|
expect(getRelatedLocalesOf('az-Cyrl-AZ')).toEqual([
|
||||||
|
@ -80,7 +80,7 @@ test('gets all fallback locales of a locale including the global fallback locale
|
||||||
])
|
])
|
||||||
})
|
})
|
||||||
test('gets all fallback locales of a locale including the global fallback locale and its fallbacks', () => {
|
test('gets all fallback locales of a locale including the global fallback locale and its fallbacks', () => {
|
||||||
configure({ fallbackLocale: 'pt-BR' })
|
init({ fallbackLocale: 'pt-BR' })
|
||||||
expect(getRelatedLocalesOf('en-US')).toEqual(['en', 'en-US', 'pt', 'pt-BR'])
|
expect(getRelatedLocalesOf('en-US')).toEqual(['en', 'en-US', 'pt', 'pt-BR'])
|
||||||
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([
|
expect(getRelatedLocalesOf('az-Cyrl-AZ')).toEqual([
|
||||||
|
@ -93,7 +93,7 @@ test('gets all fallback locales of a locale including the global fallback locale
|
||||||
})
|
})
|
||||||
|
|
||||||
test("don't list fallback locale twice", () => {
|
test("don't list fallback locale twice", () => {
|
||||||
configure({ fallbackLocale: 'pt-BR' })
|
init({ fallbackLocale: 'pt-BR' })
|
||||||
expect(getRelatedLocalesOf('pt-BR')).toEqual(['pt', 'pt-BR'])
|
expect(getRelatedLocalesOf('pt-BR')).toEqual(['pt', 'pt-BR'])
|
||||||
expect(getRelatedLocalesOf('pt')).toEqual(['pt'])
|
expect(getRelatedLocalesOf('pt')).toEqual(['pt'])
|
||||||
})
|
})
|
||||||
|
@ -105,13 +105,13 @@ test('gets the current locale', () => {
|
||||||
})
|
})
|
||||||
|
|
||||||
test('if no initial locale is set, set the locale to the fallback', () => {
|
test('if no initial locale is set, set the locale to the fallback', () => {
|
||||||
configure({ fallbackLocale: 'pt' })
|
init({ fallbackLocale: 'pt' })
|
||||||
expect(get($locale)).toBe('pt')
|
expect(get($locale)).toBe('pt')
|
||||||
expect(getOptions().fallbackLocale).toBe('pt')
|
expect(getOptions().fallbackLocale).toBe('pt')
|
||||||
})
|
})
|
||||||
|
|
||||||
test('if no initial locale was found, set to the fallback locale', () => {
|
test('if no initial locale was found, set to the fallback locale', () => {
|
||||||
configure({
|
init({
|
||||||
fallbackLocale: 'en',
|
fallbackLocale: 'en',
|
||||||
initialLocale: {
|
initialLocale: {
|
||||||
hash: 'lang',
|
hash: 'lang',
|
||||||
|
|
Loading…
Reference in New Issue
Block a user