-
Notifications
You must be signed in to change notification settings - Fork 55
added ability to change language of all app-localize-behavior instances #44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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, | ||
| observer: '__languageChanged' | ||
| }, | ||
|
|
||
| /** | ||
|
|
@@ -194,6 +212,10 @@ | |
| } | ||
| }, | ||
|
|
||
| ready() { | ||
| this.fire('app-localize-behavior-ready', this); | ||
| }, | ||
|
|
||
| /** | ||
| * Returns a computed `localize` method, based on the current `language`. | ||
| */ | ||
|
|
@@ -227,6 +249,10 @@ | |
| }; | ||
| }, | ||
|
|
||
| __languageChanged: function() { | ||
| this.fire('app-language-changed', this.language); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this one too
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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'); | ||
|
|
||
| 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> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 */ | ||
|
|
@@ -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'; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 (same question for
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Got it |
||
| }, | ||
|
|
||
| _toggle: function() { | ||
| this.language = this.$.switch.checked ? 'fr' : 'en'; | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,8 +23,6 @@ | |
|
|
||
| </head> | ||
| <body> | ||
|
|
||
| <iron-component-page></iron-component-page> | ||
|
|
||
| </body> | ||
| </html> | ||
There was a problem hiding this comment.
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 firelanguage-changedThere was a problem hiding this comment.
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.