Browse Source

refactor(core): improve the getLocalization selector of config state

#3791
pull/3936/head
mehmet-erim 6 years ago
parent
commit
ff09572736
  1. 60
      npm/ng-packs/packages/core/src/lib/states/config.state.ts

60
npm/ng-packs/packages/core/src/lib/states/config.state.ts

@ -155,33 +155,45 @@ export class ConfigState {
} }
const keys = key.split('::') as string[]; const keys = key.split('::') as string[];
const selector = createSelector([ConfigState], (state: Config.State) => { const selector = createSelector([ConfigState], (state: Config.State): string => {
if (!state.localization) return defaultValue || key; const warn = (message: string) => {
if (!state.environment.production) console.warn(message);
const defaultResourceName = snq(() => state.environment.localization.defaultResourceName); };
if (keys[0] === '') {
if (!defaultResourceName) {
throw new Error(
`Please check your environment. May you forget set defaultResourceName?
Here is the example:
{ production: false,
localization: {
defaultResourceName: 'MyProjectName'
}
}`,
);
}
keys[0] = defaultResourceName; if (keys.length < 2) {
warn('The localization source separator (::) not found.');
return key as string;
} }
if (!state.localization) return defaultValue || keys[1];
let localization = (keys as any).reduce((acc, val) => { const sourceName =
if (acc) { keys[0] ||
return acc[val]; snq(() => state.environment.localization.defaultResourceName) ||
} state.localization.defaultResourceName;
const sourceKey = keys[1];
return undefined; if (sourceName === '_') {
}, state.localization.values); return sourceKey;
}
if (!sourceName) {
warn(
'Localization source name is not specified and the defaultResourceName was not defined!',
);
return sourceKey;
}
const source = state.localization.values[sourceName];
if (!source) {
warn('Could not find localization source: ' + sourceName);
return sourceKey;
}
let localization = source[sourceKey];
if (typeof localization === 'undefined') {
return sourceKey;
}
interpolateParams = interpolateParams.filter(params => params != null); interpolateParams = interpolateParams.filter(params => params != null);
if (localization && interpolateParams && interpolateParams.length) { if (localization && interpolateParams && interpolateParams.length) {
@ -191,7 +203,7 @@ export class ConfigState {
} }
if (typeof localization !== 'string') localization = ''; if (typeof localization !== 'string') localization = '';
return localization || defaultValue || key; return localization || defaultValue || (key as string);
}); });
return selector; return selector;

Loading…
Cancel
Save