[ADF-1356] Single configuration of i18n service per project (#2199)

* rework i18n layer init

* fix unit tests

* fix tests

* test fixes

* remove obsolete tests
This commit is contained in:
Denys Vuika
2017-08-11 10:55:52 +01:00
committed by Eugenio Romano
parent 9da61ddba7
commit 5f47450643
85 changed files with 333 additions and 431 deletions

View File

@@ -15,27 +15,42 @@
* limitations under the License.
*/
import { Injectable } from '@angular/core';
import { Inject, Injectable, OpaqueToken } from '@angular/core';
import { TranslateService } from '@ngx-translate/core';
import { Observable } from 'rxjs/Rx';
import { AlfrescoTranslateLoader } from './translate-loader.service';
export const TRANSLATION_PROVIDER = new OpaqueToken('Injection token for translation providers.');
export interface TranslationProvider {
name: string;
source: string;
}
@Injectable()
export class TranslationService {
defaultLang: string = 'en';
userLang: string = 'en';
customLoader: AlfrescoTranslateLoader;
constructor(public translate: TranslateService) {
constructor(public translate: TranslateService,
@Inject(TRANSLATION_PROVIDER) providers: TranslationProvider[]) {
this.userLang = translate.getBrowserLang() || this.defaultLang;
translate.setDefaultLang(this.defaultLang);
this.customLoader = <AlfrescoTranslateLoader> this.translate.currentLoader;
this.use(this.userLang);
if (providers && providers.length > 0) {
for (let provider of providers) {
this.addTranslationFolder(provider.name, provider.source);
}
}
}
addTranslationFolder(name: string = '', path: string = '') {
if (!this.customLoader.existComponent(name)) {
this.customLoader.addComponentList(name, path);
if (!this.customLoader.providerRegistered(name)) {
this.customLoader.registerProvider(name, path);
if (this.userLang !== this.defaultLang) {
this.translate.getTranslation(this.defaultLang).subscribe(() => {
this.translate.getTranslation(this.userLang).subscribe(