Skip to content

Commit

Permalink
feat(core/language): refact the language initialization (#1563)
Browse files Browse the repository at this point in the history
  • Loading branch information
alecarn authored Jan 18, 2024
1 parent b7e31e4 commit 3b079c0
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 28 deletions.
8 changes: 8 additions & 0 deletions packages/core/src/lib/language/shared/language.interface.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
import { TranslateLoader } from '@ngx-translate/core';
import { Observable } from 'rxjs';

export interface LanguageOptions {
prefix?: string | string[];
}

export abstract class LanguageLoaderBase implements TranslateLoader {
abstract isLoaded$: Observable<boolean>;
abstract getTranslation(lang: string): Observable<any>;
}
61 changes: 36 additions & 25 deletions packages/core/src/lib/language/shared/language.loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,57 @@ import { HttpBackend, HttpClient } from '@angular/common/http';

import { ObjectUtils } from '@igo2/utils';

import { TranslateLoader } from '@ngx-translate/core';
import { forkJoin, map } from 'rxjs';
import { BehaviorSubject, first, forkJoin, map, switchMap, tap } from 'rxjs';

import { ConfigService } from '../../config/config.service';
import { LanguageLoaderBase } from './language.interface';

export class LanguageLoader implements TranslateLoader {
export class LanguageLoader implements LanguageLoaderBase {
private httpClient: HttpClient;

private _isLoaded$ = new BehaviorSubject<boolean>(null);
isLoaded$ = this._isLoaded$.asObservable();

constructor(
handler: HttpBackend,
private configService: ConfigService,
private prefix?: string | string[],
private suffix: string = '.json',
private config?: ConfigService
private suffix: string = '.json'
) {
this.httpClient = new HttpClient(handler);
}

public getTranslation(lang: string): any {
const igoLocale$ = this.httpClient.get(`locale/libs_locale/${lang}.json`);
return this.configService.isLoaded$.pipe(
first((isLoaded) => isLoaded),
switchMap(() => {
if (this.configService && !this.prefix) {
const prefix = this.configService.getConfig('language.prefix');
this.prefix = !prefix || Array.isArray(prefix) ? prefix : [prefix];
}

if (!this.prefix || this.prefix.length === 0) {
this._isLoaded$.next(true);
return igoLocale$;
}

const appLocale$ = (this.prefix as string[]).map((prefix) =>
this.httpClient.get(`${prefix}${lang}${this.suffix}`)
);

if (this.config && !this.prefix) {
const prefix = this.config.getConfig('language.prefix');
this.prefix = !prefix || Array.isArray(prefix) ? prefix : [prefix];
}

if (!this.prefix || this.prefix.length === 0) {
return igoLocale$;
}

const appLocale$ = (this.prefix as string[]).map((prefix) =>
this.httpClient.get(`${prefix}${lang}${this.suffix}`)
);

const locale$ = forkJoin([igoLocale$, ...appLocale$]);

return locale$.pipe(
map((translations) => {
return translations.reduce(
(acc, current) => ObjectUtils.mergeDeep(acc, current),
{}
const locale$ = forkJoin([igoLocale$, ...appLocale$]);

return locale$.pipe(
map((translations) => {
return translations.reduce(
(acc, current) => ObjectUtils.mergeDeep(acc, current),
{}
);
}),
tap(() => {
this._isLoaded$.next(true);
})
);
})
);
Expand Down
12 changes: 9 additions & 3 deletions packages/core/src/lib/language/shared/language.provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ import {
TranslateModule,
TranslateModuleConfig
} from '@ngx-translate/core';
import { first } from 'rxjs';

import { ConfigService } from '../../config';
import { LanguageLoaderBase } from './language.interface';
import { LanguageLoader } from './language.loader';
import { LanguageService } from './language.service';
import { IgoMissingTranslationHandler } from './missing-translation.guard';
Expand All @@ -29,7 +31,11 @@ export function provideRootTranslation(
importProvidersFrom(TranslateModule.forRoot(setTranslationConfig(loader))),
{
provide: APP_INITIALIZER,
useFactory: () => () => {},
useFactory: (languageService: LanguageService) => () => {
return (
languageService.translate.currentLoader as LanguageLoaderBase
).isLoaded$?.pipe(first((isLoaded) => isLoaded === true));
},
deps: [LanguageService],
multi: true
}
Expand All @@ -39,7 +45,7 @@ export function provideRootTranslation(
export const setTranslationConfig = (
loader?: Provider
): TranslateModuleConfig => ({
defaultLanguage: 'en',
defaultLanguage: 'fr',
loader: loader ?? DEFAULT_LANGUAGE_LOADER,
missingTranslationHandler: {
provide: MissingTranslationHandler,
Expand All @@ -54,5 +60,5 @@ export const DEFAULT_LANGUAGE_LOADER: Provider = {
};

function defaultLanguageLoader(http: HttpBackend, config?: ConfigService) {
return new LanguageLoader(http, undefined, undefined, config);
return new LanguageLoader(http, config, undefined, undefined);
}

0 comments on commit 3b079c0

Please sign in to comment.