core: Does not work after update to Angular 7
I updated my app from Angular 6 to Angular 7 and @ngx-translate/core to ^11.0.1. But ngx-translate doesn’t working, no errors… just showing keys instead translations. What can be the reason?
Flow how I load the translations:
import * as translation from '../locale/i18n/en.json';
...
translate.setDefaultLang(environment.locale); // 'en'
translate.setTranslation(environment.locale, translation);
translate.use(environment.locale);
in translate.store has loaded EN translation
About this issue
- Original URL
- State: open
- Created 6 years ago
- Reactions: 5
- Comments: 24
The way I found it to work in Angular 7:
It looks like the returned object is added under the default key tested this in Angular 7 and also tested dynamic change works just fine
Honestly I have found the HTTPTranslateLoader to be a pain in the ass sometimes so I just prefer to use a custom loader like this
Then I go ahead and import it like
import { Observable, from } from 'rxjs'
Works all the time
The issue isn’t with translate. Typescript/Angular stopped allowing imports without defaults (import * as …) without wrapping it in a default property. You need to add the following to compiler options in the tsconfig.
“allowSyntheticDefaultImports”: true, “esModuleInterop”: true
Also, change your imports to “import en from en.json” (not * as) or something similar depending on your lint set up.
Thanks @ajmccallum for your solution, this works fine for me.
I am facing issue with ngx-translate while running in production mode.
I am using
Angular 8
,ngx-translate/core : 11.0.1
&ngx-translate/http-loader: 4.0.0
It is always displaying only key.
In dev mode it works fine using
ng serve
Any help?
Thanks!
The problem is the new version of angular with 7.1 it works just fine but when you update it to 7.2 the problems start to pop up. In my case i use: “@ngx-translate/core”: “^11.0.1”, “@ngx-translate/http-loader”: “^4.0.0”, “@ngx-translate/core”: “^7.2.14” With the same implementation as this https://www.codeandweb.com/babeledit/tutorials/how-to-translate-your-angular7-app-with-ngx-translate. Everything works fine until i subscribe to the LangChangeEvent and the problem starts: Type ‘Subscription’ is missing the following properties from type 'EventEmitter<LangChangeEvent> It looks like EventEmitters are changed in angular 7.2.
@ajmccallum thank you so much! The problem was because of Ivy. I had
"enableIvy": true
in my tsconfig.json. It’s my bad.@Maryna-Yelakova are you using Ivy? It removes pipes in production mode for tree shaking?
Yeh! I was facing this problem with ngx-rocket, in the i18n service made by them, the import of translates is: import enUS from ‘…/…/translations/en-US.json’; Changing to import * as enUS from ‘…/…/translations/en-US.json’; works fine!
Don’t need of this changes:
Thanks @ajmccallum
What’s the difference on using the two approaches?
(useProvider vs useClass)
The first call to
translate.use(..)
doesn’t emit the event, but the second one yes: first one call is not considered as a change. Then, to make the trick, just calltranslate.use(..)
one time to receive next changes in your subscriberFinally, it working fine with me. thanks
I have the same issue. I tried to fix it by following this tutorial: https://www.codeandweb.com/babeledit/tutorials/how-to-translate-your-angular7-app-with-ngx-translate
My dependencies: “@ngx-translate/core”: “^11.0.1”, “@ngx-translate/http-loader”: “^4.0.0”,
Same problem here. My thought that it might be a bug in translate, but downgrading to the old version
doesn’t help.
Maybe it’s some incompability with other packages?
It seems to have something to do with how you initialize your translate file. When you switch a HTTP-loader, it will work again.
old (not working):
Probably its the
import * as de from './de.json';
not working anymore in Angular 7?new (working):