JavaScript内部的本地化
这是完全相同的问题: Polymer已经放弃了对i18n的支持(显然),转而使用format.js及其应用程序本地化行为组件。到目前为止,所有示例都使用数据绑定。但是我需要通过JavaScript的setAttribute方法设置元素的属性。。我怎样才能做到这一点JavaScript内部的本地化,javascript,polymer,polymer-1.0,Javascript,Polymer,Polymer 1.0,这是完全相同的问题: Polymer已经放弃了对i18n的支持(显然),转而使用format.js及其应用程序本地化行为组件。到目前为止,所有示例都使用数据绑定。但是我需要通过JavaScript的setAttribute方法设置元素的属性。。我怎样才能做到这一点 <dom-module id="pgarena-login"> <template> <style is="custom-style" include="pgaren
<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>
登录
登录
功能提交(事件){
Polymer.dom(event.localTarget.parentElement.submit();
}
(功能(){
"严格使用",;
聚合物({
是:'pgarena login',
行为:[
聚合物的应用化行为
],
就绪:函数(){
var$this=这个;
此.login.addEventListener('iron-form-error',函数(事件){
console.log(事件);
if(event.detail.request.xhr.status==400){
Array.prototype.slice.call(Polymer.dom(this.root).queryselectoral('paper-input'))
.forEach(函数(元素){
元素setAttribute(“无效”,true);
//此处有问题的行=>element.setAttribute(“错误消息”$this.localize(“登录失败\用户名无效”);
});
}
});
此.login.addEventListener('iron-form-response',函数(事件){
console.log('chapewn');
console.log(事件);
console.log(event.detail);
});
},
特性:{
行动:{
类型:字符串
}
},
附:函数(){
this.loadResources(this.resolveUrl('../locales.json'));
},
});
})();
您的问题实际上不是关于本地化,而是关于在
上设置属性。具体来说,您希望设置和,但您使用的是.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');
});
我还没能破解它。我什么都试过了,但仍然不知道如何访问它。今天非常感谢!那么我应该替换.propertyName的所有setAttribute()吗?看到这个:。我刚刚了解到,只要属性在元素的定义中存在,就可以使用“.”符号!