From 0fc0fb83457221b191217e4934227d3a2604873d Mon Sep 17 00:00:00 2001 From: bnymncoskuner Date: Tue, 25 Jan 2022 16:50:15 +0300 Subject: [PATCH 1/2] fix: listen to current lang instead of change in document-dir --- .../src/lib/services/localization.service.ts | 6 +++++- .../src/lib/handlers/document-dir.handler.ts | 16 +++++----------- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/npm/ng-packs/packages/core/src/lib/services/localization.service.ts b/npm/ng-packs/packages/core/src/lib/services/localization.service.ts index afdda6849b3..b69b25440bb 100644 --- a/npm/ng-packs/packages/core/src/lib/services/localization.service.ts +++ b/npm/ng-packs/packages/core/src/lib/services/localization.service.ts @@ -27,7 +27,11 @@ export class LocalizationService { * Returns currently selected language */ get currentLang(): string { - return this.latestLang || this.sessionState.getLanguage(); + return this.latestLang; + } + + get currentLang$(): Observable { + return this.sessionState.getLanguage$(); } get languageChange$(): Observable { diff --git a/npm/ng-packs/packages/theme-shared/src/lib/handlers/document-dir.handler.ts b/npm/ng-packs/packages/theme-shared/src/lib/handlers/document-dir.handler.ts index fb5d65710f6..8260a39c270 100644 --- a/npm/ng-packs/packages/theme-shared/src/lib/handlers/document-dir.handler.ts +++ b/npm/ng-packs/packages/theme-shared/src/lib/handlers/document-dir.handler.ts @@ -1,7 +1,7 @@ import { getLocaleDirection, LocalizationService } from '@abp/ng.core'; import { Injectable, Injector } from '@angular/core'; import { BehaviorSubject } from 'rxjs'; -import { map, startWith } from 'rxjs/operators'; +import { map } from 'rxjs/operators'; import { LocaleDirection } from '../models/common'; @Injectable() @@ -14,17 +14,11 @@ export class DocumentDirHandlerService { private listenToLanguageChanges() { const l10n = this.injector.get(LocalizationService); - // will always listen, no need to unsubscribe - l10n.languageChange$ - .pipe( - startWith(l10n.currentLang), - map(locale => getLocaleDirection(locale)), - ) - .subscribe(dir => { - this.dir.next(dir); - this.setBodyDir(dir); - }); + l10n.currentLang$.pipe(map(locale => getLocaleDirection(locale))).subscribe(dir => { + this.dir.next(dir); + this.setBodyDir(dir); + }); } private setBodyDir(dir: LocaleDirection) { From 80fb1ab01d33837eea973296d59e766ca41339cc Mon Sep 17 00:00:00 2001 From: bnymncoskuner Date: Tue, 25 Jan 2022 17:16:25 +0300 Subject: [PATCH 2/2] fix: add comment on currentLang of localization service --- .../packages/core/src/lib/services/localization.service.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/npm/ng-packs/packages/core/src/lib/services/localization.service.ts b/npm/ng-packs/packages/core/src/lib/services/localization.service.ts index b69b25440bb..f41c5c9426f 100644 --- a/npm/ng-packs/packages/core/src/lib/services/localization.service.ts +++ b/npm/ng-packs/packages/core/src/lib/services/localization.service.ts @@ -25,9 +25,12 @@ export class LocalizationService { /** * Returns currently selected language + * Even though this looks like it's redundant to return the same value as `getLanguage()`, + * it's actually not. This could be invoked any time, and the latestLang could be different from the + * sessionState.getLanguage() value. */ get currentLang(): string { - return this.latestLang; + return this.latestLang || this.sessionState.getLanguage(); } get currentLang$(): Observable {