From 6da951c858881b09506a99885523684afcd5a3b5 Mon Sep 17 00:00:00 2001 From: Jayk <20700514+jayk09@users.noreply.github.com> Date: Thu, 7 Apr 2022 00:06:12 +0200 Subject: [PATCH 1/3] Fix non erroring locales --- src/utils.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/utils.ts b/src/utils.ts index 3089faf..87b6fcb 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -30,6 +30,8 @@ export const sanitizeLocales = (...locales: any[]): string[] | [] => { let current = `${locale}`.toLowerCase(); try { [current] = Intl.Collator.supportedLocalesOf(locale); + + if (!current) throw new Error(`[i18n]: '${locale}' is non-standard.`); } catch (error) { console.warn(`[i18n]: Non-standard locale provided: '${locale}'. Check your 'translations' and 'loaders' in i18n config...`); } From 04c1f68c2feb427aa46964b1f74cbb70debc3fc9 Mon Sep 17 00:00:00 2001 From: Jayk <20700514+jayk09@users.noreply.github.com> Date: Thu, 7 Apr 2022 00:21:34 +0200 Subject: [PATCH 2/3] Add tests --- tests/specs/index.spec.ts | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tests/specs/index.spec.ts b/tests/specs/index.spec.ts index ae42b9f..5cf07e5 100644 --- a/tests/specs/index.spec.ts +++ b/tests/specs/index.spec.ts @@ -117,6 +117,26 @@ describe('i18n instance', () => { const $locale = locale.get(); expect($locale).toBe(initLocale.toLocaleLowerCase()); }); + it('`locale` can be non-standard', async () => { + const nonStandardLocale = 'ku'; + const { loading, locale, locales, setRoute, initialized } = new i18n({ loaders: [{ key: 'common', locale: `${nonStandardLocale}`.toUpperCase(), loader: () => import('../data/translations/en/common.json') }], parser }); + await setRoute(''); + locale.set(nonStandardLocale); + + const $loading = loading.get(); + expect($loading).toBe(true); + + await loading.toPromise(); + + const $initialized = get(initialized); + expect($initialized).toBe(true); + + const $locale = locale.get(); + expect($locale).toBe(nonStandardLocale); + + const $locales = locales.get(); + expect($locales).toContainEqual(nonStandardLocale); + }); it('`getTranslationProps` method works', async () => { const { initialized, getTranslationProps } = new i18n({ loaders, parser }); From ee411386713ff105f73936302ce51080a24396be Mon Sep 17 00:00:00 2001 From: Jayk <20700514+jayk09@users.noreply.github.com> Date: Thu, 7 Apr 2022 00:31:15 +0200 Subject: [PATCH 3/3] Update tests --- tests/data/index.ts | 4 ++-- tests/data/translations/index.ts | 10 +++++++--- tests/data/translations/{cs => ku}/common.json | 0 tests/data/translations/zh-Hans/common.json | 9 +++++++++ tests/specs/index.spec.ts | 7 ++++++- 5 files changed, 24 insertions(+), 6 deletions(-) rename tests/data/translations/{cs => ku}/common.json (100%) create mode 100644 tests/data/translations/zh-Hans/common.json diff --git a/tests/data/index.ts b/tests/data/index.ts index d628b92..5652765 100644 --- a/tests/data/index.ts +++ b/tests/data/index.ts @@ -26,8 +26,8 @@ export const CONFIG: Config.T = { }, { key: 'common', - locale: 'cs', - loader: async () => (import('../data/translations/cs/common.json')), + locale: 'zh-Hans', + loader: async () => (import('../data/translations/zh-Hans/common.json')), }, ], }; \ No newline at end of file diff --git a/tests/data/translations/index.ts b/tests/data/translations/index.ts index 1684b65..b812014 100644 --- a/tests/data/translations/index.ts +++ b/tests/data/translations/index.ts @@ -3,7 +3,8 @@ import type { Translations } from '../../../src/types'; import * as common from './en/common.json'; import * as route from './en/route.json'; -import * as common_cs from './cs/common.json'; +import * as common_ku from './ku/common.json'; +import * as common_zhHans from './zh-Hans/common.json'; export default ({ en: toDotNotation({ @@ -11,7 +12,10 @@ export default ({ route1: route, route2: route, }), - cs: toDotNotation({ - common: common_cs, + 'zh-Hans': toDotNotation({ + common: common_zhHans, + }), + ku: toDotNotation({ + common: common_ku, }), }) as Translations.T; \ No newline at end of file diff --git a/tests/data/translations/cs/common.json b/tests/data/translations/ku/common.json similarity index 100% rename from tests/data/translations/cs/common.json rename to tests/data/translations/ku/common.json diff --git a/tests/data/translations/zh-Hans/common.json b/tests/data/translations/zh-Hans/common.json new file mode 100644 index 0000000..3cf6df0 --- /dev/null +++ b/tests/data/translations/zh-Hans/common.json @@ -0,0 +1,9 @@ +{ + "no_placeholder": "NO_PLACEHOLDER", + "placeholder": "VALUES: {{value}}, {{value;}}, {{ value }}, {{ value; }}", + "modifier_test": "VALUES: {{value; option1:VALUE1; option2:VALUE2; default:DEFAULT VALUE;}}, {{ value ; option1 : VALUE1 ; option2 : VALUE2 ; default : DEFAULT VALUE ;}}", + "custom_modifier": "{{date:date;}}", + "modifier_number": "{{value:number;}}", + "modifier_date": "{{value:date;}}", + "modifier_ago": "{{value:ago;}}" +} \ No newline at end of file diff --git a/tests/specs/index.spec.ts b/tests/specs/index.spec.ts index 5cf07e5..1bc4ea0 100644 --- a/tests/specs/index.spec.ts +++ b/tests/specs/index.spec.ts @@ -119,7 +119,7 @@ describe('i18n instance', () => { }); it('`locale` can be non-standard', async () => { const nonStandardLocale = 'ku'; - const { loading, locale, locales, setRoute, initialized } = new i18n({ loaders: [{ key: 'common', locale: `${nonStandardLocale}`.toUpperCase(), loader: () => import('../data/translations/en/common.json') }], parser }); + const { loading, locale, locales, setRoute, initialized, translations } = new i18n({ loaders: [{ key: 'common', locale: `${nonStandardLocale}`.toUpperCase(), loader: () => import(`../data/translations/${nonStandardLocale}/common.json`) }], parser }); await setRoute(''); locale.set(nonStandardLocale); @@ -136,6 +136,11 @@ describe('i18n instance', () => { const $locales = locales.get(); expect($locales).toContainEqual(nonStandardLocale); + + const $translations = translations.get(); + expect($translations[nonStandardLocale]).toEqual( + expect.objectContaining(filterTranslationKeys(TRANSLATIONS[nonStandardLocale], ['common'])), + ); }); it('`getTranslationProps` method works', async () => { const { initialized, getTranslationProps } = new i18n({ loaders, parser });