localization - app-localize-behavior 和共享本地化缓存

标签 localization polymer

根据 app-localize-behavior 的 polymer 文档

Each element that displays content to be localized should add Polymer.AppLocalizeBehavior. All of these elements share a common localization cache, so you only need to load translations once.



在以下片段(改编自 this answer )中未找到 中的共享资源标签

也许我错过了什么?

<!DOCTYPE html>
<html>

<head>
  <base href="https://polygit.org/polymer+:master/components/">
  <script src="webcomponentsjs/webcomponents-lite.min.js"></script>
  <script src="https://rawgit.com/yahoo/intl-messageformat/d361003/dist/intl-messageformat.min.js"></script>

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

</head>

<body>
  <x-local-translate></x-local-translate>

  <dom-module id="x-local-translate">
    <template>

      <div>
        <span title="english">🇬🇧</span>
        <paper-toggle-button on-change="_toggle" id="switch"></paper-toggle-button>
        <span title="french">🇫🇷</span>
      </div>

      <div>
        <h4>Outside Repeater</h4>
        <div>
          <div>{{localize('greeting')}}</div>
        </div>

        <h4>Template Repeater Items</h4>
        <template is="dom-repeat" items="{{things}}">
          <div>{{localize('greeting')}}</div>
        </template>


        <x-local-test></x-local-test>
      </div>
    </template>

    <script>
      Polymer({
        is: "x-local-translate",
        behaviors: [
          Polymer.AppLocalizeBehavior
        ],
        properties: {
          things: {
            type: Array,
            value: function() {
              return [1, 2, 3];
            }
          },

          /* Overriden from AppLocalizeBehavior */
          language: {
            value: 'en',
            type: String
          },

          /* Overriden from AppLocalizeBehavior */
          resources: {
            type: Object,
            value: function() {
              return {
                'en': {
                  'greeting': 'Hello!'
                },
                'fr': {
                  'greeting': 'Bonjour!'
                }
              };
            }
          }
        },
        _toggle: function() {
          this.language = this.$.switch.checked ? 'fr' : 'en';
        }
      });
    </script>
  </dom-module>

  <dom-module id="x-local-test">
    <template>
      <h4>Inside x-local-test</h4>
      <div>{{localize('greeting')}}</div>
    </template>

    <script>
      Polymer({
        is: "x-local-test",
        behaviors: [
          Polymer.AppLocalizeBehavior
        ],

        properties: {
          things: {
            type: Array,
            value: function() {
              return [1, 2, 3];
            }
          }
        },

      });
    </script>
  </dom-module>

</body>

</html>


现在在下面的 fiddle 中,我通过传递 使它工作。资源 语言 对象作为 x-local-test 属性。
https://jsfiddle.net/g4evcxzn/2/

但没有它应该可以工作

最佳答案

根据 Jose A. 和 Jean-Rémi 的想法,这里有一些用于复制/粘贴的示例代码:

    <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>

在所有自定义组件中包含行为文件并添加行为:
<link rel="import" href="./my-localize-behavior.html">

......

behaviors: [
    MyLocalizeBehavior
],

关于localization - app-localize-behavior 和共享本地化缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37863942/

相关文章:

polymer 1.0 : how to access a property value inside a function

testing - L10N : Trusted test data for Locale Specific Sorting

.net - C#中如何区分简体中文和繁体中文?

ios - 在不重启应用程序的情况下更改 iOS 中的应用程序语言

javascript - 在自定义元素中使用 Polymer Shadow dom 时出现问题

javascript - 将 CSS 属性分配给 JavaScript [Polymer] 中的变量

iphone - 在iPhone App中管理不同服务器上的国家/地区数据库

c# - 以编程方式创建专业版时如何在 Visual Studio 中查看资源文件内容

javascript - 使用数据绑定(bind)的 polymer 核心支架更改工具

javascript - POST 400(错误请求) polymer 应用程序