app-localize-behavior and shared localization cache

混江龙づ霸主 提交于 2019-12-05 07:19:00

I took a look at AppLocaleBehavior's demo and if you actually look at the repo, they use two elements for it, one that loads the resources from an external json and one more that has them locally defined and in the demo, the don't seem to be sharing a cache, just as what's happening to you.

This struck me as odd seeing that they do mention the cache, so I took a look at the behavior's code and found out something interesting, the cache actually exists but it seems its purpose is to prevent loading the same external resource multiple times rather than sharing the resources property.

So, if you want to share localization resources on multiple elements the way to go would be having a common resource (let's say we call it locales.json) and call the loadResources function on every one of them (Since the request is cached you don't need to worry about loading the file multiple times). You could do it on the attached callback like this:

attached: function () {
  this.loadResources(this.resolveUrl('locales.json'));
}

According the ideas of Jose A. and Jean-Rémi here some example code for copy/paste:

    <link rel="import" href="../bower_components/polymer/polymer.html">
    <link rel="import" href="../bower_components/app-localize-behavior/app-localize-behavior.html">

    <script>
      MyLocalizeBehaviorImpl = {
        properties: {
          language: {
            value: 'de'
          }
        },
        attached: function() {
          this.loadResources(this.resolveUrl('locales.json'));
        }
      };
      MyLocalizeBehavior = [MyLocalizeBehaviorImpl, Polymer.AppLocalizeBehavior]; 
    </script>

Include the behavior file in all your custom components and add the behavior:

<link rel="import" href="./my-localize-behavior.html">

......

behaviors: [
    MyLocalizeBehavior
],
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!