Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Pass Key inside the value in ng translate using pipe

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

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

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading