Skip to content

Commit

Permalink
Merge pull request #56 from systelab/bugfix-52
Browse files Browse the repository at this point in the history
Warning: Entry point 'systelab-translate' contains deep imports into …
  • Loading branch information
tlainez committed Jan 31, 2022
2 parents 8827a82 + 6b2ce4d commit e092013
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 34 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,5 @@ IE11 support has been deprecated due to the upgrade to Angular 12

Use of [Ivy](https://angular.io/guide/ivy), applications that uses this library have to use Angular 12 and Ivy rendering.


Added --noImplicitOverride flag to allow override methods and get error for unintentionally overrides https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-3.html#override-and-the---noimplicitoverride-flag
34 changes: 0 additions & 34 deletions projects/systelab-translate/src/lib/i18n.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,6 @@ import { TranslateService } from '@ngx-translate/core';

import { DecimalFormat } from './decimal-format/decimal-format'
import { DateUtil } from './date-util/date-util';
import { registerLocaleData } from '@angular/common';
import localeCa from '@angular/common/locales/ca';
import localeDe from '@angular/common/locales/de';
import localeEn from '@angular/common/locales/en';
import localeEs from '@angular/common/locales/es';
import localeFr from '@angular/common/locales/fr';
import localeGl from '@angular/common/locales/gl';
import localeIt from '@angular/common/locales/it';
import localeJa from '@angular/common/locales/ja';
import localeKo from '@angular/common/locales/ko';
import localeLt from '@angular/common/locales/lt';
import localeNl from '@angular/common/locales/nl';
import localePt from '@angular/common/locales/pt';
import localeRu from '@angular/common/locales/ru';
import localeSk from '@angular/common/locales/sk';
import localeTh from '@angular/common/locales/th';
import localeZh from '@angular/common/locales/zh';

@Injectable({
providedIn: 'root'
Expand All @@ -33,23 +16,6 @@ export class I18nService {
protected dateUtil: DateUtil;

constructor(protected translateService: TranslateService) {
//By default, Angular5 only contains locale data for en-US.
registerLocaleData(localeCa);
registerLocaleData(localeDe);
registerLocaleData(localeEn);
registerLocaleData(localeEs);
registerLocaleData(localeFr);
registerLocaleData(localeGl);
registerLocaleData(localeIt);
registerLocaleData(localeJa);
registerLocaleData(localeKo);
registerLocaleData(localeLt);
registerLocaleData(localeNl);
registerLocaleData(localePt);
registerLocaleData(localeRu);
registerLocaleData(localeSk);
registerLocaleData(localeTh);
registerLocaleData(localeZh);
this.translateService.setDefaultLang('en-US');
this.dateUtil = new DateUtil('en-US');
}
Expand Down
141 changes: 141 additions & 0 deletions projects/systelab-translate/src/test/i18n.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ describe('Translate Service', () => {
service = undefined;
});

it('Is defined', () => {
expect(service).toBeDefined();
expect(service instanceof I18nService).toBeTruthy();
});

it('Check the translation of a key in english', (done) => {
service.use('en-US')
.subscribe(() => {
Expand Down Expand Up @@ -135,6 +140,26 @@ describe('Translate Service', () => {
});
});

it('Check a key added on the fly with one parameter', (done) => {
service.use('es-ES')
.subscribe(() => {
service.setStaticBundles({ USER_AGE_AND_GENDER: 'User gender is {{USER_GENDER}}' });
expect(service.instant('USER_AGE_AND_GENDER', {USER_GENDER: 'Male'} ))
.toBe('User gender is Male');
done();
});
});

it('Check a key added on the fly with empty parameters', (done) => {
service.use('es-ES')
.subscribe(() => {
service.setStaticBundles({ USER_AGE_AND_GENDER: 'User gender is {{USER_GENDER}}' });
expect(service.instant('USER_AGE_AND_GENDER', '' ))
.toBe('User gender is ');
done();
});
});

it('Check a key with multiple named not sorted parameters', (done) => {
service.use('es-ES')
.subscribe(() => {
Expand Down Expand Up @@ -177,4 +202,120 @@ describe('Translate Service', () => {
});
});

it('Should be able to get an array translations', (done) => {
const translations = { key1: 'Value1', key2: 'Value2' };

service.use('es-ES')
.subscribe(() => {
service.setTranslation('es-ES', translations);
expect(service.instant(['key1', 'key2']))
.toEqual(translations);
done();
});
});

it('Should return an empty value', (done) => {
service.use('en-US')
.subscribe(() => {
service.setTranslation('en-US', { empty: '' });
expect(service.instant('empty'))
.toBe('');
done();
});
});

it('Check get the current language', (done) => {
service.use('es-ES')
.subscribe(() => {
expect(service.getCurrentLanguage())
.toBe('es-ES');
done();
});
});

it('Should be able to get the browserLang', (done) => {
service.use('es-ES')
.subscribe(() => {
const browserLang = service.getBrowserLang();
expect(typeof browserLang === 'string')
.toBeTruthy();
done();
});
});

it('should throw an error if the key is not provided', (done) => {
service.use('es-ES')
.subscribe(() => {
service.setStaticBundles({ COMMON_DAY: 'value' });
expect(() => service.instant('',))
.toThrow(new Error('Parameter "key" required'));
expect(() => service.instant(undefined as any,))
.toThrow(new Error('Parameter "key" required'));
expect(() => service.instant(null as any,))
.toThrow(new Error('Parameter "key" required'));
done();
});
});

it('should format a number based on the decimal format', (done) => {
service.use('en-US')
.subscribe(() => {
const myNumber = 3.1;
expect(service.formatNumber(myNumber, '#.00'))
.toBe('3.10');
done();
});
});

it('should format a number based on the decimal format and locale parameters', (done) => {
service.use('es-ES')
.subscribe(() => {
const myNumber = 3.1;
expect(service.formatNumber(myNumber, '#.00',true))
.toBe('3,10');
done();
});
});

it('Should allow to change the default language', (done) => {
service.use('en-US')
.subscribe(() => {
expect(service.instant('COMMON_DAY'))
.toBe('Day');
done();
});
});

it('Should merge translations using appendTranslation method', (done) => {
service.use('en-GB')
.subscribe(() => {
service.appendTranslation('en-GB', {test: {sub1: 'value1'}});
service.appendTranslation('en-GB', {test: {sub2: 'value2'}});
expect(service.instant('test'))
.toEqual({sub1: 'value1', sub2: 'value2'});
done();
});
});

it('Should override translations using setTranslation method', (done) => {
service.use('en-GB')
.subscribe(() => {
service.setTranslation('en-GB', {test: {sub1: 'value1'}});
service.setTranslation('en-GB', {test: {sub2: 'value2'}});
expect(service.instant('test'))
.toEqual({sub2: 'value2'});
done();
});
});

it('Should be able to get translations with nested keys', (done) => {
service.use('es-ES')
.subscribe(() => {
service.setTranslation('es-ES', {test: {test1: 'This is a test'}, test2: {test2: 'This is another test'}});
expect(service.instant('test.test1'))
.toEqual('This is a test');
done();
});
});

});

0 comments on commit e092013

Please sign in to comment.