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 about translate directive with input element value

See original GitHub issue

I wonder if there is any workaround to make the translate directive works in this HTML template:

<div class="btn-container">
      <input type="submit" class="btn1" value="Login" translate />
</div>

This actually doesn’t work without using the TranslatePipe like this:

<div class="btn-container">
      <input type="submit" class="btn1" value="{{'Login' | translate}}" />
</div>

This works great, but I want to keep my HTML clean because another person is working on the UI separately and we agreed to some rules between us regarding the localization to use the translate directive when possible.

I made a workaround by using <button type="submit"> element instead of <input>, And it worked. However this will not be acceptable if the CSS is applied to a specific element type like .input[type="submit"] which it is in my case.

With some investigations in the source code, I think the problem comes from here:

    private translate(renderNode: any, nodeValue: string, key: string): void {

        this.localization.translateAsync(key, this.params).subscribe(
            (value: string) => {
                this.renderer.setText(renderNode, nodeValue.replace(key, value));
            }
        );

    }

Since we cannot setText on an input element, I suggest checking the type of the renderNode before setting the text, and to support translating input values we might use ‘setElementAttribute’ function.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
robisim74commented, Feb 8, 2017

@FabienDehopre I will try to fix it in this way: if there is a node value, it translates the node value, otherwise the value attribute: this solution should work also with checkbox and radio. @EslaMx7 If that doesn’t work in all situations, I will revert the feature.

Tonight I will release a new version (I already have another fix to do).

0reactions
EslaMx7commented, Feb 9, 2017

@FabienDehopre Thank you for this important note, it should be working as expected now. @robisim74 That’s really great, Thank you for the fix, everything is making sense now regarding the node value and the value attribute, I hope this fits all.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use translation in a directive field? - Stack Overflow
I used a directive which is using [message] field, and this value needs to be translated, but I do not know how to...
Read more >
How to translate your Angular app with ngx-translate
1. Add ngx-translate to your Angular application 2. Set up the TranslateService in your app.module.ts 3. Create your main language translation file (in...
Read more >
How To Use ngx-translate with Angular - DigitalOcean
The translation file is a JSON object of key-value pairs, where the key describes the text that is translated, and the value is...
Read more >
Directive - Angular
Decorator that marks a class as an Angular directive. You can define your own directives to attach custom behavior to elements in the...
Read more >
Angular ng-content and Content Projection: A Complete Guide ...
Component Design Problem 1 - Supporting all the HTML Properties of an HTML Input; Component Design Problem 2 - Integration with Angular Forms ......
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