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 Jan 26, 2024
1 parent 70c7875 commit a26ee50
Show file tree
Hide file tree
Showing 5 changed files with 337 additions and 80 deletions.
87 changes: 87 additions & 0 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,3 +308,90 @@ describe('i18n', () => {
expect(logger.log).toHaveBeenCalledTimes(callsLength + 1);
});
});

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 a26ee50

Please sign in to comment.