6 # If no interpolation parameter is not given, I18n should not alter the string.
7 # This behavior is due to three reasons:
9 # * Checking interpolation keys in all strings hits performance, badly;
11 # * This allows us to retrieve untouched values through I18n. For example
12 # I could have a middleware that returns I18n lookup results in JSON
13 # to be processed through Javascript. Leaving the keys untouched allows
14 # the interpolation to happen at the javascript level;
16 # * Security concerns: if I allow users to translate a web site, they can
17 # insert %{} in messages causing the I18n lookup to fail in every request.
19 test "interpolation: given no values it does not alter the string" do
20 assert_equal 'Hi %{name}!', interpolate(:default => 'Hi %{name}!')
23 test "interpolation: given values it interpolates them into the string" do
24 assert_equal 'Hi David!', interpolate(:default => 'Hi %{name}!', :name => 'David')
27 test "interpolation: given a nil value it still interpolates it into the string" do
28 assert_equal 'Hi !', interpolate(:default => 'Hi %{name}!', :name => nil)
31 test "interpolation: given a lambda as a value it calls it if the string contains the key" do
32 assert_equal 'Hi David!', interpolate(:default => 'Hi %{name}!', :name => lambda { |*args| 'David' })
35 test "interpolation: given a lambda as a value it does not call it if the string does not contain the key" do
36 assert_nothing_raised { interpolate(:default => 'Hi!', :name => lambda { |*args| raise 'fail' }) }
39 test "interpolation: given values but missing a key it raises I18n::MissingInterpolationArgument" do
40 assert_raise(I18n::MissingInterpolationArgument) do
41 interpolate(:default => '%{foo}', :bar => 'bar')
45 test "interpolation: it does not raise I18n::MissingInterpolationArgument for escaped variables" do
46 assert_nothing_raised(I18n::MissingInterpolationArgument) do
47 assert_equal 'Barr %{foo}', interpolate(:default => '%{bar} %%{foo}', :bar => 'Barr')
51 test "interpolation: it does not change the original, stored translation string" do
52 I18n.backend.store_translations(:en, :interpolate => 'Hi %{name}!')
53 assert_equal 'Hi David!', interpolate(:interpolate, :name => 'David')
54 assert_equal 'Hi Yehuda!', interpolate(:interpolate, :name => 'Yehuda')
57 test "interpolation: given the translation is in utf-8 it still works" do
58 assert_equal 'Häi David!', interpolate(:default => 'Häi %{name}!', :name => 'David')
61 test "interpolation: given the value is in utf-8 it still works" do
62 assert_equal 'Hi ゆきひろ!', interpolate(:default => 'Hi %{name}!', :name => 'ゆきひろ')
65 test "interpolation: given the translation and the value are in utf-8 it still works" do
66 assert_equal 'こんにちは、ゆきひろさん!', interpolate(:default => 'こんにちは、%{name}さん!', :name => 'ゆきひろ')
69 if Kernel.const_defined?(:Encoding)
70 test "interpolation: given a euc-jp translation and a utf-8 value it raises Encoding::CompatibilityError" do
71 assert_raise(Encoding::CompatibilityError) do
72 interpolate(:default => euc_jp('こんにちは、%{name}さん!'), :name => 'ゆきひろ')
76 test "interpolation: given a utf-8 translation and a euc-jp value it raises Encoding::CompatibilityError" do
77 assert_raise(Encoding::CompatibilityError) do
78 interpolate(:default => 'こんにちは、%{name}さん!', :name => euc_jp('ゆきひろ'))
82 test "interpolation: ASCII strings in the backend should be encoded to UTF8 if interpolation options are in UTF8" do
83 I18n.backend.store_translations 'en', 'encoding' => ('%{who} let me go'.force_encoding("ASCII"))
84 result = I18n.t 'encoding', :who => "måmmå miå"
85 assert_equal Encoding::UTF_8, result.encoding
88 test "interpolation: UTF8 strings in the backend are still returned as UTF8 with ASCII interpolation" do
89 I18n.backend.store_translations 'en', 'encoding' => 'måmmå miå %{what}'
90 result = I18n.t 'encoding', :what => 'let me go'.force_encoding("ASCII")
91 assert_equal Encoding::UTF_8, result.encoding
94 test "interpolation: UTF8 strings in the backend are still returned as UTF8 even with numbers interpolation" do
95 I18n.backend.store_translations 'en', 'encoding' => '%{count} times: måmmå miå'
96 result = I18n.t 'encoding', :count => 3
97 assert_equal Encoding::UTF_8, result.encoding
101 test "interpolation: given a translations containing a reserved key it raises I18n::ReservedInterpolationKey" do
102 assert_raise(I18n::ReservedInterpolationKey) { interpolate(:default => '%{default}', :foo => :bar) }
103 assert_raise(I18n::ReservedInterpolationKey) { interpolate(:default => '%{scope}', :foo => :bar) }
104 assert_raise(I18n::ReservedInterpolationKey) { interpolate(:default => '%{separator}', :foo => :bar) }
112 eval "$#{stream} = StringIO.new"
114 result = eval("$#{stream}").string
116 eval("$#{stream} = #{stream.upcase}")
123 string.encode!(Encoding::EUC_JP)
126 def interpolate(*args)
127 options = args.last.is_a?(Hash) ? args.pop : {}
129 I18n.backend.translate('en', key, options)