Dave
Dave

Reputation: 7359

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?

Upvotes: 0

Views: 987

Answers (1)

AhmAD
AhmAD

Reputation: 126

//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']

Upvotes: 1

Related Questions