I am just letting you know, Ionic 2 team will recommend ng2-translate for localization in the future. https://github.com/driftyco/ionic/issues/7691#issuecomment-240467024
Hello, thanks for letting me know about this!
This is an impure pipe, what I notice that is called on every single component change on the app. Is there any performance benchmark ?
No, but if you can make one that'd be cool !
I just added an example folder with an ionic 2 example, hope this helps !
@albanx like I answered on stackoverflow:
It's not because the pipe is "impure" that is has bad performance. The official async pipe is impure as well, and the TranslatePipe is based on it (the original code is almost the same). It has to be impure in order to resolve promises and observables when the lang changes or when it gets remote translations. When that is not necessary it returns immediately and the impact is minimal.
@ocombe that is not true. I made some benchmark in a simple app with 4 components, each containing 3 to 4 translation pipes. just clicking on an input fields causes all the pipe to be called at least 10 times each, so there were was 4x4x10=160 calls times just on a single click.
I solved the i18n problem of my app by changing the pipe to pure, and added just one parameter to the pipe (in this case was the language). The parameter change trigger the pipe to refresh just once.
Example:
From
to
tr.lang will change when the language is changed.
Yes they are called but it only does two === tests for each, which is very fast. That's why I said minimal.
You can test with an async pipe and it does about the same thing.
Yes, with the lang as a parameter it updates the pipe when the lang changes, but it complicates the templates a lot because you will have to add it everywhere.
I have added the parameter to the translation service as public variable, so yes I have to inject the translationService in all components... But I am a bit stingy :) so I prefer this way for the moment.
You should use the OnPush change detection strategy then
Most helpful comment
@ocombe that is not true. I made some benchmark in a simple app with 4 components, each containing 3 to 4 translation pipes. just clicking on an input fields causes all the pipe to be called at least 10 times each, so there were was 4x4x10=160 calls times just on a single click.
I solved the i18n problem of my app by changing the pipe to pure, and added just one parameter to the pipe (in this case was the language). The parameter change trigger the pipe to refresh just once.
Example:
From
to
tr.lang will change when the language is changed.