diff --git a/src/app/app.component.ts b/src/app/app.component.ts index 288c43c807f49edcdf9ec46f966266895dadb1a2..239924d964c45612e57521e5e71f9113bd3d0b0a 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -97,7 +97,7 @@ export class AppComponent implements OnInit, OnDestroy, Observer { ngOnInit() { this.intlService.addObserver(this); - this.intlService.setLocale("fr"); + this.intlService.setLocale(this.appSetupService.language); this.formulaireService.addObserver(this); this.subscribeErrorService(); this._innerWidth = window.innerWidth; @@ -196,12 +196,10 @@ export class AppComponent implements OnInit, OnDestroy, Observer { } private updateLocale() { - const tag = this.intlService.currentLanguage.tag; + const tag = this.appSetupService.language; document["locale"] = tag; + this.intlService.setLocale(tag); - // location.reload(true); - // this.cdRef.markForCheck(); - // this.cdRef.detectChanges(); this.appRef.tick(); } diff --git a/src/app/components/app-setup/app-setup.component.ts b/src/app/components/app-setup/app-setup.component.ts index 01b852d4e403810b1f9203df645ac48fe6d79525..0cc435322d515ca21eac333f9ba95660498f840a 100644 --- a/src/app/components/app-setup/app-setup.component.ts +++ b/src/app/components/app-setup/app-setup.component.ts @@ -44,12 +44,18 @@ export class ApplicationSetupComponent extends BaseComponent implements Observer this.appSetupService.addObserver(this); } - public get currentLanguageLabel(): string { - return this.intlService.currentLanguage.label; + public get availableLanguages() { + return this.intlService.languages; } - private selectLang(lc: LanguageCode) { + public get currentLanguageCode() { + return this.intlService.currentLanguage.code; + } + + public set currentLanguageCode(lc: LanguageCode) { this.intlService.setLocale(lc); + // keep language in sync in app-wide parameters service + this.appSetupService.language = this.intlService.currentLanguage.tag; } public get uitextTitle(): string { diff --git a/src/app/services/app-setup/app-setup.service.ts b/src/app/services/app-setup/app-setup.service.ts index 21137d291248f510413c34a40feaa889d9df250d..6a6069c637bfdc6986892a5e09ac8833bbb10a21 100644 --- a/src/app/services/app-setup/app-setup.service.ts +++ b/src/app/services/app-setup/app-setup.service.ts @@ -14,12 +14,13 @@ export class ApplicationSetupService extends Observable { public displayPrecision = 0.001; public computePrecision = 0.0001; public newtonMaxIterations = 50; - public language = "fr"; + private _language = "fr"; /** themes to group calculators, for displaying on the front page */ public themes: any[]; public constructor( - private httpService: HttpService) { + private httpService: HttpService + ) { super(); this.readValuesFromConfig(); @@ -29,6 +30,15 @@ export class ApplicationSetupService extends Observable { return -Math.log10(this.displayPrecision); } + public set language(lang: string) { + this._language = lang; + this.notifyObservers(null); + } + + public get language(): string { + return this._language; + } + // @TODO save preferences in cookie / localStorage ? // read default values from config and notify observers diff --git a/src/app/services/internationalisation/internationalisation.service.ts b/src/app/services/internationalisation/internationalisation.service.ts index 88bdc9fae5b30e3f654cb82394046a410d255458..5eb90bcc6c84752e416a77a2d2184de4e5e33a93 100644 --- a/src/app/services/internationalisation/internationalisation.service.ts +++ b/src/app/services/internationalisation/internationalisation.service.ts @@ -139,6 +139,9 @@ export class I18nService extends Observable { } private getMessageFromCode(c: MessageCode): string { + if (! this._Messages) { + return `*** Messages not loaded yet ***`; + } if (this._Messages[MessageCode[c]] === undefined) { return `*** Message ${MessageCode[c]} non traduit ***`; }