diff --git a/src/app/core/configurations/configuration.meta.ts b/src/app/core/configurations/configuration.meta.ts index 3c6ab845443..5d7b58ea748 100644 --- a/src/app/core/configurations/configuration.meta.ts +++ b/src/app/core/configurations/configuration.meta.ts @@ -1,3 +1,4 @@ +import { inject } from '@angular/core'; import { Params } from '@angular/router'; import { RouterNavigationPayload, routerNavigationAction } from '@ngrx/router-store'; import { ActionReducer } from '@ngrx/store'; @@ -6,6 +7,7 @@ import { applyConfiguration } from 'ish-core/store/core/configuration'; import { ConfigurationState, configurationReducer } from 'ish-core/store/core/configuration/configuration.reducer'; import { CoreState } from 'ish-core/store/core/core-store'; import { RouterState } from 'ish-core/store/core/router/router.reducer'; +import { CookiesService } from 'ish-core/utils/cookies/cookies.service'; import { mergeDeep } from 'ish-core/utils/functions'; class SimpleParamMap { @@ -19,7 +21,7 @@ class SimpleParamMap { } // eslint-disable-next-line complexity -function extractConfigurationParameters(state: ConfigurationState, paramMap: SimpleParamMap) { +function extractConfigurationParameters(state: ConfigurationState, paramMap: SimpleParamMap, preferredLocale?: string) { const keys: (keyof ConfigurationState)[] = ['channel', 'application', 'lang', 'currency', 'identityProvider']; const properties: Partial = keys .filter(key => paramMap.has(key) && paramMap.get(key) !== 'default') @@ -48,16 +50,25 @@ function extractConfigurationParameters(state: ConfigurationState, paramMap: Sim properties._deviceType = paramMap.get('device'); } + /* if the user has a language cookie it overrides the URL params locale */ + if (preferredLocale) { + properties.lang = preferredLocale; + } + if (Object.keys(properties).length) { return configurationReducer(state, applyConfiguration(properties)); } + return state; } /** * meta reducer for overriding client side state if supplied by server */ + export function configurationMeta(reducer: ActionReducer): ActionReducer { + const preferredLocale = SSR ? undefined : inject(CookiesService)?.get('locale'); + let first = false; return ( @@ -69,7 +80,7 @@ export function configurationMeta(reducer: ActionReducer): ActionRedu const payload = action.payload; const paramMap = new SimpleParamMap(payload.routerState.params); newState = mergeDeep(newState, { - configuration: extractConfigurationParameters(newState.configuration, paramMap), + configuration: extractConfigurationParameters(newState.configuration, paramMap, preferredLocale), }); first = true; }