question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Question: How to dynamically construct translation keys for input field placeholder attributes?

See original GitHub issue

I’m submitting a … (check one with “x”)

[ ] bug report => check the FAQ and search github for a similar issue or PR before submitting
[x] support request => check the FAQ and search github for a similar issue before submitting
[ ] feature request

Current behavior For a long form I am generating input fields dynamically using an ngFor loop and would like to translate field labels and placeholders dynamically. For this I am trying to generate the translation keys dynamically. My current attempt in html code looks like this:

<div *ngFor="let field of componentFormFields">
    <ion-item>
        <ion-label stacked translate>bike-passport.label.{{ field.key }}</ion-label>
        <ion-input type="{{ field.type }}"
                 formControlName="{{ field.key }}"
                 placeholder="{{ 'bike-passport.label.{{ field.key }}' | translate }}"></ion-input>
    </ion-item>
</div>

I am trying to interpolate the “field.key” property to construct the appropriate translation key which works fine for the ion-label element above. An example for a generated key would be “bike-passport.label.manufacturer”. Of course, the above doesn’t work for the placeholder attribute as I am not using the translate directive as I am doing for the ion-label element.

Therefore I’d like to kindly ask if anyone knows a way to accomplish what I am trying to do, which is to dynamically construct the translation key for the placeholder attribute, e.g. “bike-passport.label.manufacturer”.

Thanks very much in advance for any feedback.

Please tell us about your environment:

  • ngx-translate version: 6.0.1

  • Angular version: 4.1.0

  • Browser: all

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:4
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

66reactions
tsvetan-ganevcommented, May 4, 2017

Simple string concatenation should do the trick: placeholder="{{ ('bike-passport.label' + field.key) | translate }}"

9reactions
eyereancommented, Jul 10, 2017

I think this should be added to the documentation. Even though it’s more of an Angular thing, it would help a lot newcomers.

Read more comments on GitHub >

github_iconTop Results From Across the Web

angularjs - how to translate the html5 placeholders dynamically
This answer is close, but technically it is a better practice to use ng-attr-placeholder as the attribute because then Angular can hold off...
Read more >
Don't Use The Placeholder Attribute - Smashing Magazine
If a person is requesting a page to be translated, the expectation is that all visible page content will be updated. Placeholders are...
Read more >
Forms - input placeholder attribute - Introduction to Localize
1. get the value of placeholder · 2. make a. Localize.translate() call to translate that value · 3. use some jQuery to re-set...
Read more >
Angular I18n: How to Internationalize with ngx-translate | Phrase
Dynamic translation keys ​​ For the dropdown of the gender field in the form, we are creating the translation keys dynamically using a...
Read more >
How to change the placeholder text using jQuery?
The placeholder text is the text output for the user what is that input for. Like in HTML, we use Name as an...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found