From c1f88ed018c3329c59a2a991af87f0a69674e9cc Mon Sep 17 00:00:00 2001 From: Lukas Kohler Date: Wed, 1 Apr 2015 22:54:32 +0200 Subject: [PATCH 1/3] now getLocalizedPath on the i18n service can return a promise. --- addon/utils/t.js | 6 ++++-- app/helpers/t.js | 7 ++++++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/addon/utils/t.js b/addon/utils/t.js index ac2904f..fbd27af 100644 --- a/addon/utils/t.js +++ b/addon/utils/t.js @@ -29,9 +29,11 @@ function T(attributes) { result = service.applyPluralizationRules(result, locale, path, this.container, values, this); Ember.assert('Missing translation for key "' + path + '".', result); - Ember.assert('Translation for key "' + path + '" is not a string.', Ember.typeOf(result) === 'string'); - return service.fmt(result, readArray(values)); + return Ember.RSVP.resolve(result).then(function(val) { + Ember.assert('Translation for key "' + path + '" is not a string.', Ember.typeOf(val) === 'string'); + return service.fmt(val, readArray(values)); + }) }; } diff --git a/app/helpers/t.js b/app/helpers/t.js index f8717d9..d7a2168 100644 --- a/app/helpers/t.js +++ b/app/helpers/t.js @@ -8,8 +8,13 @@ export default function tHelper(params, hash, options, env) { var t = container.lookup('utils:t'); var application = container.lookup('application:main'); + var cache = ""; var stream = new Stream(function() { - return t(path, params); + return cache; + }); + t(path, params).then(function(val) { + cache = val; + stream.notify(); }); // bind any arguments that are Streams From 25486094b3e5afb65321c533b8db2e15966319af Mon Sep 17 00:00:00 2001 From: Lukas Kohler Date: Wed, 1 Apr 2015 23:03:19 +0200 Subject: [PATCH 2/3] fix missing semicolon --- addon/utils/t.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/addon/utils/t.js b/addon/utils/t.js index fbd27af..567319e 100644 --- a/addon/utils/t.js +++ b/addon/utils/t.js @@ -33,7 +33,7 @@ function T(attributes) { return Ember.RSVP.resolve(result).then(function(val) { Ember.assert('Translation for key "' + path + '" is not a string.', Ember.typeOf(val) === 'string'); return service.fmt(val, readArray(values)); - }) + }); }; } From 28d3ab3c2db9626b8a75a20df7885f2920418cc0 Mon Sep 17 00:00:00 2001 From: Lukas Kohler Date: Thu, 2 Apr 2015 05:28:17 +0200 Subject: [PATCH 3/3] added compatibility to non-promise behavious, and tests for promise-behaviour --- addon/utils/t.js | 17 +++++++++++----- app/helpers/t.js | 29 ++++++++++++++++++++------- tests/unit/utils/t-test.js | 41 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+), 12 deletions(-) diff --git a/addon/utils/t.js b/addon/utils/t.js index 567319e..ed7066c 100644 --- a/addon/utils/t.js +++ b/addon/utils/t.js @@ -26,14 +26,21 @@ function T(attributes) { locale = service.resolveLocale(this.container, this); result = service.getLocalizedPath(locale, path, this.container, this); - result = service.applyPluralizationRules(result, locale, path, this.container, values, this); Ember.assert('Missing translation for key "' + path + '".', result); - return Ember.RSVP.resolve(result).then(function(val) { - Ember.assert('Translation for key "' + path + '" is not a string.', Ember.typeOf(val) === 'string'); - return service.fmt(val, readArray(values)); - }); + var self = this; + if(Ember.typeOf(result) === 'object' && Ember.typeOf(result.then) === 'function') { + return result.then(function(val) { + val = service.applyPluralizationRules(val, locale, path, self.container, values, self); + Ember.assert('Translation for key "' + path + '" is not a string.', Ember.typeOf(val) === 'string'); + return service.fmt(val, readArray(values)); + }); + } else { + result = service.applyPluralizationRules(result, locale, path, self.container, values, self); + Ember.assert('Translation for key "' + path + '" is not a string.', Ember.typeOf(result) === 'string'); + return service.fmt(result, readArray(values)); + } }; } diff --git a/app/helpers/t.js b/app/helpers/t.js index d7a2168..ffac47b 100644 --- a/app/helpers/t.js +++ b/app/helpers/t.js @@ -12,23 +12,38 @@ export default function tHelper(params, hash, options, env) { var stream = new Stream(function() { return cache; }); - t(path, params).then(function(val) { - cache = val; - stream.notify(); - }); + + var update = function() { + var tRes = t(path, params); + if(typeof(tRes) === 'string') { + cache = tRes; + stream.notify(); + } else if(typeof(tRes) === 'undefined') { + cache = ''; + stream.notify(); + } else if(typeof(tRes) === 'object' && typeof(tRes.then) === 'function') { + tRes.then(function(val) { + cache = val; + stream.notify(); + }); + } else { + throw 'unexpected type returned from t util'; + } + }; + update(); // bind any arguments that are Streams for (var i = 0, l = params.length; i < l; i++) { var param = params[i]; if(param && param.isStream){ - param.subscribe(stream.notify, stream); + param.subscribe(update, this); }; } - application.localeStream.subscribe(stream.notify, stream); + application.localeStream.subscribe(update, this); if (path.isStream) { - path.subscribe(stream.notify, stream); + path.subscribe(update, this); } return stream; diff --git a/tests/unit/utils/t-test.js b/tests/unit/utils/t-test.js index 4040d30..107faf1 100644 --- a/tests/unit/utils/t-test.js +++ b/tests/unit/utils/t-test.js @@ -235,3 +235,44 @@ test('can override the format handler', function(assert) { assert.equal(t('foo'), 'barbiz'); }); + + +test('locale lookup handler returns promise', function(assert) { + define('dummy/services/i18n', ['ember-cli-i18n/services/i18n'], function(s) { + s['default'].getLocalizedPath = function(locale, path) { + return new Ember.RSVP.Promise(function(resolve) { + resolve("foo-bar"); + }); + }; + return s['default']; + }); + + application.defaultLocale = 'en'; + + t('foo').then(function(v) { + assert.equal(v, 'foo-bar'); + }); +}); + +test('promise pluralization', function(assert) { + define('dummy/services/i18n', ['ember-cli-i18n/services/i18n'], function(s) { + s['default'].getLocalizedPath = function(locale, path) { + return new Ember.RSVP.Promise(function(resolve) { + resolve({ + one: '%@ friend', + other: '%@ friends' + }); + }); + }; + return s['default']; + }); + + application.defaultLocale = 'en'; + + t('friend', 1).then(function(v) { + assert.equal(v, '1 friend'); + }); + t('friend', 5).then(function(v) { + assert.equal(v, '5 friends'); + }); +}); \ No newline at end of file