From b6ab880ce248811dafe454d74511ef8c823cf0d3 Mon Sep 17 00:00:00 2001 From: "R. Mark Volkmann" Date: Mon, 25 Jul 2016 17:03:13 -0500 Subject: [PATCH 1/3] added ability to change language of all app-localize-behavior instances fixed demo toggles to switch on language change simplified based on feedback from notwaldorf changed an event name --- app-localize-behavior.html | 11 +++++++- demo/app-localize-behavior-mgr.html | 40 +++++++++++++++++++++++++++++ demo/index.html | 7 +++-- demo/x-local-translate.html | 7 ++++- demo/x-translate.html | 7 ++++- index.html | 2 -- 6 files changed, 67 insertions(+), 7 deletions(-) create mode 100644 demo/app-localize-behavior-mgr.html diff --git a/app-localize-behavior.html b/app-localize-behavior.html index a990747..3dc885e 100644 --- a/app-localize-behavior.html +++ b/app-localize-behavior.html @@ -115,7 +115,8 @@ * The language used for translation. */ language: { - type: String + type: String, + observer: '__languageChanged' }, /** @@ -194,6 +195,10 @@ } }, + ready() { + this.fire('app-localize-behavior-created', this); + }, + /** * Returns a computed `localize` method, based on the current `language`. */ @@ -227,6 +232,10 @@ }; }, + __languageChanged: function() { + this.fire('app-language-changed', this.language); + }, + __onRequestResponse: function(event) { this.resources = event.response; this.fire('app-resources-loaded'); diff --git a/demo/app-localize-behavior-mgr.html b/demo/app-localize-behavior-mgr.html new file mode 100644 index 0000000..85c5219 --- /dev/null +++ b/demo/app-localize-behavior-mgr.html @@ -0,0 +1,40 @@ + + + + + + + diff --git a/demo/index.html b/demo/index.html index a25f461..6c4068a 100644 --- a/demo/index.html +++ b/demo/index.html @@ -20,6 +20,7 @@ + @@ -27,7 +28,9 @@ - - + + + + diff --git a/demo/x-local-translate.html b/demo/x-local-translate.html index afd2014..7bb6406 100644 --- a/demo/x-local-translate.html +++ b/demo/x-local-translate.html @@ -47,7 +47,8 @@

This demo's resources are loaded statically, not from an external file.

/* Overriden from AppLocalizeBehavior */ language: { value: 'en', - type: String + type: String, + observer: '_changeLanguage' }, /* Overriden from AppLocalizeBehavior */ @@ -76,6 +77,10 @@

This demo's resources are loaded statically, not from an external file.

} }, + _changeLanguage() { + this.$.switch.checked = this.language === 'fr'; + }, + _toggle: function() { this.language = this.$.switch.checked ? 'fr' : 'en'; } diff --git a/demo/x-translate.html b/demo/x-translate.html index cd90baa..0881792 100644 --- a/demo/x-translate.html +++ b/demo/x-translate.html @@ -70,7 +70,8 @@

{{localize('header_2')}}

/* Overriden from AppLocalizeBehavior */ language: { value: 'en', - type: String + type: String, + observer: '_changeLanguage' }, /* Overriden from AppLocalizeBehavior */ @@ -88,6 +89,10 @@

{{localize('header_2')}}

this.loadResources(this.resolveUrl('locales.json')); }, + _changeLanguage() { + this.$.switch.checked = this.language === 'fr'; + }, + _toggle: function() { this.language = this.$.switch.checked ? 'fr' : 'en'; } diff --git a/index.html b/index.html index 0b14f7c..6330210 100644 --- a/index.html +++ b/index.html @@ -23,8 +23,6 @@ - - From 9035757c6a18312ee9f152f8b31fe33744bbb2fa Mon Sep 17 00:00:00 2001 From: "R. Mark Volkmann" Date: Sun, 4 Sep 2016 15:15:49 -0500 Subject: [PATCH 2/3] made several changes suggested by @notwaldorf on 8/29/16 --- app-localize-behavior.html | 17 +++++++++++++++++ demo/app-localize-behavior-mgr.html | 12 ++++++++---- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/app-localize-behavior.html b/app-localize-behavior.html index 3dc885e..621ac3b 100644 --- a/app-localize-behavior.html +++ b/app-localize-behavior.html @@ -110,6 +110,23 @@ * @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-created + */ + + /** + * 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. diff --git a/demo/app-localize-behavior-mgr.html b/demo/app-localize-behavior-mgr.html index 85c5219..4f5623a 100644 --- a/demo/app-localize-behavior-mgr.html +++ b/demo/app-localize-behavior-mgr.html @@ -11,7 +11,9 @@ properties: { _instances: { type: Array, - value: () => [] + value: function () { + return []; + } }, _lastLanguage: String }, @@ -26,12 +28,14 @@ }, _changeLanguage(event) { - const language = event.detail; - if (language === this._lastLanguage) return; + var language = event.detail; + if (language === this._lastLanguage) { + return; + } this._lastLanguage = language; - this._instances.forEach(instance => { + this._instances.forEach(function (instance) { instance.language = language; }); } From 7547150adac5e128859929bd02a0d32522eb1132 Mon Sep 17 00:00:00 2001 From: "R. Mark Volkmann" Date: Thu, 29 Sep 2016 10:27:50 -0500 Subject: [PATCH 3/3] changed an event name --- app-localize-behavior.html | 4 ++-- demo/app-localize-behavior-mgr.html | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/app-localize-behavior.html b/app-localize-behavior.html index 621ac3b..7448cd5 100644 --- a/app-localize-behavior.html +++ b/app-localize-behavior.html @@ -116,7 +116,7 @@ * later a method can be called on each of them. For an example, * see demo/app-localize-behavior-mgr.html. * - * @event app-localize-behavior-created + * @event app-localize-behavior-ready */ /** @@ -213,7 +213,7 @@ }, ready() { - this.fire('app-localize-behavior-created', this); + this.fire('app-localize-behavior-ready', this); }, /** diff --git a/demo/app-localize-behavior-mgr.html b/demo/app-localize-behavior-mgr.html index 4f5623a..18ef456 100644 --- a/demo/app-localize-behavior-mgr.html +++ b/demo/app-localize-behavior-mgr.html @@ -19,15 +19,15 @@ }, listeners: { - 'app-localize-behavior-created': '_addInstance', - 'app-language-changed': '_changeLanguage' + 'app-localize-behavior-ready': '__addInstance', + 'app-language-changed': '__changeLanguage' }, - _addInstance(event) { + __addInstance(event) { this.push('_instances', event.detail); }, - _changeLanguage(event) { + __changeLanguage(event) { var language = event.detail; if (language === this._lastLanguage) { return;