From 5662f0401c759b542ea076c703aa3e7611f19fbd Mon Sep 17 00:00:00 2001 From: Christian Kaisermann Date: Thu, 9 Aug 2018 15:49:01 -0300 Subject: [PATCH] Handle nonexistent locale switch --- package.json | 2 +- src/index.js | 36 ++++++++++++++++++++++-------------- test/index.test.js | 17 +++++++++++++++-- 3 files changed, 38 insertions(+), 17 deletions(-) diff --git a/package.json b/package.json index 1207965..836179a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "svelte-i18n", - "version": "0.0.2", + "version": "0.0.3", "license": "MIT", "main": "dist/i18n.js", "module": "dist/i18n.m.js", diff --git a/src/index.js b/src/index.js index de165d3..f9af24d 100644 --- a/src/index.js +++ b/src/index.js @@ -5,9 +5,9 @@ import Formatter from './formatter' export { capital, title, upper, lower } -export function i18n(store, { dictionary }) { +export function i18n(store, { dictionary: initialDictionary }) { const formatter = new Formatter() - let dictionaries = {} + let dictionary = {} let currentLocale const getLocalizedMessage = ( @@ -16,7 +16,7 @@ export function i18n(store, { dictionary }) { locale = currentLocale, transformers = undefined, ) => { - let message = resolvePath(dictionaries[locale], path) + let message = resolvePath(dictionary[locale], path) if (!message) return path @@ -57,25 +57,33 @@ export function i18n(store, { dictionary }) { }, } + store.on('locale', newLocale => { + if (!Object.keys(dictionary).includes(newLocale)) { + console.error(`[svelte-i18n] Couldn't find the "${newLocale}" locale.`) + return + } + currentLocale = newLocale + const _ = getLocalizedMessage + + _.upper = utilities.upper + _.lower = utilities.lower + _.title = utilities.title + _.capital = utilities.capital + _.plural = utilities.plural + + store.set({ locale: newLocale, _ }) + }) + store.i18n = { setLocale(locale) { store.fire('locale', locale) }, extendDictionary(...list) { - dictionaries = deepmerge.all([dictionaries, ...list]) + dictionary = deepmerge.all([dictionary, ...list]) }, } - store.i18n.extendDictionary(dictionary) - - store.on('locale', newLocale => { - currentLocale = newLocale - const _ = getLocalizedMessage - - Object.assign(_, utilities) - - store.set({ locale: newLocale, _ }) - }) + store.i18n.extendDictionary(initialDictionary) return store } diff --git a/test/index.test.js b/test/index.test.js index 1f07fa3..f747a0a 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -42,6 +42,14 @@ describe('Utilities', () => { }) describe('Localization', () => { + beforeEach(() => { + console.error = jest.fn() + }) + + afterEach(() => { + console.error.mockRestore() + }) + it('should start with a clean store', () => { const { _, locale } = store.get() expect(locale).toBeFalsy() @@ -49,10 +57,10 @@ describe('Localization', () => { }) it('should change the locale after a "locale" store event', () => { - store.fire('locale', 'en') + store.fire('locale', 'pt-br') const { locale, _ } = store.get() - expect(locale).toBe('en') + expect(locale).toBe('pt-br') expect(_).toBeInstanceOf(Function) }) @@ -65,6 +73,11 @@ describe('Localization', () => { expect(locale).toBe('pt-br') }) + it('should handle nonexistent locale', () => { + expect(store.i18n.setLocale('foo')) + expect(console.error).toHaveBeenCalledTimes(1) + }) + it('should return the message id when no message identified by it was found', () => { store.i18n.setLocale('pt-br') const { _ } = store.get()