From 2618ae252d5072a3d7b321e4617afedc18eba4d6 Mon Sep 17 00:00:00 2001 From: Aleksandar Toshovski Date: Tue, 5 Dec 2017 00:22:05 +0100 Subject: [PATCH 1/9] Add fallback language for localization --- app-localize-behavior.html | 24 +++++++++++- demo/index.html | 2 + demo/locales.json | 6 ++- demo/x-fallback.html | 71 ++++++++++++++++++++++++++++++++++ demo/x-translate.html | 1 + test/basic.html | 20 ++++++++++ test/x-translate-fallback.html | 53 +++++++++++++++++++++++++ 7 files changed, 174 insertions(+), 3 deletions(-) create mode 100644 demo/x-fallback.html create mode 100644 test/x-translate-fallback.html diff --git a/app-localize-behavior.html b/app-localize-behavior.html index cf4dd21..180b82e 100644 --- a/app-localize-behavior.html +++ b/app-localize-behavior.html @@ -151,6 +151,21 @@ type: String }, + /** + * Fallback Language if the current language is not yet translated. + * + * For example having only english translation, but trying to open a french localization will return the fallback language + * + * this.resources = { + * 'en': { 'greeting': 'Hello!' } + * } + * + * + */ + fallbackLanguage:{ + type: String + }, + /** * The dictionary of localized messages, for each of the languages that * are going to be used. See http://formatjs.io/guides/message-syntax/ for @@ -195,7 +210,7 @@ */ localize: { type: Function, - computed: '__computeLocalize(language, resources, formats)' + computed: '__computeLocalize(language, fallbackLanguage, resources, formats)' }, /** @@ -245,7 +260,7 @@ /** * Returns a computed `localize` method, based on the current `language`. */ - __computeLocalize: function(language, resources, formats) { + __computeLocalize: function(language, fallbackLanguage, resources, formats) { var proto = this.constructor.prototype; // Check if localCache exist just in case. @@ -262,9 +277,14 @@ if (!key || !resources || !language || !resources[language]) return; + // Cache the key/value pairs for the same language, so that we don't // do extra work if we're just reusing strings across an application. var translatedValue = resources[language][key]; + //use fallback key if provided + if(!translatedValue && fallbackLanguage){ + translatedValue = resources[fallbackLanguage][key]; + } if (!translatedValue) { return this.useKeyIfMissing ? key : ''; diff --git a/demo/index.html b/demo/index.html index 2448d0e..53e0054 100644 --- a/demo/index.html +++ b/demo/index.html @@ -19,6 +19,7 @@ + @@ -30,5 +31,6 @@ + diff --git a/demo/locales.json b/demo/locales.json index f434899..d1e2b98 100644 --- a/demo/locales.json +++ b/demo/locales.json @@ -7,7 +7,8 @@ "cats": "I have {numCats, number} cats. Almost {pctBlack, number, percent} of them are black.", "sale": "Sale begins {start, date, medium}, at {time, time, short}. Everything is {price, number, USD}.", "fruit": "{num, plural, =0 {no apples} =1 {one apple} other {# apples}}", - "bananas": "{name} ate {num, plural, =0 {no bananas} =1 {a banana} other {# bananas}} {gender, select, male {at his house.} female {at her house.} other {at their house.}}" + "bananas": "{name} ate {num, plural, =0 {no bananas} =1 {a banana} other {# bananas}} {gender, select, male {at his house.} female {at her house.} other {at their house.}}", + "missing_translation": "Hello world!" }, "fr": { "header_1": "Traduction et localisation", @@ -18,5 +19,8 @@ "sale": "La vente commence le {start, date, medium} à {time, time, short}. Tout est à {price, number, USD}.", "fruit": "{num, plural, =0 {pas de pommes} =1 {une pomme} other {# pommes}}", "bananas": "{name} {num, plural, =0 {ne} =1 {} other {}} mange {num, plural, =0 {pas de bananes} =1 {une banane} other {# bananes}} {gender, select, male {chez lui.} female {chez elle.} other {chez eux.}}" + }, + "en_US": { + "missing_translation": "Default fallback language" } } diff --git a/demo/x-fallback.html b/demo/x-fallback.html new file mode 100644 index 0000000..aa17734 --- /dev/null +++ b/demo/x-fallback.html @@ -0,0 +1,71 @@ + + + + + + + + + + + + + + diff --git a/demo/x-translate.html b/demo/x-translate.html index 175477c..914cdb5 100644 --- a/demo/x-translate.html +++ b/demo/x-translate.html @@ -34,6 +34,7 @@

{{localize('header_1')}}

localize('greeting') + localize('missing_translation') localize('intro', 'name', 'Batman') localize('cats', 'numCats', 10000, 'pctBlack', 0.42) + + + + + + + + + From 883c08b78a6d6e7416a5f9298023893ef868aec6 Mon Sep 17 00:00:00 2001 From: Aleksandar Toshovski Date: Tue, 5 Dec 2017 00:22:05 +0100 Subject: [PATCH 2/9] Add fallback language for localization --- app-localize-behavior.html | 24 +++++++++++- demo/index.html | 2 + demo/locales.json | 6 ++- demo/x-fallback.html | 71 ++++++++++++++++++++++++++++++++++ demo/x-translate.html | 1 + test/basic.html | 20 ++++++++++ test/x-translate-fallback.html | 53 +++++++++++++++++++++++++ 7 files changed, 174 insertions(+), 3 deletions(-) create mode 100644 demo/x-fallback.html create mode 100644 test/x-translate-fallback.html diff --git a/app-localize-behavior.html b/app-localize-behavior.html index cf4dd21..180b82e 100644 --- a/app-localize-behavior.html +++ b/app-localize-behavior.html @@ -151,6 +151,21 @@ type: String }, + /** + * Fallback Language if the current language is not yet translated. + * + * For example having only english translation, but trying to open a french localization will return the fallback language + * + * this.resources = { + * 'en': { 'greeting': 'Hello!' } + * } + * + * + */ + fallbackLanguage:{ + type: String + }, + /** * The dictionary of localized messages, for each of the languages that * are going to be used. See http://formatjs.io/guides/message-syntax/ for @@ -195,7 +210,7 @@ */ localize: { type: Function, - computed: '__computeLocalize(language, resources, formats)' + computed: '__computeLocalize(language, fallbackLanguage, resources, formats)' }, /** @@ -245,7 +260,7 @@ /** * Returns a computed `localize` method, based on the current `language`. */ - __computeLocalize: function(language, resources, formats) { + __computeLocalize: function(language, fallbackLanguage, resources, formats) { var proto = this.constructor.prototype; // Check if localCache exist just in case. @@ -262,9 +277,14 @@ if (!key || !resources || !language || !resources[language]) return; + // Cache the key/value pairs for the same language, so that we don't // do extra work if we're just reusing strings across an application. var translatedValue = resources[language][key]; + //use fallback key if provided + if(!translatedValue && fallbackLanguage){ + translatedValue = resources[fallbackLanguage][key]; + } if (!translatedValue) { return this.useKeyIfMissing ? key : ''; diff --git a/demo/index.html b/demo/index.html index 2448d0e..53e0054 100644 --- a/demo/index.html +++ b/demo/index.html @@ -19,6 +19,7 @@ + @@ -30,5 +31,6 @@ + diff --git a/demo/locales.json b/demo/locales.json index f434899..d1e2b98 100644 --- a/demo/locales.json +++ b/demo/locales.json @@ -7,7 +7,8 @@ "cats": "I have {numCats, number} cats. Almost {pctBlack, number, percent} of them are black.", "sale": "Sale begins {start, date, medium}, at {time, time, short}. Everything is {price, number, USD}.", "fruit": "{num, plural, =0 {no apples} =1 {one apple} other {# apples}}", - "bananas": "{name} ate {num, plural, =0 {no bananas} =1 {a banana} other {# bananas}} {gender, select, male {at his house.} female {at her house.} other {at their house.}}" + "bananas": "{name} ate {num, plural, =0 {no bananas} =1 {a banana} other {# bananas}} {gender, select, male {at his house.} female {at her house.} other {at their house.}}", + "missing_translation": "Hello world!" }, "fr": { "header_1": "Traduction et localisation", @@ -18,5 +19,8 @@ "sale": "La vente commence le {start, date, medium} à {time, time, short}. Tout est à {price, number, USD}.", "fruit": "{num, plural, =0 {pas de pommes} =1 {une pomme} other {# pommes}}", "bananas": "{name} {num, plural, =0 {ne} =1 {} other {}} mange {num, plural, =0 {pas de bananes} =1 {une banane} other {# bananes}} {gender, select, male {chez lui.} female {chez elle.} other {chez eux.}}" + }, + "en_US": { + "missing_translation": "Default fallback language" } } diff --git a/demo/x-fallback.html b/demo/x-fallback.html new file mode 100644 index 0000000..aa17734 --- /dev/null +++ b/demo/x-fallback.html @@ -0,0 +1,71 @@ + + + + + + + + + + + + + + diff --git a/demo/x-translate.html b/demo/x-translate.html index 175477c..914cdb5 100644 --- a/demo/x-translate.html +++ b/demo/x-translate.html @@ -34,6 +34,7 @@

{{localize('header_1')}}

localize('greeting') + localize('missing_translation') localize('intro', 'name', 'Batman') localize('cats', 'numCats', 10000, 'pctBlack', 0.42) + + + + + + + + + From fccd944653ef40dffab2f5e6a9b313e63ca4b498 Mon Sep 17 00:00:00 2001 From: Aleksandar Toshovski Date: Tue, 5 Dec 2017 21:56:40 +0100 Subject: [PATCH 3/9] fix polymer 1.x tests --- app-localize-behavior.html | 6 ++++-- test/basic.html | 6 ++++-- test/x-translate-fallback.html | 3 +++ 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/app-localize-behavior.html b/app-localize-behavior.html index 180b82e..8f9460c 100644 --- a/app-localize-behavior.html +++ b/app-localize-behavior.html @@ -210,7 +210,7 @@ */ localize: { type: Function, - computed: '__computeLocalize(language, fallbackLanguage, resources, formats)' + computed: '__computeLocalize(language, resources, formats)' }, /** @@ -260,9 +260,11 @@ /** * Returns a computed `localize` method, based on the current `language`. */ - __computeLocalize: function(language, fallbackLanguage, resources, formats) { + __computeLocalize: function(language, resources, formats) { var proto = this.constructor.prototype; + var fallbackLanguage = this.fallbackLanguage; + // Check if localCache exist just in case. this.__checkLocalizationCache(proto); diff --git a/test/basic.html b/test/basic.html index d2a4392..4bc7825 100644 --- a/test/basic.html +++ b/test/basic.html @@ -93,7 +93,8 @@ elem.constructor.prototype.__localizationCache.messages = {}; } - suite('basic', function() { + + suite('fallback', function(){ test('uses fallback language, if translation is missing', function() { var app = fixture('fallback'); assert.equal(app.language, 'fr'); @@ -101,8 +102,9 @@ assert.equal(app.$.fallback.innerHTML, 'français hello'); }); + }); - + suite('basic', function() { test('can translate a basic string', function() { var app = fixture('basic'); diff --git a/test/x-translate-fallback.html b/test/x-translate-fallback.html index 7638023..1f05abd 100644 --- a/test/x-translate-fallback.html +++ b/test/x-translate-fallback.html @@ -48,6 +48,9 @@ } } } + + + }); From 294d533b8118457089f3d8cf785de9872557aece Mon Sep 17 00:00:00 2001 From: Aleksandar Toshovski Date: Sun, 10 Dec 2017 15:46:58 +0100 Subject: [PATCH 4/9] set formatjs to latest version --- bower.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bower.json b/bower.json index 78c2181..93eb9aa 100644 --- a/bower.json +++ b/bower.json @@ -22,7 +22,7 @@ "ignore": [], "dependencies": { "iron-ajax": "PolymerElements/iron-ajax#1 - 2", - "intl-messageformat": "yahoo/intl-messageformat#^1.0.0", + "intl-messageformat": "yahoo/intl-messageformat#^2.2.0", "polymer": "Polymer/polymer#1.9 - 2" }, "devDependencies": { From 622145230a80f67e9f3a741d36fc7c2fadf6a078 Mon Sep 17 00:00:00 2001 From: Aleksandar Toshovski Date: Sun, 10 Dec 2017 18:19:08 +0100 Subject: [PATCH 5/9] add structured translations --- .gitignore | 2 + .travis.yml | 15 +++----- README.md | 7 +++- app-localize-behavior.html | 23 +++++++++++- demo/locales.json | 10 ++++- demo/x-local-translate.html | 2 +- demo/x-translate.html | 11 ++++++ test/basic.html | 33 ++++++++++++++++- test/x-translate-structured.html | 63 ++++++++++++++++++++++++++++++++ 9 files changed, 148 insertions(+), 18 deletions(-) create mode 100644 test/x-translate-structured.html diff --git a/.gitignore b/.gitignore index b0c174c..4131ed0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ bower_components* bower-1.x.json +build/ +.idea diff --git a/.travis.yml b/.travis.yml index 8dc09e8..2ae27f5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,16 +10,11 @@ addons: packages: - google-chrome-stable before_script: - - npm install -g polymer-cli - - polymer install --variants + - "export DISPLAY=:99.0" + - "sh -e /etc/init.d/xvfb start" + - sleep 3 # give xvfb some time to start script: - xvfb-run polymer test - >- - if [ "${TRAVIS_PULL_REQUEST}" = "false" ]; then polymer test -s 'default'; - fi -env: - global: - - secure: >- - DitCRBZSh4YLwnI3LjD1ukbftNDIx6RrUjEYwHi+RMt+OCAkKiY4N0FI9PujlGEnt7tNdaD0CTzLSu+R2vpNeMaqsrjyFKzLfWH8rFRQ1dHCvaqZ1upV92yadzqLW6dhFMMre1OzhMoU0r/4b7hUV93UYAxUZU70WYzJTSjKvv+1ul97UVLMThmulvMBDrOx8cA06SF3oRfZwdxRj7zc8knUZvRmub41exfVonEVYjXdhOstHGLvXBXxmUBOjaTdNNaIT4KvvwMMmMqyQpnoB1uZimTLAy4y4ogpnf9HttPHTSZSTdWI2avk75nlqQCFFCcBier0NWT3phgkQt0CKrKs62w2RwjAIij2/ZVhsUiGLezIuOilG3k3+UYzXHb86KtLMie573XfAsX6cC/DYozJB7NfPA79MOwa/BuzTlyDJdZfKKm8+6FCmh7xQj0OcXHdqqxzTZu40bXwjz32TLDNnkHBdJNjTQrBm//TTQsxZ8BiXiw2tg4r7vXMIr3NjiRLQSLuAiHHQ2MnryhJuvAeBf1cqgjZXxy4mG6ctdus7Fc8nl9GSkY/5VmT4BMGwxbUcdd5/EDf750jwgE/sXzYABABYXd1cwvYPwT5oPsIMOs3hUC5mls+DlTk6Y57EtezsGuvA+EWeyg0rRzuv/u3rfJtOvYV7I7kA9xe/ZU= - - secure: >- - hSCnfwhOhq23wOyRk3vBz4Gz+KBYRaCsekoW9leSQ5LRt7QYF6gESoaiqQaqQ6aL0FX4tW6DCxGStFhwvOcR8EU9J1dz04Xku21bf3haLEFGTpOyDtQwisxw2TKp6ki95FIaBsx6435UvLXn77M3c4X5YW+1N+GKfZ62A/RqP12u6q8c2Sooe70V5E4J7oQS+CW20CvdcfrlEoHgJ89hF7hyu1A5GEj0Y4TJMsNfgqBC7t3k0YMIZ8yEgXRyzxsamKVFaa69U8435Wokol4zcgyVtirXZyKyqVLGk5j5dcUlzlwx4+dpoETrtTsUcIvLyxQ/SxsqBKr3j4u8CMAmKSRkMrDI/Pf6c4Q4H9N+CVOv+VW/TAqD/CnncI9eGaTJ3Ba2HZUMmHImPgBD1a6DqHvaFLbU5J9MHWvwCRBcOwQTAVqaOppgOvmu/mpwNnq9+NJFrwR6aTDgovYzi4TMA9DdoB7O3L6fvwYjqMYRahfukBPSYX5da4DHiQV0P6wEr/Sdub/ERQSgWqXLE6e/jpl4sXBY2rII4Ao3Tc/Xg9suwHjUKvmi8axOyfm0LjE9fmGj1bkFFPKU1JIAbGOaDInbpE6fMjbgpoOeqe6hxRxdrjDn5+/odQhhhUoGNk3YIKuLSeK82lW/ZMgH0/z/NubIQjchIuKDbLqmyX7ZZ2w= + if [ "${TRAVIS_PULL_REQUEST}" = "false" ]; then polymer test; + fi \ No newline at end of file diff --git a/README.md b/README.md index 83ebbed..9579544 100644 --- a/README.md +++ b/README.md @@ -12,9 +12,14 @@ thing! https://github.com/PolymerLabs/tedium/issues --> -[![Build status](https://travis-ci.org/PolymerElements/app-localize-behavior.svg?branch=master)](https://travis-ci.org/PolymerElements/app-localize-behavior) +[![Build status](https://travis-ci.org/toshovski/app-localize-behavior.svg?branch=master)](https://travis-ci.org/PolymerElements/app-localize-behavior) +This is a fork of the AppLocalizeBehavior. There are some improved features: + + * Support of structured files + * fallback Language + ## Polymer.AppLocalizeBehavior `Polymer.AppLocalizeBehavior` wraps the [format.js](http://formatjs.io/) library to diff --git a/app-localize-behavior.html b/app-localize-behavior.html index 8f9460c..3d478a4 100644 --- a/app-localize-behavior.html +++ b/app-localize-behavior.html @@ -282,10 +282,10 @@ // Cache the key/value pairs for the same language, so that we don't // do extra work if we're just reusing strings across an application. - var translatedValue = resources[language][key]; + var translatedValue = this.__findTranslatedValue(resources,language,key); //use fallback key if provided if(!translatedValue && fallbackLanguage){ - translatedValue = resources[fallbackLanguage][key]; + translatedValue = this.__findTranslatedValue(resources,fallbackLanguage,key) } if (!translatedValue) { @@ -309,6 +309,25 @@ }; }, + __findTranslatedValue: function(resources, language, key){ + var subKey = key.split(":"); + var translatedValue = null; + if (subKey.length > 1) { + // use namesSpace multi level + translatedValue = resources[language][subKey[0]]; + for (var i=1; i
- 🇬🇧 EN + 🇬🇧 ENqw FR 🇫🇷
diff --git a/demo/x-translate.html b/demo/x-translate.html index 914cdb5..8d6eae2 100644 --- a/demo/x-translate.html +++ b/demo/x-translate.html @@ -58,6 +58,17 @@

{{localize('header_2')}}

localize('bananas', 'name', 'Robin', 'gender', 'other', 'num', 4)
+ + +

Structured locales

+
+
+
{{localize('fruits:apple', 'num', 0)}} / {{localize('fruit', 'num', 1)}}/ {{localize('fruit', 'num', 3)}}
+
+
+ localize('fruits:apple', 'num', 0) / localize('fruits:apple', 'num', 1) / localize('fruits:apple', 'num', 3) +
+
+ From d5f5f757a230533d52702fdd09dde764c36d7473 Mon Sep 17 00:00:00 2001 From: Aleksandar Toshovski Date: Sun, 10 Dec 2017 18:23:08 +0100 Subject: [PATCH 6/9] add polymer cli, remove sauce labs test --- .travis.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.travis.yml b/.travis.yml index 2ae27f5..4c79a36 100644 --- a/.travis.yml +++ b/.travis.yml @@ -13,6 +13,8 @@ before_script: - "export DISPLAY=:99.0" - "sh -e /etc/init.d/xvfb start" - sleep 3 # give xvfb some time to start + - npm install -g polymer-cli + - polymer install --variants script: - xvfb-run polymer test - >- From 6e31b24b777d40f970a04542065584c962b8bc04 Mon Sep 17 00:00:00 2001 From: Aleksandar Toshovski Date: Sun, 10 Dec 2017 18:31:52 +0100 Subject: [PATCH 7/9] try out without xvfb-run --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 4c79a36..535dcae 100644 --- a/.travis.yml +++ b/.travis.yml @@ -16,7 +16,7 @@ before_script: - npm install -g polymer-cli - polymer install --variants script: - - xvfb-run polymer test + - polymer test - >- if [ "${TRAVIS_PULL_REQUEST}" = "false" ]; then polymer test; fi \ No newline at end of file From b5270b6f7d612f2cc3934480101df060b89832b1 Mon Sep 17 00:00:00 2001 From: Aleksandar Toshovski Date: Sun, 28 Jan 2018 22:26:07 +0100 Subject: [PATCH 8/9] Update .gitignore remove build/ and .idea from .gitignore --- .gitignore | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 4131ed0..a110873 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,3 @@ bower_components* bower-1.x.json -build/ -.idea + From 8bcc16dcf2366804cda6df73cc7eba65f7c5b808 Mon Sep 17 00:00:00 2001 From: Aleksandar Toshovski Date: Sun, 28 Jan 2018 22:27:15 +0100 Subject: [PATCH 9/9] Update x-local-translate.html remove typo --- demo/x-local-translate.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/demo/x-local-translate.html b/demo/x-local-translate.html index 0cad0c3..5b8fcce 100644 --- a/demo/x-local-translate.html +++ b/demo/x-local-translate.html @@ -18,7 +18,7 @@
- 🇬🇧 ENqw + 🇬🇧 EN FR 🇫🇷