core icon indicating copy to clipboard operation
core copied to clipboard

Missing Translations Handler does not consider interpolateParams while returning missing keys

Open yaniv1988 opened this issue 6 years ago • 3 comments

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".

yaniv1988 avatar Mar 08 '19 06:03 yaniv1988

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
    );
  }
}

LukeHowellDev avatar Aug 11 '20 16:08 LukeHowellDev

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: (?).

duzenko avatar Jun 25 '21 15:06 duzenko

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;
}

Corasonn avatar Nov 14 '22 14:11 Corasonn