core
core copied to clipboard
Translate service returning array with string data only
I have upgraded my Ionic App from Ionic 6 to Ionic 8. Angular version is 19.2
Current behavior
I have below array in en.json file "ARR_TEST": [{ "num": 1, "prediction": "Loss"}, {"num": 2, "prediction": "Meditation"}];
In my .ts file, I'm using below import { TranslateModule, TranslateService } from '@ngx-translate/core'; private translate: TranslateService
this.translate.get('ARR_TEST').subscribe((res: Array<String>) => { console.log(res); });
Here it is returning me array like. "ARR_TEST": [{ "prediction": "Loss"}, {"prediction": "Meditation"}];
so num key is missing from my response array. But if I assign num key value as "1" or "2" i.e. as string, it works well and returns correct array.
It was all working fine in Ionic 6. This happened after upgrading to ionic 8.
Expected behavior
console.log(res) should return below array with num key
"ARR_TEST": [{ "num": 1, "prediction": "Loss"}, {"num": 2, "prediction": "Meditation"}];
How do you think that we should fix this?
No Idea, I searched many articles to understand if there is any ground breaking changes related to this, but couldn't find.
Environment
"@ngx-translate/core": "^16.0.4",
"@ngx-translate/http-loader": "^16.0.1",
"@angular/common": "^19.2.0",
"@angular/core": "^19.2.0",
"@angular/fire": "^19.0.0",
"@angular/forms": "^19.2.0",
"@angular/platform-browser": "^19.2.0",
"@angular/platform-browser-dynamic": "^19.2.0",
"@angular/router": "^19.2.0",
"@ionic/angular": "^8.4.3",
"@ionic/cli": "^7.2.0",
Please help.
This behaviour is on purpose.
The older versions of ngx-translate used "any" everywhere as data type. This made things hard to handle in the code. The new release adds more type safety and uses explicit typing.
Translation files should only contain translatable data. That is: strings. "num": 1 is not translatable. So passing the value through the resolve function etc. does not make much sense - but adds complexity to the interface and library code.
The question is: Why do you put data like this in your translation files?
This behaviour is on purpose.
The older versions of ngx-translate used "any" everywhere as data type. This made things hard to handle in the code. The new release adds more type safety and uses explicit typing.
Translation files should only contain translatable data. That is: strings. "num": 1 is not translatable. So passing the value through the resolve function etc. does not make much sense - but adds complexity to the interface and library code.
The question is: Why do you put data like this in your translation files?
There are Id's in my json records which are numeric. Like I'm having astrology data having sign numbers and house numbers, degrees etc. All those values are used in calculation or logic implementation. So I was using this from last 5 years but with latest upgrades that issue start coming up. Unfortunately I have to return to previous version as upgrading it needs code updates everywhere I'm using such arrays.
If you can suggest anyways to handle it in newer version, let me know.
Would it not make more sense to separate the items?
If it's degrees etc, it's not language dependent, I suppose. So duplicating the data would give you the risk of inconsistencies. So why not move the language independent data to a separate JSON file that is used for all languages and only translate the data that is required?