Angular 7: Translate service in component does not work, but in HTML works fine
See original GitHub issueCurrent behavior
In my project, I load the TranslateModule as follows:
app.module.ts
export function HttpLoaderFactory(http: HttpClient) {
return new TranslateHttpLoader(http);
}
@NgModule({
...
imports: [
TranslateModule.forRoot({
loader: {
provide: TranslateLoader,
useFactory: HttpLoaderFactory,
deps: [HttpClient]
}
}),
...
app.component.ts
constructor(
private translate: TranslateService,
private store: Store<AppState>
) {
translate.setDefaultLang('en');
translate.use('en');
}
In the web pages for the app components, translates work correctly. But, in the component Typescript code the translate service returns the key and not the translate. For example:
const filter = this.translate.instant('toolbar.action-filter-options.all-actions');
Expected behavior
Expecting the method call to return the translation for the string.
How do you think that we should fix this?
If this is a user error, I’m not sure what it could be. I use @ngx-translate in another project and it works correctly there.
Minimal reproduction of the problem with instructions
Environment
ngx-translate version: 11.0.1
Angular version: 7.2.0
Browser:
- [X] Chrome (desktop) version 73.0.3683.86
- [ ] Chrome (Android) version XX
- [ ] Chrome (iOS) version XX
- [ ] Firefox version XX
- [ ] Safari (desktop) version XX
- [ ] Safari (iOS) version XX
- [ ] IE version XX
- [ ] Edge version XX
For Tooling issues:
- Node version: v10.15.0
- Platform: Mac
Others:
Issue Analytics
- State:
- Created 4 years ago
- Comments:8
Top Results From Across the Web
Translate service not working on load page - Stack Overflow
On your code, you have this line: translateService.use('en); and it is missing a ' to close the string. – Ismael Miguel · @IsmaelMiguel...
Read more >ngx-translate Documentation - CodeAndWeb
Documentation for ngx-translate the internationalization library for Angular. Covers: TranslateService API, Translate Pipe & Directive. Tutorial included.
Read more >How to translate your Angular app with ngx-translate
1. Add ngx-translate to your Angular application 2. Set up the TranslateService in your app.module.ts 3. Create your main language translation file (in...
Read more >Upgrading from AngularJS to Angular
Pure AngularJS applications can be automatically bootstrapped by using an ng-app directive somewhere on the HTML page. But for hybrid applications, you manually ......
Read more >Best Practices for Angular Localization with SSR
To fix this, we will make the language to be anchor tags to direct to the current URL in the other language, and...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@mcpierce You are calling instant method. It’s possible that translations are not yet loaded when you call it. You could try to use something like translate.get(‘test’) . Then subscribe and wait when translations are loaded and then use instant ().
I think that’s the case, which is strange since on another project doing the same works consistently. But on this project I have the code waiting for a language change before generating the text and it works now.