ngx-translate-messageformat-compiler icon indicating copy to clipboard operation
ngx-translate-messageformat-compiler copied to clipboard

A function returned if translations loaded by .get() method.

Open rasulovdev opened this issue 6 years ago • 6 comments

Translations returned as functions when I try to load them async. So I need to execute the key to access the value.

this.translate.get('shared').subscribe(translations => {
    console.log(translations.key) // Function { return 'Value' }
    console.log(translations.key()) // Value
});

Is it intended to work like this?

rasulovdev avatar Nov 26 '19 09:11 rasulovdev

From the ngx-translate README:

get(key: string|Array<string>, interpolateParams?: Object): Observable<string|Object>: Gets the translated value of a key (or an array of keys) or the key if the value was not found

So no, I don't think that's expected. This most probably needs to be investigated and fixed in @ngx-translate/core. I don't have time to look into that at the moment, unfortunately. If you decide to tackle this yourself, @AzamatRasulov, feel free to let me know your findings and I can try to support the effort.

lephyrus avatar Nov 28 '19 07:11 lephyrus

This most probably needs to be investigated and fixed in @ngx-translate/core.

This behaviour occur only if I use TranslateMessageFormatCompiler. The default ngx-translate TranslateCompiler doesn't produce this issue. I should've mentioned it initially, sorry.

rasulovdev avatar Nov 28 '19 08:11 rasulovdev

@AzamatRasulov Oh, absolutely - it wouldn't affect the default compiler. In order to do be able to write this plugin (which does very little - look at the code), I've submitted a PR to get two things into @ngx-translate/core:

  • a plugable compile step (which does nothing by default, but with this plugin uses messageformat to compile the ICU syntax to a function)
  • support for "function interpolation" ((expr: string, params: any) => string) in addition to "string interpolation" ((expr: Function, params: any) => string)

From what you're saying, get doesn't seem to work correctly with function interpolation.

lephyrus avatar Nov 28 '19 09:11 lephyrus

@lephyrus, ok, I'll try to discuss this in @ngx-translate/core repo. Thanks.

rasulovdev avatar Nov 29 '19 06:11 rasulovdev

@AzamatRasulov By chance, did you have any luck with this? I'm having the same problem and have no idea what went wrong...

Saucistophe avatar Mar 08 '22 13:03 Saucistophe

Also found a reference here. https://forum.primefaces.org/viewtopic.php?t=64937

Saucistophe avatar Mar 08 '22 13:03 Saucistophe