Skip to content

Commit 15985ed

Browse files
committed
i18n.js: apply wrappers prior to interpolation
makes for happier handlebars templates Change-Id: I0f2e9dae3f61700542c253fd6f9e0b4d652397da Reviewed-on: https://gerrit.instructure.com/6079 Tested-by: Hudson <hudson@instructure.com> Reviewed-by: Ryan Shaw <ryan@instructure.com>
1 parent df9fab1 commit 15985ed

4 files changed

Lines changed: 21 additions & 10 deletions

File tree

public/javascripts/i18n.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,15 @@ I18n.prepareOptions = function() {
8787
};
8888

8989
I18n.interpolate = function(message, options) {
90+
var placeholder, value, name, matches, needsEscaping = false;
91+
9092
options = this.prepareOptions(options);
91-
var matches = message.match(this.PLACEHOLDER) || [];
93+
if (options.wrapper) {
94+
needsEscaping = true;
95+
message = this.applyWrappers(message, options.wrapper);
96+
}
9297

93-
var placeholder, value, name, needsEscaping;
98+
matches = message.match(this.PLACEHOLDER) || [];
9499

95100
for (var i = 0; placeholder = matches[i]; i++) {
96101
name = placeholder.replace(this.PLACEHOLDER, "$1");
@@ -118,9 +123,6 @@ I18n.interpolate = function(message, options) {
118123
message = message.replace(regex, value);
119124
}
120125

121-
if (options.wrapper) {
122-
message = this.applyWrappers(needsEscaping ? $.raw(message) : message, options.wrapper)
123-
}
124126
return message;
125127
};
126128

spec/lib/handlebars_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
end
3131

3232
it "should extract wrappers" do
33-
Handlebars.prepare_i18n('{{#t "test"}}<b>{{person}}</b> is <b>so</b> <b><i>cool</i></b>{{/t}}', 'test').
34-
should eql('{{{t "test" "*%{person}* is *so* **cool**" scope="test" w0="<b>$1</b>" w1="<b><i>$1</i></b>"}}}')
33+
Handlebars.prepare_i18n('{{#t "test"}}<b>{{person}}</b> is <b>so</b> <b title="{{definition}}"><i>cool</i></b>{{/t}}', 'test').
34+
should eql('{{{t "test" "*%{person}* is *so* **cool**" scope="test" w0="<b>$1</b>" w1="<b title=\\"%{definition}\\"><i>$1</i></b>"}}}')
3535
end
3636

3737
it "should not allow nested helper calls" do

spec/selenium/handlebars_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818
<li>{{#t "protip" type=../type}}Important {{type}} tip:{{/t}} {{this}}</li>
1919
{{/each}}
2020
</ol>
21-
{{#t "bye"}}welp, see you l8r!{{/t}}
21+
{{#t "bye"}}welp, see you l8r! dont forget 2 <a href="{{url}}">like us</a> on facebook lol{{/t}}
2222
HTML
2323
driver.execute_script Handlebars.compile_template(template, 'test')
2424

25-
result = driver.execute_script("return Template('test', {title: 'greetings', name: 'katie', type: 'yoga', items: ['dont forget to stretch!!!']})")
25+
result = driver.execute_script("return Template('test', {title: 'greetings', name: 'katie', type: 'yoga', items: ['dont forget to stretch!!!'], url: 'http://foo.bar'})")
2626
result.should eql(<<-RESULT)
2727
<h1>greetings</h1>
2828
<p>ohai my name is katie im your <b>yoga</b> instructure!! ;) heres some tips to get you started:</p>
@@ -31,7 +31,7 @@
3131
<li>Important yoga tip: dont forget to stretch!!!</li>
3232
3333
</ol>
34-
welp, see you l8r!
34+
welp, see you l8r! dont forget 2 <a href="http://foo.bar">like us</a> on facebook lol
3535
RESULT
3636
end
3737

spec/selenium/i18n_js_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,15 @@
4646
return I18n.scoped('foo').translate('bar', '*1 + 1* == *2*', {wrapper: '<i>$1</i>'})
4747
JS
4848
end
49+
50+
it "should interpolate placeholders in wrappers" do
51+
# this functionality is primarily useful in handlebars templates where
52+
# wrappers are auto-generated ... in normal js you'd probably just
53+
# manually concatenate it into your wrapper
54+
driver.execute_script(<<-JS).should == 'you need to <a href="http://foo.bar">log in</a>'
55+
return I18n.scoped('foo').translate('bar', 'you need to *log in*', {wrapper: '<a href="%{url}">$1</a>', url: 'http://foo.bar'})
56+
JS
57+
end
4958
end
5059

5160
context "strftime" do

0 commit comments

Comments
 (0)