2016-02-05 34 views
2

我试图使我的自定义i18n行为(目前没有适合我需要的i18n组件,我检查i18n-msg,i18n-next在stackoverflow上找到一些自定义实现...) 。聚合物1.0的行为变化没有传播

它适用于第一代页面,但当我尝试更改我的语言时,标签不会更新。

这里我的代码:

行为:

var Polymer = Polymer || {}; 
    /* Defining behavior's */ 
    Polymer.I18n = Polymer.I18n ||{ 
     properties: { 
      lang: { 
       type: String, 
       value: "en", 
       observer: '_onChangedLocale' 
      }, 
      /** Exposes all translations */ 
      translation: { 
       type: Object, 
      } 
     }, 
     created: function() { 
      /* Initialisation */ 
      this._onChangedLocale(Polymer.I18n.lang); 
     }, 
     _onChangedLocale: function(locale) { 
      switch(locale) { 
       case 'de': 
        Polymer.I18n.translation = i18nDE; 
        break; 
       default: 
        Polymer.I18n.translation = i18nEN; 
      } 
     }, 
     i18n: function(key) { 
      return Polymer.I18n.translation[key] ? Polymer.I18n.translation[key] : key; 
     } 
} 

组件实现的行为:

<dom-module id="my-page"> 
    <template> 
     [[i18n('hello')]] 
     <span on-click="onChangeLanguageDEClick">DE</span> 
    </template> 
    <script> 
     Polymer({ 
      is: "my-page", 
      /* use localisation */ 
      behaviors: [Polymer.I18n], 
      onChangeLanguageDEClick:function (event, detail, sender){ 
       this.lang = 'de'; 
      }, 
     }); 
    </script> 
</dom-module> 

i18nDE等i18nEN是的JSONObject包含翻译。

当我点击'DE'时,_onChangedLocale被调用,Polymer.I18n.translation被更新,但不是'[[i18n('hello')]]',如果没有任何事情发生,他会保持第一个值。

我错过了什么吗?聚合物文档讲'notifyPath',但我不明白如何在我的情况下使用它

解决方案: 问题是生命周期。 聚合物在组件属性初始化之前对dom和数据绑定进行评估......当您使用this.my-prop时,您得到的错误表明您的属性未定义(如果您未使用“创建的”回调)。或者像我这样做,它只能在第一时间运作,但改变永远不会受到影响。

它绝对没有记录,也没有看到其他有关它的信息,但为了摆脱这一点,我给第二个参数给我[[i18n('hello')]] => [[i18n('hello' ,翻译)]]。函数i18n仍然只用于参数,但是这样做,您告诉聚合函数,该函数使用特性“翻译”,使他能够更改生命周期并在特性初始化后评估表达式。现在它按预期工作...

回答

0

谢谢您的回答,遗憾的是这是我第一次尝试(基于https://www.polymer-project.org/1.0/docs/devguide/behaviors.html),它不工作也没有。

我找出问题(这不是行为相对聚合物):

看我的国际化组件的这种极简代码:

<dom-module id="my-page"> 
<template> 
    [[expr('toto',word)]] 
    <span on-click="onClickButton">Change plz</span> 
</template> 
<script> 
    Polymer({ 
     is: "my-page", 
     /* use localisation */ 
     properties : { 
      word: { 
       type: Object, 
      } 
     }, 
     ready: function() { 
      this.onChange('Try it'); 
     }, 
     onClickButton:function (event, detail, sender, arg){ 
      this.onChange('CHANGED !'); 
     }, 
     onChange:function (arg){ 
      var json = {}; 
      json.toto = arg; 
      this.word = json; 
     }, 
     expr:function(arg) { 
      return this.word[arg]; 
     } 
    }); 
</script> 

此代码的工作(当我在“变点击绑定更新如预期),但我不明白为什么,因为我在这条线上犯了一个错误: [[expr('toto',word)]]。

我的函数'expr'没有采用第二个参数,并猜测是什么。如果我像[[expr('toto')]]一样去除它,它就不再工作了(无法读取未定义的属性'toto')!oO

有人可以解释我这种行为,然后我就可以将它扩展到我的i18n组件?

0

我强烈建议有你自己的命名空间,而不是使用聚合物。 如果您的行为定义现在不存在,请将import添加到polymer.html中。

尝试做出以下更改,看看它是否有效。

/* Defining behavior's */ 
    Polymer.I18n = Polymer.I18n ||{ 
     properties: { 
      lang: { 
       type: String, 
       value: "en", 
       observer: '_onChangedLocale' 
      }, 
      /** Exposes all translations */ 
      translation: { 
       type: Object, 
      } 
     }, 
     created: function() { 
      /* Initialisation */ 
      this._onChangedLocale(this.lang); 
     }, 
     _onChangedLocale: function(locale) { 
      switch(locale) { 
       case 'de': 
        this.translation = i18nDE; 
        break; 
       default: 
        this.translation = i18nEN; 
      } 
     }, 
     i18n: function(key) { 
      return this.translation[key] ? this.translation[key] : key; 
     }