I work on an Angular (v13) project which uses ngx-translate/core
for i18n. There are multiple languages (15 currently) configured. I find the following very strange behaviour (TranslateService is injected as this.translateService
):
this.translateService.getLangs()
returns an array with the 15 language codes as expected. I can log this value or e.g. configure a dropdown select with these values.[0]
(says undefined
), .length
(returns 0), or for-in
or for-of
. No ...
operator to create a new array or object with the values, no Object.assign([], langs)
, no Array.from()
- they just return empty arrays. Let alone .map()
, .forEach()
, etc.typeof
says object, Array.isArray()
says true.Some basic example code:
ngOnInit(): void {
console.log(
this.translateService.getLangs(),
this.translateService.getLangs().length,
this.translateService.getLangs()[0],
this.translateService.getLangs().includes('en'),
this.translateService.getLangs().includes('aa')
);
}
shows in the console:
[] 0 undefined false false
and if I expand the []
it shows the 15 codes (including 'en'), and length: 15
.
ngx-translate
?Thank you to Amer for his comment pointing me in the right direction.
this.translateService.getLangs()
is called before languages have been loaded via this.translateService.addLangs()
. The language values are obtained via a HttpClient.get()
request, which returns an Observable
and calls addLangs()
once the results are received (refer Observable.subscribe(...)
). (So no funny happenings, just a wrong understanding.)complete
callback of the Observable
returned by HttpClient.get()
(or use one of the other synchronization mechanisms available). This was a bug in the original code and has now been fixed.[]
(which could then be expanded to the full array) was a confusion on my part. When the array is available at logging time, it is logged as expanded, e.g. [ "ar", "en", .... ]
. (By the time I clicked on the expansion widget in the console, the array had obviously been loaded in the mean time, which enables it to be shown on expansion.)