'angular: From translation file in i18n of ngx-translate, how can I get all keys?
I am using Angular 11 and I need how to get all the keys of json file of the selected language using ngx-translate-core library.
I need it because I need to filter the keys for the current page loaded.
How can I do it?
Solution 1:[1]
//1- inject 'TranslateService' in the 'constructor' of the Component that //you want to call the keys
constructor(private translate: TranslateService) {}
//2- in 'ngOnInit'
this.translate.getTranslation(this.translate.currentLang)
.subscribe(transAsJson => {
let allKeysAsString = Object.keys(transAsJson);
console.log(allKeysAsString)
});
that will get you all the keys for the current language but not the complex keys for sub-objects in the JSON you will have to check if the value of the key is complex or not .. for an example if the JSON file value for the current language is
{ "first": "valuee", "second": "valuee", "FirstSub": { "FirstSub_first": "valuee", "FirstSub_second": "valuee" } }
the result should be like
['first', 'second', 'FirstSub']
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 | AhmAD |
