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
18 changes: 17 additions & 1 deletion addon/utils/t.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ function T(attributes) {
var service = this.container.lookupFactory('service:i18n');
var result;
var locale;
var application = this.container.lookup('application:main');
var htmlLocales = application.htmlLocales ? true : false;

if (!Ember.isArray(values)) {
values = Array.prototype.slice.call(arguments, 1);
Expand All @@ -31,7 +33,21 @@ function T(attributes) {
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 t = readArray(values);
if( htmlLocales === true ) {
t = t.map(function(value) {
return Ember.Handlebars.Utils.escapeExpression(value);
});
}

result = service.fmt(result, readArray(t));

if( htmlLocales !== true ) {
return result;
}
else {
return Ember.String.htmlSafe( result );
}
};
}

Expand Down
24 changes: 24 additions & 0 deletions tests/unit/utils/t-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,3 +235,27 @@ test('can override the format handler', function(assert) {

assert.equal(t('foo'), 'barbiz');
});

test('escapes html as default', function(assert) {
application.defaultLocale = 'en';

assert.equal(typeof t('foo'), 'string');

application.htmlLocales = false;

assert.equal(typeof t('foo'), 'string');
});

test('allow html in locales', function(assert) {
application.defaultLocale = 'en';
application.htmlLocales = true;

assert.equal(typeof t('foo'), 'object');
});

test('escape interpolation values if htmlLocales are enabled', function(assert) {
application.defaultLocale = 'en';
application.htmlLocales = true;

assert.equal(t('name', '<s>foo</s>', '<u>bar</u>').toString(), '&lt;s&gt;foo&lt;/s&gt; &lt;u&gt;bar&lt;/u&gt;');
});