[ADF-3784] support for browser cultures (i18n) (#4066)

* support language cultures

* update docs

* fix typo

* fix tests

* correctly replace fallback translations

* login dialog fixes

* fix error component

* [denys-i18n-cultures] Fix error content unit tests
This commit is contained in:
Denys Vuika
2018-12-20 16:41:56 +00:00
committed by Eugenio Romano
parent d6df5bc862
commit b5f9036545
9 changed files with 174 additions and 142 deletions

View File

@@ -33,10 +33,15 @@ export class TranslateLoaderService implements TranslateLoader {
private suffix: string = '.json';
private providers: ComponentTranslationModel[] = [];
private queue: string [][] = [];
private defaultLang: string = 'en';
constructor(private http: HttpClient) {
}
setDefaultLang(value: string) {
this.defaultLang = value || 'en';
}
registerProvider(name: string, path: string) {
let registered = this.providers.find((provider) => provider.name === name);
if (registered) {
@@ -50,6 +55,29 @@ export class TranslateLoaderService implements TranslateLoader {
return this.providers.find((x) => x.name === name) ? true : false;
}
fetchLanguageFile(lang: string, component: ComponentTranslationModel, fallbackUrl?: string): Observable<void> {
const translationUrl = fallbackUrl || `${component.path}/${this.prefix}/${lang}${this.suffix}?v=${Date.now()}`;
return this.http.get(translationUrl).pipe(
map((res: Response) => {
component.json[lang] = res;
}),
retry(3),
catchError(() => {
if (!fallbackUrl && lang.includes('-')) {
const [langId] = lang.split('-');
if (langId && langId !== this.defaultLang) {
const url = `${component.path}/${this.prefix}/${langId}${this.suffix}?v=${Date.now()}`;
return this.fetchLanguageFile(lang, component, url);
}
}
return throwError(`Failed to load ${translationUrl}`);
})
);
}
getComponentToFetch(lang: string): Array<Observable<any>> {
const observableBatch = [];
if (!this.queue[lang]) {
@@ -59,16 +87,8 @@ export class TranslateLoaderService implements TranslateLoader {
if (!this.isComponentInQueue(lang, component.name)) {
this.queue[lang].push(component.name);
const translationUrl = `${component.path}/${this.prefix}/${lang}${this.suffix}?v=${Date.now()}`;
observableBatch.push(
this.http.get(translationUrl).pipe(
map((res: Response) => {
component.json[lang] = res;
}),
retry(3),
catchError(() => throwError(`Failed to load ${translationUrl}`))
)
this.fetchLanguageFile(lang, component)
);
}
});

View File

@@ -43,6 +43,7 @@ export class TranslationService {
this.defaultLang = 'en';
translate.setDefaultLang(this.defaultLang);
this.customLoader.setDefaultLang(this.defaultLang);
if (providers && providers.length > 0) {
for (let provider of providers) {

View File

@@ -117,24 +117,24 @@ describe('UserPreferencesService', () => {
it('should return as default locale the app.config locate as first', () => {
appConfig.config.locale = 'fake-locate-config';
spyOn(translate, 'getBrowserLang').and.returnValue('fake-locate-browser');
spyOn(translate, 'getBrowserCultureLang').and.returnValue('fake-locate-browser');
expect(preferences.getDefaultLocale()).toBe('fake-locate-config');
});
it('should return as default locale the browser locale as second', () => {
spyOn(translate, 'getBrowserLang').and.returnValue('fake-locate-browser');
spyOn(translate, 'getBrowserCultureLang').and.returnValue('fake-locate-browser');
expect(preferences.getDefaultLocale()).toBe('fake-locate-browser');
});
it('should return as default locale the component property as third ', () => {
spyOn(translate, 'getBrowserLang').and.stub();
spyOn(translate, 'getBrowserCultureLang').and.stub();
expect(preferences.getDefaultLocale()).toBe('en');
});
it('should return as locale the store locate', () => {
preferences.locale = 'fake-store-locate';
appConfig.config.locale = 'fake-locate-config';
spyOn(translate, 'getBrowserLang').and.returnValue('fake-locate-browser');
spyOn(translate, 'getBrowserCultureLang').and.returnValue('fake-locate-browser');
expect(preferences.locale).toBe('fake-store-locate');
});

View File

@@ -183,7 +183,7 @@ export class UserPreferencesService {
* @returns Default locale language code
*/
public getDefaultLocale(): string {
return this.appConfig.get<string>('locale') || this.translate.getBrowserLang() || 'en';
return this.appConfig.get<string>('locale') || this.translate.getBrowserCultureLang() || 'en';
}
}