Skip to content

Commit

Permalink
Merge pull request #4 from sveltekit-i18n/fix/non-standard-locales
Browse files Browse the repository at this point in the history
Non-standard locales
  • Loading branch information
jarda-svoboda committed Apr 6, 2022
2 parents a2175f0 + ee41138 commit 3eb75be
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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...`);
}
Expand Down
4 changes: 2 additions & 2 deletions tests/data/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')),
},
],
};
10 changes: 7 additions & 3 deletions tests/data/translations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@ 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({
common,
route1: route,
route2: route,
}),
cs: toDotNotation({
common: common_cs,
'zh-Hans': toDotNotation({
common: common_zhHans,
}),
ku: toDotNotation({
common: common_ku,
}),
}) as Translations.T;
File renamed without changes.
9 changes: 9 additions & 0 deletions tests/data/translations/zh-Hans/common.json
Original file line number Diff line number Diff line change
@@ -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;}}"
}
25 changes: 25 additions & 0 deletions tests/specs/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,31 @@ 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, translations } = new i18n({ loaders: [{ key: 'common', locale: `${nonStandardLocale}`.toUpperCase(), loader: () => import(`../data/translations/${nonStandardLocale}/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);

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 });

Expand Down

0 comments on commit 3eb75be

Please sign in to comment.