Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular translate on custom attributes

I have a switch widget which uses custom data attribute value to label itself.

.switch.switch-text .switch-label::before {
  right: 1px;
  color: #c2cfd6;
  content: attr(data-hide);
  }
  
  .switch.switch-text .switch-label::after {
  left: 1px;
  color: #c2cfd6;
  content: attr(data-show);
  opacity: 0;
}

.switch.switch-text .switch-input:checked ~ .switch-label::before {
  opacity: 0;
}

.switch.switch-text .switch-input:checked ~ .switch-label::after {
  opacity: 1;
}
<label class="switch switch-text switch-pill switch-primary">
                            <input type="checkbox" class="switch-input" checked>
                            <span class="switch-label" attr.data-show="{{GLOBALS.ACTIONS.SHOW | translate}}" attr.data-hide="{{GLOBALS.ACTIONS.HIDE | translate}}"></span>
                            <span class="switch-handle"></span>
                        </label>

But it just does not work. I have looked at different answers relating to similar problem but some says it works some says it doesn't. If I user without attr. in front I get a binding error anyway since it does not recognise the attribute.

How can I translate the value of custom attribute using angular translate?

like image 949
tekin Avatar asked Feb 19 '18 12:02

tekin


People also ask

What is the use of translate in Angular?

NGX-Translate is an internationalization library for Angular. Internationalization is the process of translating an application into multiple languages. Using this library, we can translate our application language into multiple languages. This will work with not only static data, but dynamic data as well.

What is the use of translate pipe in Angular?

The service also contains a method called translate. It is later called by the pipe to get the translation for a specific key. It always uses the language specified in the 'language'-variable of the service, to lookup the translation. That's it.


1 Answers

You just have a typo in your template. Your have to use a one way data binding syntax to update attributes "data-show" with the translated value. If you omit the bracket then you just create a static "attr.data-show" attribute whom value is "{{GLOBALS.ACTIONS.SHOW | translate}}"

Your code produces :

<span class="switch-label" attr.data-show="{{GLOBALS.ACTIONS.SHOW | translate}}" attr.data-hide="{{GLOBALS.ACTIONS.HIDE | translate}}"></span>

The corrected template syntax is :

  <label class="switch switch-text switch-pill switch-primary">
        <input type="checkbox" class="switch-input" checked>
        <span class="switch-label" [attr.data-show]="'GLOBALS.ACTIONS.SHOW' | translate" [attr.data-hide]="'GLOBALS.ACTIONS.HIDE' | translate"></span>
        <span class="switch-handle"></span>
   </label>
like image 199
Pierre Mallet Avatar answered Sep 30 '22 15:09

Pierre Mallet