Merge pull request #2264 from steven-zou/master

Use local json loader for ngx-translate
This commit is contained in:
Steven Zou 2017-05-09 16:24:24 +08:00 committed by GitHub
commit 68cc911373
8 changed files with 86 additions and 10 deletions

View File

@ -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",

View File

@ -100,6 +100,7 @@ export function initConfig(translateService: TranslateService, config: IServiceC
}
translateService.use(selectedLang);
console.log('initConfig => ', translateService.currentLang);
};
}

View File

@ -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"
}
};

View File

@ -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"
}
};

View File

@ -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": "未知"
}
};

View File

@ -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<any> {
let dict: any = langs[lang] ? langs[lang] : {};
return Observable.of(dict);
}
}

View File

@ -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('注册');
});
}));
});

View File

@ -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,