gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 13:22:24 29 4
gpt4 key购买 nike

根据 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/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com