From 28eafd2f303113ac5f3ffab54ba01105258e0635 Mon Sep 17 00:00:00 2001 From: Steven Zou Date: Tue, 9 May 2017 16:09:03 +0800 Subject: [PATCH] Use local json loader for ngx-translate --- src/ui_ng/lib/package.json | 2 +- src/ui_ng/lib/src/harbor-library.module.ts | 1 + .../lang/{en-us-lang.json => en-us-lang.ts} | 4 +- .../lang/{es-es-lang.json => es-es-lang.ts} | 4 +- .../lang/{zh-cn-lang.json => zh-cn-lang.ts} | 4 +- src/ui_ng/lib/src/i18n/local-json.loader.ts | 31 ++++++++++++++ src/ui_ng/lib/src/i18n/translate.spec.ts | 40 +++++++++++++++++++ src/ui_ng/lib/src/shared/shared.module.ts | 10 +++-- 8 files changed, 86 insertions(+), 10 deletions(-) rename src/ui_ng/lib/src/i18n/lang/{en-us-lang.json => en-us-lang.ts} (99%) rename src/ui_ng/lib/src/i18n/lang/{es-es-lang.json => es-es-lang.ts} (99%) rename src/ui_ng/lib/src/i18n/lang/{zh-cn-lang.json => zh-cn-lang.ts} (99%) create mode 100644 src/ui_ng/lib/src/i18n/local-json.loader.ts create mode 100644 src/ui_ng/lib/src/i18n/translate.spec.ts diff --git a/src/ui_ng/lib/package.json b/src/ui_ng/lib/package.json index b88ac6956..7ca28f2c2 100644 --- a/src/ui_ng/lib/package.json +++ b/src/ui_ng/lib/package.json @@ -9,7 +9,7 @@ "test:once": "karma start karma.conf.js --single-run", "pree2e": "webdriver-manager update", "e2e": "protractor", - "cleanup": "rimraf dist/bundles dist/src dist/index.d.ts dist/index.ngsummary.json dist/index.metadata.json dist/index.js dist/index.js.map dist/LICENSE dist/AUTHORS", + "cleanup": "rimraf dist", "copy": "copyfiles -f LICENSE AUTHORS pkg/package.json dist", "transpile": "ngc -p tsconfig.json", "package": "rollup -c", diff --git a/src/ui_ng/lib/src/harbor-library.module.ts b/src/ui_ng/lib/src/harbor-library.module.ts index b0b715014..618896b3c 100644 --- a/src/ui_ng/lib/src/harbor-library.module.ts +++ b/src/ui_ng/lib/src/harbor-library.module.ts @@ -100,6 +100,7 @@ export function initConfig(translateService: TranslateService, config: IServiceC } translateService.use(selectedLang); + console.log('initConfig => ', translateService.currentLang); }; } diff --git a/src/ui_ng/lib/src/i18n/lang/en-us-lang.json b/src/ui_ng/lib/src/i18n/lang/en-us-lang.ts similarity index 99% rename from src/ui_ng/lib/src/i18n/lang/en-us-lang.json rename to src/ui_ng/lib/src/i18n/lang/en-us-lang.ts index 749021087..1b4178daf 100644 --- a/src/ui_ng/lib/src/i18n/lang/en-us-lang.json +++ b/src/ui_ng/lib/src/i18n/lang/en-us-lang.ts @@ -1,4 +1,4 @@ -{ +export const EN_US_LANG: any = { "APP_TITLE": { "VMW_HARBOR": "VMware Harbor", "HARBOR": "Harbor", @@ -445,4 +445,4 @@ "SERVER_ERROR": "We are unable to perform your action because internal server errors have occurred.", "INCONRRECT_OLD_PWD": "The old password is incorrect.", "UNKNOWN": "n/a" -} \ No newline at end of file +}; \ No newline at end of file diff --git a/src/ui_ng/lib/src/i18n/lang/es-es-lang.json b/src/ui_ng/lib/src/i18n/lang/es-es-lang.ts similarity index 99% rename from src/ui_ng/lib/src/i18n/lang/es-es-lang.json rename to src/ui_ng/lib/src/i18n/lang/es-es-lang.ts index 087df658b..bc58a1846 100644 --- a/src/ui_ng/lib/src/i18n/lang/es-es-lang.json +++ b/src/ui_ng/lib/src/i18n/lang/es-es-lang.ts @@ -1,4 +1,4 @@ -{ +export const ES_ES_LANG: any = { "APP_TITLE": { "VMW_HARBOR": "VMware Harbor", "HARBOR": "Harbor", @@ -444,4 +444,4 @@ "SERVER_ERROR": "No hemos podido llevar a cabo la acción debido a un error interno.", "INCONRRECT_OLD_PWD": "La contraseña antigua no es correcta.", "UNKNOWN": "n/a" -} \ No newline at end of file +}; \ No newline at end of file diff --git a/src/ui_ng/lib/src/i18n/lang/zh-cn-lang.json b/src/ui_ng/lib/src/i18n/lang/zh-cn-lang.ts similarity index 99% rename from src/ui_ng/lib/src/i18n/lang/zh-cn-lang.json rename to src/ui_ng/lib/src/i18n/lang/zh-cn-lang.ts index fae39b8c0..f97d39931 100644 --- a/src/ui_ng/lib/src/i18n/lang/zh-cn-lang.json +++ b/src/ui_ng/lib/src/i18n/lang/zh-cn-lang.ts @@ -1,4 +1,4 @@ -{ +export const ZH_CN_LANG: any = { "APP_TITLE": { "VMW_HARBOR": "VMware Harbor", "HARBOR": "Harbor", @@ -445,4 +445,4 @@ "SERVER_ERROR": "服务器出现内部错误,请求无法完成。", "INCONRRECT_OLD_PWD": "旧密码不正确。", "UNKNOWN": "未知" -} \ No newline at end of file +}; \ No newline at end of file diff --git a/src/ui_ng/lib/src/i18n/local-json.loader.ts b/src/ui_ng/lib/src/i18n/local-json.loader.ts new file mode 100644 index 000000000..cc2048af6 --- /dev/null +++ b/src/ui_ng/lib/src/i18n/local-json.loader.ts @@ -0,0 +1,31 @@ +import { TranslateLoader } from '@ngx-translate/core'; +import 'rxjs/add/observable/of'; + +import { Observable } from 'rxjs/Observable'; +import { EN_US_LANG } from './lang/en-us-lang'; +import { ES_ES_LANG } from './lang/es-es-lang'; +import { ZH_CN_LANG } from './lang/zh-cn-lang'; + + +/** + * Define language mapping + */ +export const langs: { [key: string]: any } = { + "en-us": EN_US_LANG, + "es-es": ES_ES_LANG, + "zh-cn": ZH_CN_LANG +}; + +/** + * Declare a translation loader with local json object + * + * @export + * @class TranslatorJsonLoader + * @extends {TranslateLoader} + */ +export class TranslatorJsonLoader extends TranslateLoader { + getTranslation(lang: string): Observable { + let dict: any = langs[lang] ? langs[lang] : {}; + return Observable.of(dict); + } +} \ No newline at end of file diff --git a/src/ui_ng/lib/src/i18n/translate.spec.ts b/src/ui_ng/lib/src/i18n/translate.spec.ts new file mode 100644 index 000000000..549cd71b9 --- /dev/null +++ b/src/ui_ng/lib/src/i18n/translate.spec.ts @@ -0,0 +1,40 @@ +import { TestBed, inject } from '@angular/core/testing'; +import { SharedModule } from '../shared/shared.module'; +import { TranslateService } from '@ngx-translate/core'; +import { DEFAULT_LANG } from '../utils'; + +describe('TranslateService', () => { + beforeEach(() => { + TestBed.configureTestingModule({ + imports: [ + SharedModule + ], + providers: [] + }); + }); + + it('should be initialized', inject([TranslateService], (service: TranslateService) => { + expect(service).toBeTruthy(); + })); + + it('should use the specified lang', inject([TranslateService], (service: TranslateService) => { + service.use(DEFAULT_LANG).subscribe(() => { + expect(service.currentLang).toEqual(DEFAULT_LANG); + }); + })); + + it('should translate key to text [en-us]', inject([TranslateService], (service: TranslateService) => { + service.use(DEFAULT_LANG); + service.get('APP_TITLE.HARBOR').subscribe(text => { + expect(text).toEqual('Harbor'); + }); + })); + + it('should translate key to text [zh-cn]', inject([TranslateService], (service: TranslateService) => { + service.use('zh-cn'); + service.get('SIGN_UP.TITLE').subscribe(text => { + expect(text).toEqual('注册'); + }); + })); + +}); diff --git a/src/ui_ng/lib/src/shared/shared.module.ts b/src/ui_ng/lib/src/shared/shared.module.ts index cfbe6bc71..70f00d7ea 100644 --- a/src/ui_ng/lib/src/shared/shared.module.ts +++ b/src/ui_ng/lib/src/shared/shared.module.ts @@ -7,9 +7,14 @@ import { TranslateModule, TranslateLoader, TranslateService, MissingTranslationH import { MyMissingTranslationHandler } from '../i18n/missing-trans.handler'; import { TranslateHttpLoader } from '@ngx-translate/http-loader'; import { Http } from '@angular/http'; +import { TranslatorJsonLoader } from '../i18n/local-json.loader'; -export function HttpLoaderFactory(http: Http) { +/*export function HttpLoaderFactory(http: Http) { return new TranslateHttpLoader(http, 'i18n/lang/', '-lang.json'); +}*/ + +export function LocalJsonLoaderFactory() { + return new TranslatorJsonLoader(); } /** @@ -28,8 +33,7 @@ export function HttpLoaderFactory(http: Http) { TranslateModule.forRoot({ loader: { provide: TranslateLoader, - useFactory: (HttpLoaderFactory), - deps: [Http] + useFactory: (LocalJsonLoaderFactory) }, missingTranslationHandler: { provide: MissingTranslationHandler,