mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-05-19 17:14:57 +00:00
#1145 Fix multi-language
This commit is contained in:
parent
6006c026dc
commit
76a9fa41e6
@ -18,11 +18,11 @@
|
||||
export class ComponentTranslationModel {
|
||||
name: string;
|
||||
path: string;
|
||||
json: string;
|
||||
json: string [];
|
||||
|
||||
constructor(obj?: any) {
|
||||
this.name = obj && obj.name;
|
||||
this.path = obj && obj.path;
|
||||
this.json = obj && obj.json || null;
|
||||
this.json = obj && obj.json || [];
|
||||
}
|
||||
}
|
||||
|
@ -16,6 +16,8 @@
|
||||
*/
|
||||
|
||||
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 { ResponseOptions, Response, XHRBackend, HttpModule } from '@angular/http';
|
||||
import { MockBackend, MockConnection } from '@angular/http/testing';
|
||||
@ -36,7 +38,10 @@ describe('AlfrescoTranslationService', () => {
|
||||
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({
|
||||
imports: [HttpModule, TranslateModule.forRoot()],
|
||||
imports: [HttpModule, TranslateModule.forRoot({
|
||||
provide: TranslateLoader,
|
||||
useClass: AlfrescoTranslationLoader
|
||||
})],
|
||||
providers: [
|
||||
AlfrescoTranslationService,
|
||||
{provide: XHRBackend, useClass: MockBackend}
|
||||
@ -46,6 +51,7 @@ describe('AlfrescoTranslationService', () => {
|
||||
backend = injector.get(XHRBackend);
|
||||
alfrescoTranslationService = injector.get(AlfrescoTranslationService);
|
||||
backend.connections.subscribe((c: MockConnection) => connection = c);
|
||||
alfrescoTranslationService.addTranslationFolder('fake-name', 'fake-path');
|
||||
});
|
||||
|
||||
it('is defined', () => {
|
||||
@ -53,12 +59,20 @@ describe('AlfrescoTranslationService', () => {
|
||||
expect(alfrescoTranslationService instanceof AlfrescoTranslationService).toBeTruthy();
|
||||
});
|
||||
|
||||
it('should be able to get translations', () => {
|
||||
alfrescoTranslationService.use('en');
|
||||
it('should be able to get translations of the KEY: TEST', () => {
|
||||
alfrescoTranslationService.get('TEST').subscribe((res: string) => {
|
||||
expect(res).toEqual('This is a 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"}');
|
||||
});
|
||||
|
||||
});
|
||||
|
@ -23,16 +23,18 @@ import { AlfrescoTranslationLoader } from './AlfrescoTranslationLoader.service';
|
||||
@Injectable()
|
||||
export class AlfrescoTranslationService {
|
||||
userLang: string = 'en';
|
||||
customLoader: AlfrescoTranslationLoader;
|
||||
|
||||
constructor(public translate: TranslateService) {
|
||||
this.userLang = translate.getBrowserLang() || 'en';
|
||||
translate.setDefaultLang(this.userLang);
|
||||
this.customLoader = <AlfrescoTranslationLoader> this.translate.currentLoader;
|
||||
this.customLoader.init(this.userLang);
|
||||
}
|
||||
|
||||
addTranslationFolder(name: string = '', path: string = '') {
|
||||
let loader = <AlfrescoTranslationLoader> this.translate.currentLoader;
|
||||
if (!loader.existComponent(name)) {
|
||||
loader.addComponentList(name, path);
|
||||
if (!this.customLoader.existComponent(name)) {
|
||||
this.customLoader.addComponentList(name, path);
|
||||
this.translate.getTranslation(this.userLang).subscribe(
|
||||
() => {
|
||||
this.translate.use(this.userLang);
|
||||
@ -42,6 +44,7 @@ export class AlfrescoTranslationService {
|
||||
}
|
||||
|
||||
use(lang: string): Observable<any> {
|
||||
this.customLoader.init(lang);
|
||||
return this.translate.use(lang);
|
||||
}
|
||||
|
||||
|
@ -27,7 +27,7 @@ export class AlfrescoTranslationLoader implements TranslateLoader {
|
||||
private prefix: string = 'i18n';
|
||||
private suffix: string = '.json';
|
||||
private _componentList: ComponentTranslationModel[] = [];
|
||||
private queue: string[] = [];
|
||||
private queue: string [][] = [];
|
||||
|
||||
constructor(private http: Http) {
|
||||
}
|
||||
@ -43,11 +43,11 @@ export class AlfrescoTranslationLoader implements TranslateLoader {
|
||||
getComponentToFetch(lang: string) {
|
||||
let observableBatch = [];
|
||||
this._componentList.forEach((component) => {
|
||||
if (!this.queue.find(x => x === component.name)) {
|
||||
this.queue.push(component.name);
|
||||
if (!this.isComponentInQueue(lang, component.name)) {
|
||||
this.queue[lang].push(component.name);
|
||||
observableBatch.push(this.http.get(`${component.path}/${this.prefix}/${lang}${this.suffix}`)
|
||||
.map((res: Response) => {
|
||||
component.json = res.json();
|
||||
component.json[lang] = res.json();
|
||||
})
|
||||
.catch(() => {
|
||||
// Empty Observable just to go ahead
|
||||
@ -58,12 +58,22 @@ export class AlfrescoTranslationLoader implements TranslateLoader {
|
||||
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 cloneList = this._componentList.slice(0);
|
||||
cloneList.reverse().forEach((component) => {
|
||||
if (component.json !== undefined && component.json !== null) {
|
||||
fullTranslation += JSON.stringify(component.json);
|
||||
if (component.json && component.json[lang]) {
|
||||
fullTranslation += JSON.stringify(component.json[lang]);
|
||||
}
|
||||
});
|
||||
if (fullTranslation !== '') {
|
||||
@ -78,7 +88,7 @@ export class AlfrescoTranslationLoader implements TranslateLoader {
|
||||
if (observableBatch.length > 0) {
|
||||
Observable.forkJoin(observableBatch).subscribe(
|
||||
() => {
|
||||
let fullTranslation = this.getFullTranslationJSON();
|
||||
let fullTranslation = this.getFullTranslationJSON(lang);
|
||||
if (fullTranslation) {
|
||||
observer.next(fullTranslation);
|
||||
}
|
||||
@ -88,11 +98,10 @@ export class AlfrescoTranslationLoader implements TranslateLoader {
|
||||
console.error(err);
|
||||
});
|
||||
} else {
|
||||
let fullTranslation = this.getFullTranslationJSON();
|
||||
let fullTranslation = this.getFullTranslationJSON(lang);
|
||||
if (fullTranslation) {
|
||||
observer.next(fullTranslation);
|
||||
}
|
||||
observer.complete();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@ -76,7 +76,7 @@ export class AlfrescoSearchAutocompleteComponent implements OnInit, OnChanges {
|
||||
|
||||
ngOnInit(): void {
|
||||
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');
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user