diff --git a/addon/utils/t.js b/addon/utils/t.js index ac2904f..ed7066c 100644 --- a/addon/utils/t.js +++ b/addon/utils/t.js @@ -26,12 +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); - Ember.assert('Translation for key "' + path + '" is not a string.', Ember.typeOf(result) === 'string'); - return service.fmt(result, 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 f8717d9..ffac47b 100644 --- a/app/helpers/t.js +++ b/app/helpers/t.js @@ -8,22 +8,42 @@ 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; }); + 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