Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): language add ability to define the default language #1704

Merged
merged 1 commit into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions packages/core/language/src/shared/language.provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,14 @@ const TIMEOUT_DURATION = 5000;
/**
* Make sure you only call this method in the root module of your application, most of the time called AppModule.
*/
export function provideTranslation(loader?: Provider): EnvironmentProviders {
export function provideTranslation(
defaultLanguage?: string | undefined,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

au lieu de string, on ne serait pas mieux de basé sur une enum?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

J'avais mit un Type au début mais après coup vu que cette partie va être utilisé dans d'autre application et librairie, on ne peut connaitre d'avance l'ensemble des langues... Alors je crois que le string est le mieux placés.

loader?: Provider
): EnvironmentProviders {
return makeEnvironmentProviders([
importProvidersFrom(TranslateModule.forRoot(setTranslationConfig(loader))),
importProvidersFrom(
TranslateModule.forRoot(setTranslationConfig(loader, defaultLanguage))
),
{
provide: APP_INITIALIZER,
useFactory: (languageService: LanguageService) => () => {
Expand All @@ -53,9 +58,10 @@ export function provideTranslation(loader?: Provider): EnvironmentProviders {
}

export const setTranslationConfig = (
loader?: Provider
loader?: Provider,
defaultLanguage?: string
): TranslateModuleConfig => ({
defaultLanguage: 'fr',
defaultLanguage: defaultLanguage,
loader: loader ?? DEFAULT_LANGUAGE_LOADER,
missingTranslationHandler: {
provide: MissingTranslationHandler,
Expand Down
16 changes: 14 additions & 2 deletions packages/core/language/src/shared/language.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,30 @@ export class LanguageService {
readonly language$ = new BehaviorSubject<string>(undefined);

constructor(public translate: TranslateService) {
if (
!this.translate.defaultLang ||
!this.matchLanguage(this.translate.defaultLang)
) {
this.setBrowserLanguage();
}
}
private setBrowserLanguage() {
this.language = this.translate.getBrowserLang();
const lang = this.getLanguage();
this.translate.setDefaultLang(lang);
this.language$.next(lang);
}

public getLanguage(): string {
return this.language.match(/en|fr/) ? this.language : 'en';
return this.matchLanguage(this.language) ? this.language : 'en';
}

private matchLanguage(lang: string): boolean {
return !!lang.match(/en|fr/);
}

public setLanguage(language: string) {
this.language = language.match(/en|fr/) ? language : 'en';
this.language = this.matchLanguage(language) ? language : 'en';
combineLatest([
this.translate.use(this.language),
this.translate.reloadLang(this.language)
Expand Down
Loading