Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion app-localize-behavior.html
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,30 @@
* @event app-resources-error
*/

/**
* This event is fired when each instance of app-localize-behavior
* is created. It is useful for collecting the instances so that
* later a method can be called on each of them. For an example,
* see demo/app-localize-behavior-mgr.html.
*
* @event app-localize-behavior-ready
*/

/**
* This event is fired every time the language is changed.
* For an example of how this might be used,
* see demo/app-localize-behavior-mgr.html.
*
* @event app-language-changed
*/

properties: {
/**
* The language used for translation.
*/
language: {
type: String
type: String,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, sorry I missed this the first time around: I don't think you need an observer. You can just add notify: true, and this will fire language-changed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried making that change, but it stops working. I suspect it is because notify is only notifying the parent and this.fire is notifying all ancestors. In my case the event must go beyond the immediate parent.

observer: '__languageChanged'
},

/**
Expand Down Expand Up @@ -194,6 +212,10 @@
}
},

ready() {
this.fire('app-localize-behavior-ready', this);
},

/**
* Returns a computed `localize` method, based on the current `language`.
*/
Expand Down Expand Up @@ -227,6 +249,10 @@
};
},

__languageChanged: function() {
this.fire('app-language-changed', this.language);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this one too

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Feel free to improve the wording if you feel there is a better description.

},

__onRequestResponse: function(event) {
this.resources = event.response;
this.fire('app-resources-loaded');
Expand Down
44 changes: 44 additions & 0 deletions demo/app-localize-behavior-mgr.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<link rel="import" href="../../polymer/polymer.html">

<dom-module id="app-localize-behavior-mgr">
<template>
<content></content>
</template>

<script>
Polymer({
is: "app-localize-behavior-mgr",
properties: {
_instances: {
type: Array,
value: function () {
return [];
}
},
_lastLanguage: String
},

listeners: {
'app-localize-behavior-ready': '__addInstance',
'app-language-changed': '__changeLanguage'
},

__addInstance(event) {
this.push('_instances', event.detail);
},

__changeLanguage(event) {
var language = event.detail;
if (language === this._lastLanguage) {
return;
}

this._lastLanguage = language;

this._instances.forEach(function (instance) {
instance.language = language;
});
}
});
</script>
</dom-module>
7 changes: 5 additions & 2 deletions demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,17 @@
<link rel="import" href="../../paper-styles/typography.html">
<link rel="import" href="x-translate.html">
<link rel="import" href="x-local-translate.html">
<link rel="import" href="app-localize-behavior-mgr.html">

<!-- Intl polyfill -->
<script src="../../intl/dist/Intl.min.js"></script>
<script src="../../intl/locale-data/jsonp/en.js"></script>
</head>

<body unresolved>
<x-translate></x-translate>
<x-local-translate></x-local-translate>
<app-localize-behavior-mgr>
<x-translate></x-translate>
<x-local-translate></x-local-translate>
</app-localize-behavior-mgr>
</body>
</html>
7 changes: 6 additions & 1 deletion demo/x-local-translate.html
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ <h4>This demo's resources are loaded statically, not from an external file.</h4>
/* Overriden from AppLocalizeBehavior */
language: {
value: 'en',
type: String
type: String,
observer: '_changeLanguage'
},

/* Overriden from AppLocalizeBehavior */
Expand Down Expand Up @@ -76,6 +77,10 @@ <h4>This demo's resources are loaded statically, not from an external file.</h4>
}
},

_changeLanguage() {
this.$.switch.checked = this.language === 'fr';
Copy link
Contributor

@notwaldorf notwaldorf Aug 29, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this a bit circular? When you flick the toggle, it calls _toggle, which sets this.language, which fires this observer, which re-updates the switch to the same thing? It doesn't look like this is needed.

(same question for x-translate)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is working as expected. What I am seeing is that manually changing the toggle from the UI triggers a call to _toggle which changes this.language. That triggers the language to be changed in all instances of app-localize-behavior which triggers a call to _changeLanguage in the two demo components. _changeLanguage sets the state of the toggle to match the current language. The key seems to be that doing this does not trigger another call to _toggle, so there is no infinite recursion.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it

},

_toggle: function() {
this.language = this.$.switch.checked ? 'fr' : 'en';
}
Expand Down
7 changes: 6 additions & 1 deletion demo/x-translate.html
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ <h4>{{localize('header_2')}}</h4>
/* Overriden from AppLocalizeBehavior */
language: {
value: 'en',
type: String
type: String,
observer: '_changeLanguage'
},

/* Overriden from AppLocalizeBehavior */
Expand All @@ -88,6 +89,10 @@ <h4>{{localize('header_2')}}</h4>
this.loadResources(this.resolveUrl('locales.json'));
},

_changeLanguage() {
this.$.switch.checked = this.language === 'fr';
},

_toggle: function() {
this.language = this.$.switch.checked ? 'fr' : 'en';
}
Expand Down
2 changes: 0 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@

</head>
<body>

<iron-component-page></iron-component-page>

</body>
</html>