From 070061107fc857326f29731f3b6045f069bbca7f Mon Sep 17 00:00:00 2001 From: mauriziovitale84 Date: Wed, 23 Nov 2016 17:32:43 +0000 Subject: [PATCH] #1076 Improve the TranslationLoader --- .../src/models/component.model.ts | 28 ++++++ .../services/AlfrescoTranslation.service.ts | 11 ++- .../AlfrescoTranslationLoader.service.ts | 85 ++++++++++++------- 3 files changed, 91 insertions(+), 33 deletions(-) create mode 100644 ng2-components/ng2-alfresco-core/src/models/component.model.ts diff --git a/ng2-components/ng2-alfresco-core/src/models/component.model.ts b/ng2-components/ng2-alfresco-core/src/models/component.model.ts new file mode 100644 index 0000000000..0db6831b8c --- /dev/null +++ b/ng2-components/ng2-alfresco-core/src/models/component.model.ts @@ -0,0 +1,28 @@ +/*! + * @license + * Copyright 2016 Alfresco Software, Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export class ComponentTranslationModel { + name: string; + path: string; + json: string; + + constructor(obj?: any) { + this.name = obj && obj.name; + this.path = obj && obj.path; + this.json = obj && obj.json || null; + } +} diff --git a/ng2-components/ng2-alfresco-core/src/services/AlfrescoTranslation.service.ts b/ng2-components/ng2-alfresco-core/src/services/AlfrescoTranslation.service.ts index 71cef189b6..c7ec9d0dd1 100644 --- a/ng2-components/ng2-alfresco-core/src/services/AlfrescoTranslation.service.ts +++ b/ng2-components/ng2-alfresco-core/src/services/AlfrescoTranslation.service.ts @@ -29,13 +29,16 @@ export class AlfrescoTranslationService { translate.setDefaultLang(this.userLang); } - addTranslationFolder(name: string = '') { + addTranslationFolder(name: string = '', path: string = '') { let loader = this.translate.currentLoader; if (!loader.existComponent(name)) { - loader.addComponentList(name); - this.translate.getTranslation(this.userLang); + loader.addComponentList(name, path); + this.translate.getTranslation(this.userLang).subscribe( + () => { + this.translate.use(this.userLang); + } + ); } - this.translate.use(this.userLang); } use(lang: string): Observable { diff --git a/ng2-components/ng2-alfresco-core/src/services/AlfrescoTranslationLoader.service.ts b/ng2-components/ng2-alfresco-core/src/services/AlfrescoTranslationLoader.service.ts index 9fa0d64688..2cc2e3e4ee 100644 --- a/ng2-components/ng2-alfresco-core/src/services/AlfrescoTranslationLoader.service.ts +++ b/ng2-components/ng2-alfresco-core/src/services/AlfrescoTranslationLoader.service.ts @@ -19,54 +19,81 @@ import { Injectable } from '@angular/core'; import { Response, Http } from '@angular/http'; import { Observable } from 'rxjs/Observable'; import { TranslateLoader } from 'ng2-translate/ng2-translate'; +import { ComponentTranslationModel } from '../models/component.model'; @Injectable() export class AlfrescoTranslationLoader implements TranslateLoader { private prefix: string = 'i18n'; private suffix: string = '.json'; - private _componentList: string[] = []; + private _componentList: ComponentTranslationModel[] = []; + private queue: string[] = []; constructor(private http: Http) { } - addComponentList(name: string) { - this._componentList.push(name); + addComponentList(nameInput: string, pathInput: string) { + this._componentList.push(new ComponentTranslationModel({name: nameInput, path: pathInput})); } - existComponent(name: string) { - return this._componentList.indexOf(name) >= 0; + existComponent(name: string): boolean { + return this._componentList.find(x => x.name === name) ? true : false; + } + + getComponentToFetch(lang: string) { + let observableBatch = []; + this._componentList.forEach((component) => { + if (!this.queue.find(x => x === component.name)) { + this.queue.push(component.name); + observableBatch.push(this.http.get(`${component.path}/${this.prefix}/${lang}${this.suffix}`) + .map((res: Response) => { + component.json = res.json(); + }) + .catch((/*err: any, source: Observable, caught: Observable*/) => { + // Empty Observable just to go ahead + return Observable.of(''); + })); + } + }); + return observableBatch; + } + + getFullTranslationJSON() { + 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 (fullTranslation !== '') { + return JSON.parse(fullTranslation.replace(/}{/g, ',')); + } } getTranslation(lang: string): Observable { - let self = this; - let observableBatch = []; - this._componentList.forEach((component) => { - observableBatch.push(this.http.get(`${component}/${self.prefix}/${lang}${self.suffix}`) - .map((res: Response) => res.json()) - .catch( (/*err: any, source: Observable, caught: Observable*/) => { - // Empty Observable just to go ahead - return Observable.of(''); - })); - }); + let observableBatch = this.getComponentToFetch(lang); return Observable.create(observer => { - Observable.forkJoin(observableBatch).subscribe( - (translations: any[]) => { - let multiLanguage: any = ''; - translations.forEach((translate) => { - if (translate !== '') { - multiLanguage += JSON.stringify(translate); + if (observableBatch.length > 0) { + Observable.forkJoin(observableBatch).subscribe( + () => { + let fullTranslation = this.getFullTranslationJSON(); + if (fullTranslation) { + observer.next(fullTranslation); } - }); - if (multiLanguage !== '') { - observer.next(JSON.parse(multiLanguage.replace(/}{/g, ','))); - } - observer.complete(); - }, - (err: any) => { + observer.complete(); + }, + (err: any) => { console.error(err); - }); + }); + } else { + let fullTranslation = this.getFullTranslationJSON(); + if (fullTranslation) { + observer.next(fullTranslation); + } + observer.complete(); + } }); } }