[ADF-3630] fix i18n fallback behaviour (#3851)

* fix i18n fallback behaviour

* update tests

* update tests

* configuration fixes

* test configuration fix
This commit is contained in:
Denys Vuika
2018-10-04 10:11:30 +01:00
committed by Eugenio Romano
parent d1f87837ac
commit 54cca45ec1
12 changed files with 126 additions and 92 deletions

View File

@@ -100,8 +100,8 @@ import { ViewUtilService } from './viewer/services/view-util.service';
import { LoginDialogService } from './services/login-dialog.service';
import { ExternalAlfrescoApiService } from './services/external-alfresco-api.service';
export function createTranslateLoader(http: HttpClient, logService: LogService) {
return new TranslateLoaderService(http, logService);
export function createTranslateLoader(http: HttpClient) {
return new TranslateLoaderService(http);
}
export function providers() {

View File

@@ -28,11 +28,11 @@ module.exports = function (config) {
{pattern: 'lib/core/i18n/**/en.json', included: false, served: true, watched: false},
{pattern: 'lib/core/./**/*.ts', included: false, served: true, watched: false},
{pattern: 'lib/core/**/*.ts', included: false, served: true, watched: false},
{pattern: 'lib/core/assets/**/*.svg', included: false, served: true, watched: false},
{pattern: 'lib/config/app.config.json', included: false, served: true, watched: false},
{pattern: 'lib/core//viewer/assets/fake-test-file.pdf', included: false, served: true, watched: false},
{pattern: 'lib/core/viewer/assets/fake-test-file.pdf', included: false, served: true, watched: false},
{pattern: 'lib/core/viewer/assets/fake-test-file.txt', included: false, served: true, watched: false},
{
pattern: 'lib/core//viewer/assets/fake-test-password-file.pdf',

View File

@@ -34,17 +34,13 @@ export class TranslationMock implements TranslationService {
onLangChange: new EventEmitter<LangChangeEvent>()
};
addTranslationFolder() {
addTranslationFolder() {}
}
onTranslationChanged() {}
onTranslationChanged() {
use(): any {}
}
use(): any {
}
loadTranslation() {}
get(key: string | Array<string>, interpolateParams?: Object): Observable<string | any> {
return of(key);

View File

@@ -19,11 +19,10 @@ import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Response } from '@angular/http';
import { TranslateLoader } from '@ngx-translate/core';
import { Observable, forkJoin } from 'rxjs';
import { Observable, forkJoin, throwError } from 'rxjs';
import { ComponentTranslationModel } from '../models/component.model';
import { ObjectUtils } from '../utils/object-utils';
import { LogService } from './log.service';
import { map } from 'rxjs/operators';
import { map, catchError } from 'rxjs/operators';
@Injectable()
export class TranslateLoaderService implements TranslateLoader {
@@ -33,8 +32,7 @@ export class TranslateLoaderService implements TranslateLoader {
private providers: ComponentTranslationModel[] = [];
private queue: string [][] = [];
constructor(private http: HttpClient,
private logService: LogService) {
constructor(private http: HttpClient) {
}
registerProvider(name: string, path: string) {
@@ -59,21 +57,16 @@ export class TranslateLoaderService implements TranslateLoader {
if (!this.isComponentInQueue(lang, component.name)) {
this.queue[lang].push(component.name);
const loader = Observable.create(observer => {
const translationUrl = `${component.path}/${this.prefix}/${lang}${this.suffix}?v=${Date.now()}`;
const translationUrl = `${component.path}/${this.prefix}/${lang}${this.suffix}?v=${Date.now()}`;
this.http.get(translationUrl).pipe(map((res: Response) => {
component.json[lang] = res;
})).subscribe((result) => {
observer.next(result);
observer.complete();
}, () => {
observer.next('');
observer.complete();
});
});
observableBatch.push(loader);
observableBatch.push(
this.http.get(translationUrl).pipe(
map((res: Response) => {
component.json[lang] = res;
}),
catchError(() => throwError(`Error loading ${translationUrl}`))
)
);
}
});
@@ -114,7 +107,7 @@ export class TranslateLoaderService implements TranslateLoader {
}
getTranslation(lang: string): Observable<any> {
let observableBatch = this.getComponentToFetch(lang);
const observableBatch = this.getComponentToFetch(lang);
return Observable.create(observer => {
if (observableBatch.length > 0) {
@@ -126,13 +119,14 @@ export class TranslateLoaderService implements TranslateLoader {
}
observer.complete();
},
(err: any) => {
this.logService.error(err);
(err) => {
observer.error(err);
});
} else {
let fullTranslation = this.getFullTranslationJSON(lang);
if (fullTranslation) {
observer.next(fullTranslation);
observer.complete();
}
}
});

View File

@@ -62,22 +62,29 @@ export class TranslationService {
addTranslationFolder(name: string = '', path: string = '') {
if (!this.customLoader.providerRegistered(name)) {
this.customLoader.registerProvider(name, path);
if (this.userLang) {
this.translate.getTranslation(this.userLang).subscribe(() => {
this.translate.use(this.userLang);
this.onTranslationChanged(this.userLang);
}
);
this.loadTranslation(this.userLang, this.defaultLang);
} else {
this.translate.getTranslation(this.defaultLang).subscribe(() => {
this.translate.use(this.defaultLang);
this.onTranslationChanged(this.defaultLang);
}
);
this.loadTranslation(this.defaultLang);
}
}
}
loadTranslation(lang: string, fallback?: string) {
this.translate.getTranslation(lang).subscribe(
() => {
this.translate.use(lang);
this.onTranslationChanged(lang);
},
() => {
if (fallback && fallback !== lang) {
this.loadTranslation(fallback);
}
}
);
}
/**
* Triggers a notification callback when the translation language changes.
* @param lang The new language code