Home > Enterprise >  Pass Key inside the value in ng translate using pipe
Pass Key inside the value in ng translate using pipe

Time:10-07

Is there is any way to pass key as value

for example :

<p>
        {{ "TEXT" | translate: { value1: {{"NAME"}} } }}
      </p>

TEXT and NAME are KEYS in my JSON file, and I am trying to pass "NAME" which is a key as a value.

Here is my JSON file, so you will have an idea of my requirements.

{
  "TEXT": "Hello {{value1}}",
  "NAME": "Harry"
}

CodePudding user response:

I never saw this kind of usage, but I would say you can try something like:

<p>{{ "TEXT" | translate: {value1: ("NAME" | translate)} }}</p>

But I don't understand why you are not assigning your "NAME" into a public property in your component and use it in your template as below:

export class MyComponent implements OnInit {
     name: string = '';

     constructor(private translate: TranslateService) {
 
     }

     ngOnInit() {
        this.name = this.translate.instant(`NAME`);
      } 
}

And then in your template:

<p>{{ "TEXT" | translate: {value1: name} }</p>

You just need to be careful when the language change.

  • Related