【问题标题】:Localization inside JavaScriptJavaScript 中的本地化
【发布时间】:2016-07-24 12:19:16
【问题描述】:

这是完全相同的问题: Localization/Internationalization for Polymer 1.0

Polymer 已经放弃了对 i18n 的支持(显然),并开始使用 format.js 及其 app-localize-behavior 组件。到目前为止,所有示例都使用数据绑定。但是我需要通过 JavaScript 的 setAttribute 方法来设置一个元素的属性。我该如何实现呢?

    <dom-module id="pgarena-login">
      <template>
        <style is="custom-style" include="pgarena-login-styles"></style>
        <style>

        </style>
        <form is="iron-form" method="post" action$="{{action}}" id="login">
           <h2>Login</h2>
          <paper-input label="Username" required tabindex="0" name="UserName"></paper-input>
          <paper-input label="Password" type="password" required tabindex="0" name="Password"></paper-input>
          <!-- Anti Forgery Token -->
          <content></content>
          <paper-button onclick="_submit(event)">Login</paper-button>
        </form>
      </template>
      <script>
          function _submit(event) {
              Polymer.dom(event).localTarget.parentElement.submit();
          }

      (function() {
          'use strict';
           Polymer({
               is: 'pgarena-login',
               behaviors: [
                Polymer.AppLocalizeBehavior
               ],
               ready: function () {

                   var $this = this;

           this.$.login.addEventListener('iron-form-error', function (event) {
                       console.log(event);
                       if (event.detail.request.xhr.status === 400) {
                           Array.prototype.slice.call(Polymer.dom(this.root).querySelectorAll('paper-input'))
                               .forEach(function(element) {
                                   element.setAttribute("invalid", true);
//Problematic Line here=>                                    element.setAttribute("error-message",         $this.localize("Login_Fail_UsernameInvalid"));
                               });
                       }
                   });

                   this.$.login.addEventListener('iron-form-response', function (event) {
                       console.log('chapewn');
                       console.log(event);
                       console.log(event.detail);
                   });
               },
            properties : {
              action : {
                type: String
              }
            },
            attached: function () {
                this.loadResources(this.resolveUrl('../locales.json'));
            },
          });
        })();

      </script>
    </dom-module>

【问题讨论】:

  • 我还没有破解它。我已经尝试了所有方法,但仍然不知道如何访问它。

标签: javascript polymer polymer-1.0


【解决方案1】:

您的问题并不是关于本地化,而是关于在&lt;paper-input&gt; 上设置属性。具体来说,您想设置&lt;paper-input&gt;.invalid&lt;paper-input&gt;.errorMessage,但您错误地使用.setAttribute() 而不是直接赋值:

// incorrect
el.setAttribute('error-message', 'Invalid username/password');

// correct
el.errorMessage = 'Invalid username/password';

在您的代码中,您将替换为:

Array.prototype.slice.call(Polymer.dom(this.root).querySelectorAll('paper-input'))
  .forEach(function(element) {
    element.setAttribute("invalid", true);
    element.setAttribute("error-message", $this.localize("Login_Fail_UsernameInvalid"));
  });

用这个:

[].slice.call(this.querySelectorAll('paper-input')).forEach(function(element) {
    element.invalid = true;
    element.errorMessage = $this.localize('Login_Fail_UsernameInvalid');
  });

codepen

【讨论】:

  • 非常感谢 tony19!那么我应该替换 .propertyName 的所有 setAttribute() 吗?
  • 看到这个:stackoverflow.com/questions/3919291/…。我刚刚了解到,只要元素的定义中存在属性,就可以使用“。”签到!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-03-06
  • 1970-01-01
  • 2023-01-29
  • 2010-10-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多