Skip to content

Commit

Permalink
fix: 🐛 fallback behaviour and simplify API contact points
Browse files Browse the repository at this point in the history
  • Loading branch information
kaisermann committed Nov 24, 2019
1 parent ea2ac47 commit 64e69eb
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 96 deletions.
9 changes: 5 additions & 4 deletions example/src/routes/_layout.svelte
@@ -1,10 +1,11 @@
<script context="module">
import { isLoading, waitInitialLocale } from 'svelte-i18n'
import { isLoading, setInitialLocale } from 'svelte-i18n'
export async function preload() {
return waitInitialLocale({
default: 'en-US',
navigator: true,
return setInitialLocale({
fallback: 'en-US',
// navigator: true,
search: 'lang'
})
}
</script>
Expand Down
8 changes: 3 additions & 5 deletions src/client/includes/loaderQueue.ts
Expand Up @@ -4,11 +4,9 @@ import {
$dictionary,
addMessages,
} from '../stores/dictionary'
import { getCurrentLocale } from '../stores/locale'
import { getCurrentLocale, getFallbacksOf } from '../stores/locale'
import { $isLoading } from '../stores/loading'

import { getAllFallbackLocales } from './utils'

type Queue = Set<MessagesLoader>
const loaderQueue: Record<string, Queue> = {}

Expand All @@ -25,7 +23,7 @@ function getLocaleQueue(locale: string) {
}

function getLocalesQueues(locale: string) {
return getAllFallbackLocales(locale)
return getFallbacksOf(locale)
.reverse()
.map<[string, MessagesLoader[]]>(localeItem => {
const queue = getLocaleQueue(localeItem)
Expand All @@ -35,7 +33,7 @@ function getLocalesQueues(locale: string) {
}

export function hasLocaleQueue(locale: string) {
return getAllFallbackLocales(locale)
return getFallbacksOf(locale)
.reverse()
.some(getLocaleQueue)
}
Expand Down
5 changes: 2 additions & 3 deletions src/client/includes/lookup.ts
Expand Up @@ -2,8 +2,7 @@
import resolvePath from 'object-resolve-path'

import { hasLocaleDictionary } from '../stores/dictionary'

import { getFallbackLocale } from './utils'
import { getFallbackOf } from '../stores/locale'

const lookupCache: Record<string, Record<string, string>> = {}

Expand Down Expand Up @@ -34,6 +33,6 @@ export const lookupMessage = (
return addToCache(
path,
locale,
lookupMessage(dictionary, path, getFallbackLocale(locale))
lookupMessage(dictionary, path, getFallbackOf(locale))
)
}
16 changes: 3 additions & 13 deletions src/client/includes/utils.ts
Expand Up @@ -16,15 +16,6 @@ export function lower(str: string) {
return str.toLocaleLowerCase()
}

export function getFallbackLocale(locale: string) {
const index = locale.lastIndexOf('-')
return index > 0 ? locale.slice(0, index) : null
}

export function getAllFallbackLocales(locale: string) {
return locale.split('-').map((_, i, arr) => arr.slice(0, i + 1).join('-'))
}

const getFromURL = (urlPart: string, key: string) => {
const keyVal = urlPart
.substr(1)
Expand All @@ -49,12 +40,11 @@ export const getClientLocale = ({
pathname,
hostname,
default: defaultLocale,
fallback = defaultLocale,
}: GetClientLocaleOptions) => {
let locale

if (typeof window === 'undefined') {
return defaultLocale
}
if (typeof window === 'undefined') return fallback

if (hostname) {
locale = getFirstMatch(window.location.hostname, hostname)
Expand Down Expand Up @@ -88,5 +78,5 @@ export const getClientLocale = ({
if (locale) return locale
}

return defaultLocale
return fallback
}
20 changes: 8 additions & 12 deletions src/client/index.ts
@@ -1,22 +1,18 @@
import merge from 'deepmerge'

import { GetClientLocaleOptions, MessageObject } from './types'
import { getClientLocale } from './includes/utils'
import { $locale } from './stores/locale'
import { MessageObject } from './types'

// defineMessages allow us to define and extract dynamic message ids
export function defineMessages(i: Record<string, MessageObject>) {
return i
}

export function waitInitialLocale(options: GetClientLocaleOptions | string) {
if (typeof options === 'string') {
return $locale.set(options)
}
return $locale.set(getClientLocale(options))
}

export { $locale as locale, loadLocale as preloadLocale } from './stores/locale'
export {
$locale as locale,
setInitialLocale,
// @deprecated
setInitialLocale as waitInitialLocale,
} from './stores/locale'
export {
$dictionary as dictionary,
$locales as locales,
Expand All @@ -26,7 +22,7 @@ export { $isLoading as isLoading } from './stores/loading'
export { $format as format, $format as _, $format as t } from './stores/format'

// utilities
export { getClientLocale, merge }
export { merge }
export { customFormats, addCustomFormats } from './includes/formats'
export {
flushQueue as waitLocale,
Expand Down
27 changes: 13 additions & 14 deletions src/client/stores/dictionary.ts
Expand Up @@ -3,22 +3,25 @@ import { writable, derived } from 'svelte/store'

import { LocaleDictionary } from '../types/index'

let dictionary: LocaleDictionary
import { getFallbackOf } from './locale'

let dictionary: LocaleDictionary
const $dictionary = writable<LocaleDictionary>({})
$dictionary.subscribe(newDictionary => {
dictionary = newDictionary
})

function getDictionary() {
export function getDictionary() {
return dictionary
}

function hasLocaleDictionary(locale: string) {
export function hasLocaleDictionary(locale: string) {
return locale in dictionary
}

function addMessages(locale: string, ...partials: LocaleDictionary[]) {
export function getAvailableLocale(locale: string): string | null {
if (locale in dictionary || locale == null) return locale
return getAvailableLocale(getFallbackOf(locale))
}

export function addMessages(locale: string, ...partials: LocaleDictionary[]) {
$dictionary.update(d => {
dictionary[locale] = merge.all([dictionary[locale] || {}].concat(partials))
return d
Expand All @@ -29,10 +32,6 @@ const $locales = derived([$dictionary], ([$dictionary]) =>
Object.keys($dictionary)
)

export {
$dictionary,
$locales,
getDictionary,
hasLocaleDictionary,
addMessages,
}
$dictionary.subscribe(newDictionary => (dictionary = newDictionary))

export { $dictionary, $locales }
12 changes: 3 additions & 9 deletions src/client/stores/format.ts
Expand Up @@ -3,13 +3,7 @@ import { derived } from 'svelte/store'
import { Formatter, MessageObject } from '../types'
import { lookupMessage } from '../includes/lookup'
import { hasLocaleQueue } from '../includes/loaderQueue'
import {
getAllFallbackLocales,
capital,
upper,
lower,
title,
} from '../includes/utils'
import { capital, upper, lower, title } from '../includes/utils'
import {
getMessageFormatter,
getTimeFormatter,
Expand All @@ -18,7 +12,7 @@ import {
} from '../includes/formats'

import { getDictionary, $dictionary } from './dictionary'
import { getCurrentLocale, $locale } from './locale'
import { getCurrentLocale, getFallbacksOf, $locale } from './locale'

const formatMessage: Formatter = (id, options = {}) => {
if (typeof id === 'object') {
Expand All @@ -38,7 +32,7 @@ const formatMessage: Formatter = (id, options = {}) => {

if (!message) {
console.warn(
`[svelte-i18n] The message "${id}" was not found in "${getAllFallbackLocales(
`[svelte-i18n] The message "${id}" was not found in "${getFallbacksOf(
locale
).join('", "')}". ${
hasLocaleQueue(getCurrentLocale())
Expand Down
66 changes: 43 additions & 23 deletions src/client/stores/locale.ts
@@ -1,30 +1,54 @@
import { writable } from 'svelte/store'

import { getFallbackLocale, getAllFallbackLocales } from '../includes/utils'
import { flushQueue, hasLocaleQueue } from '../includes/loaderQueue'
import { getClientLocale } from '../includes/utils'
import { GetClientLocaleOptions } from '../types'

import { getDictionary } from './dictionary'
import { getAvailableLocale } from './dictionary'

let fallback: string = null
let current: string
const $locale = writable(null)

function getCurrentLocale() {
return current
export function getFallbackLocale() {
return fallback
}

export function setfallbackLocale(locale: string) {
fallback = locale
}

export function isFallbackLocaleOf(localeA: string, localeB: string) {
return localeB.indexOf(localeA) === 0
}

export function getFallbackOf(locale: string) {
const index = locale.lastIndexOf('-')
if (index > 0) return locale.slice(0, index)
if (fallback && !isFallbackLocaleOf(locale, fallback)) return fallback
return null
}

function getAvailableLocale(locale: string): string | null {
if (locale in getDictionary() || locale == null) return locale
return getAvailableLocale(getFallbackLocale(locale))
export function getFallbacksOf(locale: string): string[] {
const locales = locale
.split('-')
.map((_, i, arr) => arr.slice(0, i + 1).join('-'))

if (fallback != null && !isFallbackLocaleOf(locale, fallback)) {
return locales.concat(getFallbacksOf(fallback))
}
return locales
}

function getCurrentLocale() {
return current
}

function loadLocale(localeToLoad: string) {
return Promise.all(
getAllFallbackLocales(localeToLoad).map(localeItem =>
flushQueue(localeItem)
.then(() => [localeItem, { err: undefined }])
.catch(e => [localeItem, { err: e }])
)
)
export function setInitialLocale(options: GetClientLocaleOptions) {
if (typeof options.fallback === 'string') {
setfallbackLocale(options.fallback)
}
return $locale.set(getClientLocale(options))
}

$locale.subscribe((newLocale: string) => {
Expand All @@ -37,17 +61,13 @@ $locale.subscribe((newLocale: string) => {

const localeSet = $locale.set
$locale.set = (newLocale: string): void | Promise<void> => {
if (getAvailableLocale(newLocale)) {
if (hasLocaleQueue(newLocale)) {
return flushQueue(newLocale).then(() => localeSet(newLocale))
}
return localeSet(newLocale)
if (getAvailableLocale(newLocale) && hasLocaleQueue(newLocale)) {
return flushQueue(newLocale).then(() => localeSet(newLocale))
}

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

$locale.update = (fn: (locale: string) => void | Promise<void>) =>
localeSet(fn(current))

export { $locale, loadLocale, flushQueue, getCurrentLocale }
export { $locale, flushQueue, getCurrentLocale }
16 changes: 3 additions & 13 deletions test/client/index.test.ts
Expand Up @@ -4,13 +4,12 @@ import {
dictionary,
locale,
format,
getClientLocale,
addCustomFormats,
customFormats,
preloadLocale,
register,
waitLocale,
} from '../../src/client'
import { getClientLocale } from '../../src/client/includes/utils'

global.Intl = require('intl')

Expand Down Expand Up @@ -42,19 +41,9 @@ describe('locale', () => {
await locale.set('en-US')
expect(currentLocale).toBe('en-US')
})

it("should throw an error if locale doesn't exist", () => {
expect(() => locale.set('FOO')).toThrow()
})
})

describe('dictionary', () => {
it('load a locale and its derived locales if dictionary is a loader', async () => {
const loaded = await preloadLocale('pt-PT')
expect(loaded[0][0]).toEqual('pt')
expect(loaded[1][0]).toEqual('pt-PT')
})

it('load a partial dictionary and merge it with the existing one', async () => {
await locale.set('en')
register('en', () => import('../fixtures/partials/en.json'))
Expand Down Expand Up @@ -96,7 +85,8 @@ describe('formatting', () => {
expect(_({ id: 'switch.lang' })).toBe('Switch language')
})

it('should translate to passed locale', () => {
it('should translate to passed locale', async () => {
await waitLocale('pt-BR')
expect(_('switch.lang', { locale: 'pt' })).toBe('Trocar idioma')
})

Expand Down

0 comments on commit 64e69eb

Please sign in to comment.