Skip to content

Commit

Permalink
feat: add data, fallbackLang & lang options as constructor arguments (#…
Browse files Browse the repository at this point in the history
…40)

* feat: add data, defaultLang & lang options as constructor arguments

* refactor: rename defaultLang to fallbackLang

* refactor: throw an error in case of languages absence

* refactor: rework i18n method

* refactor: change ErrorCode naming

* fix: use object properties instead strings in mapErrorCodeToMessage switch cases

* fix: review fixes

* refactor: mark code property in mapErrorCodeToMessage as required

* fix: do not throw an error in case of NoLanguageData

* fix: remove fallbackLamg from getTranslationData method

* fix: remove none actual test

* fix: i18n method fixes
  • Loading branch information
korvin89 authored and vladimirfilosof committed Mar 21, 2024
1 parent cf83e1d commit 2e832b4
Show file tree
Hide file tree
Showing 5 changed files with 365 additions and 68 deletions.
87 changes: 87 additions & 0 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -472,3 +472,90 @@ describe('i18n', () => {
})).toBe('11 пользователей');
});
});

describe('constructor options', () => {
describe('lang', () => {
it('should return translation [set lang via options.lang]', () => {
i18n = new I18N({lang: 'en'});
i18n.registerKeyset('en', 'notification', {
title: 'New version'
});
expect(i18n.i18n('notification', 'title')).toBe('New version');
});

it('should return translation [set lang via i18n.setLang]', () => {
i18n.setLang('en');
i18n.registerKeyset('en', 'notification', {
title: 'New version'
});
expect(i18n.i18n('notification', 'title')).toBe('New version');
});
});

describe('data', () => {
it('should return translation [set data via options.data]', () => {
i18n = new I18N({lang: 'en', data: {en: {notification: {title: 'New version'}}}});
expect(i18n.i18n('notification', 'title')).toBe('New version');
});

it('should return translation [set data via i18n.registerKeyset]', () => {
i18n = new I18N({lang: 'en'});
i18n.registerKeyset('en', 'notification', {
title: 'New version'
});
expect(i18n.i18n('notification', 'title')).toBe('New version');
});
});

describe('fallbackLang', () => {
it('should return translation from default language in case of language data absence', () => {
i18n = new I18N({
lang: 'sr',
fallbackLang: 'en',
data: {en: {notification: {title: 'New version'}}},
});
expect(i18n.i18n('notification', 'title')).toBe('New version');
});

it('should return fallback from default language in case of language data absence', () => {
i18n = new I18N({lang: 'sr', fallbackLang: 'en', data: {en: {notification: {}}}});
expect(i18n.i18n('notification', 'title')).toBe('title');
});

it('should return translation from default language in case of empty keyset', () => {
i18n = new I18N({
lang: 'sr',
fallbackLang: 'en',
data: {
en: {notification: {title: 'New version'}},
sr: {notification: {}},
},
});
expect(i18n.i18n('notification', 'title')).toBe('New version');
});

it('should return translation from default language in case of missing key', () => {
i18n = new I18N({
lang: 'sr',
fallbackLang: 'en',
data: {
en: {notification: {title: 'New version'}},
sr: {notification: {hey: 'Zdravo!'}},
},
});
expect(i18n.i18n('notification', 'title')).toBe('New version');
});

it('should return fallback from default language in case of missing key', () => {
i18n = new I18N({
lang: 'sr',
fallbackLang: 'en',
data: {
en: {notification: {hey: 'Hello!'}},
sr: {notification: {hey: 'Zdravo!'}},
},
});
expect(i18n.i18n('notification', 'title')).toBe('title');
});
});
});
Loading

0 comments on commit 2e832b4

Please sign in to comment.