feat: add createLocalizationPipeKeyGenerator

pull/3996/head
Arman Ozak 5 years ago
parent 6eb28e35c9
commit 024a640157

@ -1,4 +1,8 @@
import { createLocalizer, createLocalizerWithFallback } from '../utils/localization-utils';
import {
createLocalizationPipeKeyGenerator,
createLocalizer,
createLocalizerWithFallback,
} from '../utils/localization-utils';
describe('Localization Utils', () => {
describe('#createLocalizer', () => {
@ -87,4 +91,50 @@ describe('Localization Utils', () => {
},
);
});
describe('#createLocalizationPipeKeyGenerator', () => {
const generateLocalizationPipeKey = createLocalizationPipeKeyGenerator({
values: { foo: { bar: 'baz' }, x: { y: 'z' } },
defaultResourceName: 'x',
currentCulture: null,
languages: [],
});
test.each`
resources | keys | defaultKey | expected
${['', '_']} | ${['TEST', 'OTHER']} | ${'DEFAULT'} | ${'TEST'}
${['foo']} | ${['bar']} | ${'DEFAULT'} | ${'foo::bar'}
${['x']} | ${['bar']} | ${'DEFAULT'} | ${'DEFAULT'}
${['a', 'b', 'c']} | ${['bar']} | ${'DEFAULT'} | ${'DEFAULT'}
${['']} | ${['bar']} | ${'DEFAULT'} | ${'DEFAULT'}
${[]} | ${['bar']} | ${'DEFAULT'} | ${'DEFAULT'}
${['foo']} | ${['y']} | ${'DEFAULT'} | ${'x::y'}
${['x']} | ${['y']} | ${'DEFAULT'} | ${'x::y'}
${['a', 'b', 'c']} | ${['y']} | ${'DEFAULT'} | ${'x::y'}
${['']} | ${['y']} | ${'DEFAULT'} | ${'x::y'}
${[]} | ${['y']} | ${'DEFAULT'} | ${'x::y'}
${['foo']} | ${['bar', 'y']} | ${'DEFAULT'} | ${'foo::bar'}
${['x']} | ${['bar', 'y']} | ${'DEFAULT'} | ${'x::y'}
${['a', 'b', 'c']} | ${['bar', 'y']} | ${'DEFAULT'} | ${'x::y'}
${['']} | ${['bar', 'y']} | ${'DEFAULT'} | ${'x::y'}
${[]} | ${['bar', 'y']} | ${'DEFAULT'} | ${'x::y'}
${['foo']} | ${['']} | ${'DEFAULT'} | ${'DEFAULT'}
${['x']} | ${['']} | ${'DEFAULT'} | ${'DEFAULT'}
${['a', 'b', 'c']} | ${['']} | ${'DEFAULT'} | ${'DEFAULT'}
${['']} | ${['']} | ${'DEFAULT'} | ${'DEFAULT'}
${[]} | ${['']} | ${'DEFAULT'} | ${'DEFAULT'}
${['foo']} | ${[]} | ${'DEFAULT'} | ${'DEFAULT'}
${['x']} | ${[]} | ${'DEFAULT'} | ${'DEFAULT'}
${['a', 'b', 'c']} | ${[]} | ${'DEFAULT'} | ${'DEFAULT'}
${['']} | ${[]} | ${'DEFAULT'} | ${'DEFAULT'}
${[]} | ${[]} | ${'DEFAULT'} | ${'DEFAULT'}
`(
'should return $expected when resource names are $resources and keys are $keys',
({ resources, keys, defaultKey, expected }) => {
const result = generateLocalizationPipeKey(resources, keys, defaultKey);
expect(result).toBe(expected);
},
);
});
});

@ -22,6 +22,18 @@ export function createLocalizerWithFallback(localization: ApplicationConfigurati
};
}
export function createLocalizationPipeKeyGenerator(
localization: ApplicationConfiguration.Localization,
) {
const findLocalization = createLocalizationFinder(localization);
/* tslint:disable-next-line:only-arrow-functions */
return function(resourceNames: string[], keys: string[], defaultKey: string) {
const { resourceName, key } = findLocalization(resourceNames, keys);
return !resourceName ? defaultKey : resourceName === '_' ? key : `${resourceName}::${key}`;
};
}
function createLocalizationFinder(localization: ApplicationConfiguration.Localization) {
const localize = createLocalizer(localization);

Loading…
Cancel
Save