core
core copied to clipboard
Missing Translations Handler does not consider interpolateParams while returning missing keys
Looking at current implementation of the default handling of missing translations, it always returns the key. In my case, if there are 'interpolateParams' in the params, then they are not being considered. As a result, the string returned is not replaced with the interpolateParams.
Example : Say my key is something like "{{count}} items selected" and interpolateParams is like "{count : 5}". Then the string returned is "{{count}} items selected" instead of "5 items selected".
I was able to deal with this by interpolating inside the missing translation handler
export class CustomMissingTranslationHandler
implements MissingTranslationHandler {
tokens = {};
constructor(private parser: TranslateParser) {}
handle(params: MissingTranslationHandlerParams): any {
return this.parser.interpolate(
'Your translation is missing, {{name}}',
params.interpolateParams
);
}
}
export class CustomMissingTranslationHandler implements MissingTranslationHandler { tokens = {}; constructor(private parser: TranslateParser) {} handle(params: MissingTranslationHandlerParams): any { return this.parser.interpolate( 'Your translation is missing, {{name}}', params.interpolateParams ); } }
I get this error
Error: Can't resolve all parameters for CustomMissingTranslationHandler: (?).
Try this:
handle(params: MissingTranslationHandlerParams): string {
const result = params.key.split(".").reduce((pre, cur) => !!pre ? pre[cur] : null, this.commonTranslation);
return !!result ? params.translateService.parser.interpolate(result, params.interpolateParams) : params.key;
}