From 8a4e2f3316a45c17d671cf83f1dd2b7f99b1e0f2 Mon Sep 17 00:00:00 2001 From: Mahmut Gundogdu Date: Mon, 7 Nov 2022 12:12:00 +0300 Subject: [PATCH] getting language resources sepereated --- .../packages/core/src/lib/core.module.ts | 2 ++ ...include-localization-resources.provider.ts | 8 +++++ .../packages/core/src/lib/providers/index.ts | 1 + .../src/lib/services/config-state.service.ts | 33 ++++++++++++++----- .../src/lib/services/localization.service.ts | 4 +-- .../include-localization-resources.token.ts | 3 ++ .../packages/core/src/lib/tokens/index.ts | 1 + 7 files changed, 42 insertions(+), 10 deletions(-) create mode 100644 npm/ng-packs/packages/core/src/lib/providers/include-localization-resources.provider.ts create mode 100644 npm/ng-packs/packages/core/src/lib/tokens/include-localization-resources.token.ts diff --git a/npm/ng-packs/packages/core/src/lib/core.module.ts b/npm/ng-packs/packages/core/src/lib/core.module.ts index 4bf3fae3a1..212bf02654 100644 --- a/npm/ng-packs/packages/core/src/lib/core.module.ts +++ b/npm/ng-packs/packages/core/src/lib/core.module.ts @@ -38,6 +38,7 @@ import { ShortDateTimePipe } from './pipes/short-date-time.pipe'; import { ShortTimePipe } from './pipes/short-time.pipe'; import { ShortDatePipe } from './pipes/short-date.pipe'; import { TimeoutLimitedOAuthService } from './services/timeout-limited-oauth.service'; +import { IncludeLocalizationResourcesProvider } from './providers/include-localization-resources.provider'; export function storageFactory(): OAuthStorage { return oAuthStorage; @@ -193,6 +194,7 @@ export class CoreModule { useValue: localizationContributor(options.localizations), deps: [LocalizationService], }, + IncludeLocalizationResourcesProvider ], }; } diff --git a/npm/ng-packs/packages/core/src/lib/providers/include-localization-resources.provider.ts b/npm/ng-packs/packages/core/src/lib/providers/include-localization-resources.provider.ts new file mode 100644 index 0000000000..f530f72cb1 --- /dev/null +++ b/npm/ng-packs/packages/core/src/lib/providers/include-localization-resources.provider.ts @@ -0,0 +1,8 @@ + import { Provider } from "@angular/core"; + import { INCUDE_LOCALIZATION_RESOURCES_TOKEN } from "../tokens/include-localization-resources.token"; + +export const IncludeLocalizationResourcesProvider: Provider = { + provide: INCUDE_LOCALIZATION_RESOURCES_TOKEN, + useValue: false, + }; + \ No newline at end of file diff --git a/npm/ng-packs/packages/core/src/lib/providers/index.ts b/npm/ng-packs/packages/core/src/lib/providers/index.ts index f7310ef146..8a9f61bda5 100644 --- a/npm/ng-packs/packages/core/src/lib/providers/index.ts +++ b/npm/ng-packs/packages/core/src/lib/providers/index.ts @@ -1,2 +1,3 @@ export * from './cookie-language.provider'; export * from './locale.provider'; +export * from './include-localization-resources.provider'; \ No newline at end of file diff --git a/npm/ng-packs/packages/core/src/lib/services/config-state.service.ts b/npm/ng-packs/packages/core/src/lib/services/config-state.service.ts index 0a16cf8143..2505b4f601 100644 --- a/npm/ng-packs/packages/core/src/lib/services/config-state.service.ts +++ b/npm/ng-packs/packages/core/src/lib/services/config-state.service.ts @@ -1,4 +1,4 @@ -import { Injectable } from '@angular/core'; +import { Inject, Injectable } from '@angular/core'; import { Observable, Subject, of } from 'rxjs'; import { map, switchMap, take } from 'rxjs/operators'; import { AbpApplicationConfigurationService } from '../proxy/volo/abp/asp-net-core/mvc/application-configurations/abp-application-configuration.service'; @@ -7,6 +7,7 @@ import { ApplicationConfigurationDto, ApplicationGlobalFeatureConfigurationDto, } from '../proxy/volo/abp/asp-net-core/mvc/application-configurations/models'; +import { INCUDE_LOCALIZATION_RESOURCES_TOKEN } from '../tokens/include-localization-resources.token'; import { InternalStore } from '../utils/internal-store-utils'; @Injectable({ @@ -14,8 +15,7 @@ import { InternalStore } from '../utils/internal-store-utils'; }) export class ConfigStateService { private readonly store = new InternalStore({} as ApplicationConfigurationDto); - private includeLocalizationResources = false; - + get createOnUpdateStream() { return this.store.sliceUpdate; } @@ -25,6 +25,7 @@ export class ConfigStateService { constructor( private abpConfigService: AbpApplicationConfigurationService, private abpApplicationLocalizationService: AbpApplicationLocalizationService, + @Inject(INCUDE_LOCALIZATION_RESOURCES_TOKEN) private readonly includeLocalizationResources: boolean, ) { this.initUpdateStream(); } @@ -36,7 +37,7 @@ export class ConfigStateService { this.abpConfigService.get({ includeLocalizationResources: this.includeLocalizationResources, }), - ), + ) ) .pipe(switchMap(appState => this.getLocalizationAndCombineWithAppState(appState))) .subscribe(res => this.store.set(res)); @@ -45,20 +46,36 @@ export class ConfigStateService { private getLocalizationAndCombineWithAppState( appState: ApplicationConfigurationDto, ): Observable { - return this.abpApplicationLocalizationService - .get({ cultureName: appState.localization.currentCulture.name, onlyDynamics: false }) + return this.getlocalizationResource(appState.localization.currentCulture.cultureName) .pipe( - switchMap(result => - of({ ...appState, localization: { ...appState.localization, ...result } }), + map(result =>({ ...appState, localization: { ...appState.localization, ...result } }), ), ); } + private getlocalizationResource(cultureName:string){ + return this.abpApplicationLocalizationService + .get({ cultureName: cultureName, onlyDynamics: false }) + } refreshAppState() { this.updateSubject.next(); return this.createOnUpdateStream(state => state).pipe(take(1)); } + refreshLocalization(lang:string) { + if (this.includeLocalizationResources) { + return this.refreshAppState(); + } else { + return this.getlocalizationResource(lang) + .pipe( + switchMap(result => { + this.store.patch({ localization: { ...this.store.state.localization, ...result } }); + return of(null); + }), + ); + } + } + getOne$(key: string) { return this.store.sliceState(state => state[key]); } 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 aca4383a94..512ecdebae 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 @@ -1,6 +1,6 @@ import { registerLocaleData } from '@angular/common'; import { Injectable, Injector, isDevMode, Optional, SkipSelf } from '@angular/core'; -import { BehaviorSubject, combineLatest, from, Observable, Subject } from 'rxjs'; +import { BehaviorSubject, combineLatest, from, Observable, of, Subject } from 'rxjs'; import { filter, map, mapTo, switchMap } from 'rxjs/operators'; import { ABP } from '../models/common'; import { LocalizationWithDefault } from '../models/localization'; @@ -133,7 +133,7 @@ export class LocalizationService { filter( lang => this.configState.getDeep('localization.currentCulture.cultureName') !== lang, ), - switchMap(lang => this.configState.refreshAppState().pipe(mapTo(lang))), + switchMap(lang => this.configState.refreshLocalization(lang).pipe(map(() => lang))), switchMap(lang => from(this.registerLocale(lang).then(() => lang))), ) .subscribe(lang => this._languageChange$.next(lang)); diff --git a/npm/ng-packs/packages/core/src/lib/tokens/include-localization-resources.token.ts b/npm/ng-packs/packages/core/src/lib/tokens/include-localization-resources.token.ts new file mode 100644 index 0000000000..ac805a8ee8 --- /dev/null +++ b/npm/ng-packs/packages/core/src/lib/tokens/include-localization-resources.token.ts @@ -0,0 +1,3 @@ +import { InjectionToken } from "@angular/core"; + +export const INCUDE_LOCALIZATION_RESOURCES_TOKEN = new InjectionToken('INCUDE_LOCALIZATION_RESOURCES_TOKEN'); diff --git a/npm/ng-packs/packages/core/src/lib/tokens/index.ts b/npm/ng-packs/packages/core/src/lib/tokens/index.ts index da1ea9bdad..20cab35c28 100644 --- a/npm/ng-packs/packages/core/src/lib/tokens/index.ts +++ b/npm/ng-packs/packages/core/src/lib/tokens/index.ts @@ -6,3 +6,4 @@ export * from './lodaer-delay.token'; export * from './manage-profile.token'; export * from './options.token'; export * from './tenant-key.token'; +export * from './include-localization-resources.token'; \ No newline at end of file