Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add data, fallbackLang & lang options as constructor arguments #40

Merged
merged 13 commits into from
Jan 26, 2024
Merged
92 changes: 92 additions & 0 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,3 +308,95 @@ describe('i18n', () => {
expect(logger.log).toHaveBeenCalledTimes(callsLength + 1);
});
});

describe('constructor options', () => {
it('should throw an error in case of languages absence', () => {
i18n = new I18N();
expect(() => i18n.i18n('notification', 'title')).toThrow();
});

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
Loading