#1145 Fix multi-language

This commit is contained in:
mauriziovitale84 2016-11-25 16:43:14 +00:00 committed by Mario Romano
parent 6006c026dc
commit 76a9fa41e6
5 changed files with 45 additions and 19 deletions

View File

@ -18,11 +18,11 @@
export class ComponentTranslationModel { export class ComponentTranslationModel {
name: string; name: string;
path: string; path: string;
json: string; json: string [];
constructor(obj?: any) { constructor(obj?: any) {
this.name = obj && obj.name; this.name = obj && obj.name;
this.path = obj && obj.path; this.path = obj && obj.path;
this.json = obj && obj.json || null; this.json = obj && obj.json || [];
} }
} }

View File

@ -16,6 +16,8 @@
*/ */
import { AlfrescoTranslationService } from '../services/AlfrescoTranslation.service'; import { AlfrescoTranslationService } from '../services/AlfrescoTranslation.service';
import { TranslateLoader } from 'ng2-translate/ng2-translate';
import { AlfrescoTranslationLoader } from '../services/AlfrescoTranslationLoader.service';
import { Injector } from '@angular/core'; import { Injector } from '@angular/core';
import { ResponseOptions, Response, XHRBackend, HttpModule } from '@angular/http'; import { ResponseOptions, Response, XHRBackend, HttpModule } from '@angular/http';
import { MockBackend, MockConnection } from '@angular/http/testing'; import { MockBackend, MockConnection } from '@angular/http/testing';
@ -36,7 +38,10 @@ describe('AlfrescoTranslationService', () => {
beforeEach(() => { beforeEach(() => {
TestBed.configureTestingModule({ TestBed.configureTestingModule({
imports: [HttpModule, TranslateModule.forRoot()], imports: [HttpModule, TranslateModule.forRoot({
provide: TranslateLoader,
useClass: AlfrescoTranslationLoader
})],
providers: [ providers: [
AlfrescoTranslationService, AlfrescoTranslationService,
{provide: XHRBackend, useClass: MockBackend} {provide: XHRBackend, useClass: MockBackend}
@ -46,6 +51,7 @@ describe('AlfrescoTranslationService', () => {
backend = injector.get(XHRBackend); backend = injector.get(XHRBackend);
alfrescoTranslationService = injector.get(AlfrescoTranslationService); alfrescoTranslationService = injector.get(AlfrescoTranslationService);
backend.connections.subscribe((c: MockConnection) => connection = c); backend.connections.subscribe((c: MockConnection) => connection = c);
alfrescoTranslationService.addTranslationFolder('fake-name', 'fake-path');
}); });
it('is defined', () => { it('is defined', () => {
@ -53,12 +59,20 @@ describe('AlfrescoTranslationService', () => {
expect(alfrescoTranslationService instanceof AlfrescoTranslationService).toBeTruthy(); expect(alfrescoTranslationService instanceof AlfrescoTranslationService).toBeTruthy();
}); });
it('should be able to get translations', () => { it('should be able to get translations of the KEY: TEST', () => {
alfrescoTranslationService.use('en');
alfrescoTranslationService.get('TEST').subscribe((res: string) => { alfrescoTranslationService.get('TEST').subscribe((res: string) => {
expect(res).toEqual('This is a test'); expect(res).toEqual('This is a test');
}); });
mockBackendResponse(connection, '{"TEST": "This is a test", "TEST2": "This is another test"}'); mockBackendResponse(connection, '{"TEST": "This is a test", "TEST2": "This is another test"}');
}); });
it('should be able to get translations of the KEY: TEST2', () => {
alfrescoTranslationService.get('TEST2').subscribe((res: string) => {
expect(res).toEqual('This is another test');
});
mockBackendResponse(connection, '{"TEST": "This is a test", "TEST2": "This is another test"}');
});
}); });

View File

@ -23,16 +23,18 @@ import { AlfrescoTranslationLoader } from './AlfrescoTranslationLoader.service';
@Injectable() @Injectable()
export class AlfrescoTranslationService { export class AlfrescoTranslationService {
userLang: string = 'en'; userLang: string = 'en';
customLoader: AlfrescoTranslationLoader;
constructor(public translate: TranslateService) { constructor(public translate: TranslateService) {
this.userLang = translate.getBrowserLang() || 'en'; this.userLang = translate.getBrowserLang() || 'en';
translate.setDefaultLang(this.userLang); translate.setDefaultLang(this.userLang);
this.customLoader = <AlfrescoTranslationLoader> this.translate.currentLoader;
this.customLoader.init(this.userLang);
} }
addTranslationFolder(name: string = '', path: string = '') { addTranslationFolder(name: string = '', path: string = '') {
let loader = <AlfrescoTranslationLoader> this.translate.currentLoader; if (!this.customLoader.existComponent(name)) {
if (!loader.existComponent(name)) { this.customLoader.addComponentList(name, path);
loader.addComponentList(name, path);
this.translate.getTranslation(this.userLang).subscribe( this.translate.getTranslation(this.userLang).subscribe(
() => { () => {
this.translate.use(this.userLang); this.translate.use(this.userLang);
@ -42,6 +44,7 @@ export class AlfrescoTranslationService {
} }
use(lang: string): Observable<any> { use(lang: string): Observable<any> {
this.customLoader.init(lang);
return this.translate.use(lang); return this.translate.use(lang);
} }

View File

@ -27,7 +27,7 @@ export class AlfrescoTranslationLoader implements TranslateLoader {
private prefix: string = 'i18n'; private prefix: string = 'i18n';
private suffix: string = '.json'; private suffix: string = '.json';
private _componentList: ComponentTranslationModel[] = []; private _componentList: ComponentTranslationModel[] = [];
private queue: string[] = []; private queue: string [][] = [];
constructor(private http: Http) { constructor(private http: Http) {
} }
@ -43,11 +43,11 @@ export class AlfrescoTranslationLoader implements TranslateLoader {
getComponentToFetch(lang: string) { getComponentToFetch(lang: string) {
let observableBatch = []; let observableBatch = [];
this._componentList.forEach((component) => { this._componentList.forEach((component) => {
if (!this.queue.find(x => x === component.name)) { if (!this.isComponentInQueue(lang, component.name)) {
this.queue.push(component.name); this.queue[lang].push(component.name);
observableBatch.push(this.http.get(`${component.path}/${this.prefix}/${lang}${this.suffix}`) observableBatch.push(this.http.get(`${component.path}/${this.prefix}/${lang}${this.suffix}`)
.map((res: Response) => { .map((res: Response) => {
component.json = res.json(); component.json[lang] = res.json();
}) })
.catch(() => { .catch(() => {
// Empty Observable just to go ahead // Empty Observable just to go ahead
@ -58,12 +58,22 @@ export class AlfrescoTranslationLoader implements TranslateLoader {
return observableBatch; return observableBatch;
} }
getFullTranslationJSON() { init(lang:string) {
if (this.queue[lang] === undefined) {
this.queue[lang] = [];
}
}
isComponentInQueue(lang:string, name: string) {
return this.queue[lang].find(x => x === name) ? true : false;
}
getFullTranslationJSON(lang: string) {
let fullTranslation: string = ''; let fullTranslation: string = '';
let cloneList = this._componentList.slice(0); let cloneList = this._componentList.slice(0);
cloneList.reverse().forEach((component) => { cloneList.reverse().forEach((component) => {
if (component.json !== undefined && component.json !== null) { if (component.json && component.json[lang]) {
fullTranslation += JSON.stringify(component.json); fullTranslation += JSON.stringify(component.json[lang]);
} }
}); });
if (fullTranslation !== '') { if (fullTranslation !== '') {
@ -78,7 +88,7 @@ export class AlfrescoTranslationLoader implements TranslateLoader {
if (observableBatch.length > 0) { if (observableBatch.length > 0) {
Observable.forkJoin(observableBatch).subscribe( Observable.forkJoin(observableBatch).subscribe(
() => { () => {
let fullTranslation = this.getFullTranslationJSON(); let fullTranslation = this.getFullTranslationJSON(lang);
if (fullTranslation) { if (fullTranslation) {
observer.next(fullTranslation); observer.next(fullTranslation);
} }
@ -88,11 +98,10 @@ export class AlfrescoTranslationLoader implements TranslateLoader {
console.error(err); console.error(err);
}); });
} else { } else {
let fullTranslation = this.getFullTranslationJSON(); let fullTranslation = this.getFullTranslationJSON(lang);
if (fullTranslation) { if (fullTranslation) {
observer.next(fullTranslation); observer.next(fullTranslation);
} }
observer.complete();
} }
}); });
} }

View File

@ -76,7 +76,7 @@ export class AlfrescoSearchAutocompleteComponent implements OnInit, OnChanges {
ngOnInit(): void { ngOnInit(): void {
if (this.translate) { if (this.translate) {
this.translate.addTranslationFolder('node_modules/ng2-alfresco-search/dist/src'); this.translate.addTranslationFolder('ng2-alfresco-search', 'node_modules/ng2-alfresco-search/dist/src');
} }
} }