'No array operations work on getLangs() from ngx-translate/core TranslateService
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):
- Calling
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. - However, I can do absolutely no array operations on this value, including indexing
[0](saysundefined),.length(returns 0), orfor-inorfor-of. No...operator to create a new array or object with the values, noObject.assign([], langs), noArray.from()- they just return empty arrays. Let alone.map(),.forEach(), etc. typeofsays 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.
Questions
- Am I reading the ngx-translate documentation wrong (that what is there)?
- Am I using it wrong somehow due to not understanding something?
- Is this a bug in
ngx-translate? - Is this a bug in JavaScript?
- Is there any workaround?
Solution 1:[1]
Thank you to Amer for his comment pointing me in the right direction.
- The problem occurs because
this.translateService.getLangs()is called before languages have been loaded viathis.translateService.addLangs(). The values are obtained via aHttpClient.get()request, which returns an Observable and callsaddLangs()once the results are received. (So no funny happenings, just a wrong understanding.) - The simple solution is to only do any operations on the array of languages in the
completecallback of the Observable (or use one of the other synchronization mechanisms available). This was a bug in the original code and has now been fixed. - The browser (Chrome) logging
[](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.) - I strongly advise against synchronization via timeouts as someone may wrongly read from Amer's comment, where it is suggested only for problem detection. One should always use one of the synchronization methods (Promises, Observables, events, etc. etc.) in final code to synchronize between asynchronous processes.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | frIT |
