Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions addon/utils/t.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
};
}

Expand Down
28 changes: 24 additions & 4 deletions app/helpers/t.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
41 changes: 41 additions & 0 deletions tests/unit/utils/t-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});