I could not find any documentation in the i18n (for Angular2) that goes beyond showing the translated text in the HTML. What I need to do is to get the translated text directly in my code. In Angular 1 using the ng-translate feature, it was easy to get it using a $translate service. I can't find the equivalent in the new i18n for Angular 2. Am I missing something?
Perhaps this has been answered but the following did it for me.
import { TranslateService } from '@ngx-translate/core';
private translate: TranslateService;
const response= this.translate.instant('HELLO_WORLD');
It seems there are two methods for this:
See notes on their documentation regarding the instant method. https://github.com/ngx-translate/core
This method is synchronous and the default file loader is asynchronous. You are responsible for knowing when your translations have been loaded and it is safe to use this method. If you are not sure then you should use the get method instead.
Since Angular 9 there is an experimental and undocumented $localize tag function. It can be applied to a template string.
greetings = $localize`Hello ${this.world}!`;
Internationalization with @angular/localize
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With