mirror of
https://github.com/goharbor/harbor.git
synced 2024-11-23 02:35:17 +01:00
Merge pull request #2264 from steven-zou/master
Use local json loader for ngx-translate
This commit is contained in:
commit
68cc911373
@ -9,7 +9,7 @@
|
|||||||
"test:once": "karma start karma.conf.js --single-run",
|
"test:once": "karma start karma.conf.js --single-run",
|
||||||
"pree2e": "webdriver-manager update",
|
"pree2e": "webdriver-manager update",
|
||||||
"e2e": "protractor",
|
"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",
|
"copy": "copyfiles -f LICENSE AUTHORS pkg/package.json dist",
|
||||||
"transpile": "ngc -p tsconfig.json",
|
"transpile": "ngc -p tsconfig.json",
|
||||||
"package": "rollup -c",
|
"package": "rollup -c",
|
||||||
|
@ -100,6 +100,7 @@ export function initConfig(translateService: TranslateService, config: IServiceC
|
|||||||
}
|
}
|
||||||
|
|
||||||
translateService.use(selectedLang);
|
translateService.use(selectedLang);
|
||||||
|
console.log('initConfig => ', translateService.currentLang);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
{
|
export const EN_US_LANG: any = {
|
||||||
"APP_TITLE": {
|
"APP_TITLE": {
|
||||||
"VMW_HARBOR": "VMware Harbor",
|
"VMW_HARBOR": "VMware Harbor",
|
||||||
"HARBOR": "Harbor",
|
"HARBOR": "Harbor",
|
||||||
@ -445,4 +445,4 @@
|
|||||||
"SERVER_ERROR": "We are unable to perform your action because internal server errors have occurred.",
|
"SERVER_ERROR": "We are unable to perform your action because internal server errors have occurred.",
|
||||||
"INCONRRECT_OLD_PWD": "The old password is incorrect.",
|
"INCONRRECT_OLD_PWD": "The old password is incorrect.",
|
||||||
"UNKNOWN": "n/a"
|
"UNKNOWN": "n/a"
|
||||||
}
|
};
|
@ -1,4 +1,4 @@
|
|||||||
{
|
export const ES_ES_LANG: any = {
|
||||||
"APP_TITLE": {
|
"APP_TITLE": {
|
||||||
"VMW_HARBOR": "VMware Harbor",
|
"VMW_HARBOR": "VMware Harbor",
|
||||||
"HARBOR": "Harbor",
|
"HARBOR": "Harbor",
|
||||||
@ -444,4 +444,4 @@
|
|||||||
"SERVER_ERROR": "No hemos podido llevar a cabo la acción debido a un error interno.",
|
"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.",
|
"INCONRRECT_OLD_PWD": "La contraseña antigua no es correcta.",
|
||||||
"UNKNOWN": "n/a"
|
"UNKNOWN": "n/a"
|
||||||
}
|
};
|
@ -1,4 +1,4 @@
|
|||||||
{
|
export const ZH_CN_LANG: any = {
|
||||||
"APP_TITLE": {
|
"APP_TITLE": {
|
||||||
"VMW_HARBOR": "VMware Harbor",
|
"VMW_HARBOR": "VMware Harbor",
|
||||||
"HARBOR": "Harbor",
|
"HARBOR": "Harbor",
|
||||||
@ -445,4 +445,4 @@
|
|||||||
"SERVER_ERROR": "服务器出现内部错误,请求无法完成。",
|
"SERVER_ERROR": "服务器出现内部错误,请求无法完成。",
|
||||||
"INCONRRECT_OLD_PWD": "旧密码不正确。",
|
"INCONRRECT_OLD_PWD": "旧密码不正确。",
|
||||||
"UNKNOWN": "未知"
|
"UNKNOWN": "未知"
|
||||||
}
|
};
|
31
src/ui_ng/lib/src/i18n/local-json.loader.ts
Normal file
31
src/ui_ng/lib/src/i18n/local-json.loader.ts
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
40
src/ui_ng/lib/src/i18n/translate.spec.ts
Normal file
40
src/ui_ng/lib/src/i18n/translate.spec.ts
Normal 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('注册');
|
||||||
|
});
|
||||||
|
}));
|
||||||
|
|
||||||
|
});
|
@ -7,9 +7,14 @@ import { TranslateModule, TranslateLoader, TranslateService, MissingTranslationH
|
|||||||
import { MyMissingTranslationHandler } from '../i18n/missing-trans.handler';
|
import { MyMissingTranslationHandler } from '../i18n/missing-trans.handler';
|
||||||
import { TranslateHttpLoader } from '@ngx-translate/http-loader';
|
import { TranslateHttpLoader } from '@ngx-translate/http-loader';
|
||||||
import { Http } from '@angular/http';
|
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');
|
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({
|
TranslateModule.forRoot({
|
||||||
loader: {
|
loader: {
|
||||||
provide: TranslateLoader,
|
provide: TranslateLoader,
|
||||||
useFactory: (HttpLoaderFactory),
|
useFactory: (LocalJsonLoaderFactory)
|
||||||
deps: [Http]
|
|
||||||
},
|
},
|
||||||
missingTranslationHandler: {
|
missingTranslationHandler: {
|
||||||
provide: MissingTranslationHandler,
|
provide: MissingTranslationHandler,
|
||||||
|
Loading…
Reference in New Issue
Block a user