From 824487846f32ba9b8ef21930d9728e216df8c52d Mon Sep 17 00:00:00 2001 From: jelhan Date: Sat, 4 Apr 2015 21:07:59 +0200 Subject: [PATCH 1/2] support html tags in translations --- addon/utils/t.js | 11 ++++++++++- tests/unit/utils/t-test.js | 17 +++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/addon/utils/t.js b/addon/utils/t.js index ac2904f..96597b6 100644 --- a/addon/utils/t.js +++ b/addon/utils/t.js @@ -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); @@ -31,7 +33,14 @@ 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)); + result = service.fmt(result, readArray(values)); + + if( htmlLocales !== true ) { + return result; + } + else { + return Ember.String.htmlSafe( result ); + } }; } diff --git a/tests/unit/utils/t-test.js b/tests/unit/utils/t-test.js index 4040d30..00fd92f 100644 --- a/tests/unit/utils/t-test.js +++ b/tests/unit/utils/t-test.js @@ -235,3 +235,20 @@ 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'); +}); From 125c2d3cccc766e86d894a75ad37d35e7c97ee45 Mon Sep 17 00:00:00 2001 From: jelhan Date: Sat, 4 Apr 2015 22:12:45 +0200 Subject: [PATCH 2/2] escape interpolation values if html tags are allowed in translation string --- addon/utils/t.js | 9 ++++++++- tests/unit/utils/t-test.js | 7 +++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/addon/utils/t.js b/addon/utils/t.js index 96597b6..6c4d201 100644 --- a/addon/utils/t.js +++ b/addon/utils/t.js @@ -33,7 +33,14 @@ 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'); - result = 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; diff --git a/tests/unit/utils/t-test.js b/tests/unit/utils/t-test.js index 00fd92f..63bbacc 100644 --- a/tests/unit/utils/t-test.js +++ b/tests/unit/utils/t-test.js @@ -252,3 +252,10 @@ test('allow html in locales', function(assert) { 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', 'foo', 'bar').toString(), '<s>foo</s> <u>bar</u>'); +});