# encoding: utf-8moduleI18nmoduleTestsmoduleInterpolation# If no interpolation parameter is not given, I18n should not alter the string.# This behavior is due to three reasons:## * Checking interpolation keys in all strings hits performance, badly;## * This allows us to retrieve untouched values through I18n. For example# I could have a middleware that returns I18n lookup results in JSON# to be processed through Javascript. Leaving the keys untouched allows# the interpolation to happen at the javascript level;## * Security concerns: if I allow users to translate a web site, they can# insert %{} in messages causing the I18n lookup to fail in every request.#test"interpolation: given no values it does not alter the string"doassert_equal'Hi %{name}!',interpolate(:default=>'Hi %{name}!')endtest"interpolation: given values it interpolates them into the string"doassert_equal'Hi David!',interpolate(:default=>'Hi %{name}!',:name=>'David')endtest"interpolation: given a nil value it still interpolates it into the string"doassert_equal'Hi !',interpolate(:default=>'Hi %{name}!',:name=>nil)endtest"interpolation: given a lambda as a value it calls it if the string contains the key"doassert_equal'Hi David!',interpolate(:default=>'Hi %{name}!',:name=>lambda{|*args|'David'})endtest"interpolation: given a lambda as a value it does not call it if the string does not contain the key"doassert_nothing_raised{interpolate(:default=>'Hi!',:name=>lambda{|*args|raise'fail'})}endtest"interpolation: given values but missing a key it raises I18n::MissingInterpolationArgument"doassert_raise(I18n::MissingInterpolationArgument)dointerpolate(:default=>'%{foo}',:bar=>'bar')endendtest"interpolation: it does not raise I18n::MissingInterpolationArgument for escaped variables"doassert_nothing_raised(I18n::MissingInterpolationArgument)doassert_equal'Barr %{foo}',interpolate(:default=>'%{bar} %%{foo}',:bar=>'Barr')endendtest"interpolation: it does not change the original, stored translation string"doI18n.backend.store_translations(:en,:interpolate=>'Hi %{name}!')assert_equal'Hi David!',interpolate(:interpolate,:name=>'David')assert_equal'Hi Yehuda!',interpolate(:interpolate,:name=>'Yehuda')endtest"interpolation: given the translation is in utf-8 it still works"doassert_equal'Häi David!',interpolate(:default=>'Häi %{name}!',:name=>'David')endtest"interpolation: given the value is in utf-8 it still works"doassert_equal'Hi ゆきひろ!',interpolate(:default=>'Hi %{name}!',:name=>'ゆきひろ')endtest"interpolation: given the translation and the value are in utf-8 it still works"doassert_equal'こんにちは、ゆきひろさん!',interpolate(:default=>'こんにちは、%{name}さん!',:name=>'ゆきひろ')endifObject.const_defined?(:Encoding)test"interpolation: given a euc-jp translation and a utf-8 value it raises Encoding::CompatibilityError"doassert_raise(Encoding::CompatibilityError)dointerpolate(:default=>euc_jp('こんにちは、%{name}さん!'),:name=>'ゆきひろ')endendtest"interpolation: given a utf-8 translation and a euc-jp value it raises Encoding::CompatibilityError"doassert_raise(Encoding::CompatibilityError)dointerpolate(:default=>'こんにちは、%{name}さん!',:name=>euc_jp('ゆきひろ'))endendtest"interpolation: ASCII strings in the backend should be encoded to UTF8 if interpolation options are in UTF8"doI18n.backend.store_translations'en','encoding'=>('%{who} let me go'.force_encoding("ASCII"))result=I18n.t'encoding',:who=>"måmmå miå"assert_equalEncoding::UTF_8,result.encodingendtest"interpolation: UTF8 strings in the backend are still returned as UTF8 with ASCII interpolation"doI18n.backend.store_translations'en','encoding'=>'måmmå miå %{what}'result=I18n.t'encoding',:what=>'let me go'.force_encoding("ASCII")assert_equalEncoding::UTF_8,result.encodingendtest"interpolation: UTF8 strings in the backend are still returned as UTF8 even with numbers interpolation"doI18n.backend.store_translations'en','encoding'=>'%{count} times: måmmå miå'result=I18n.t'encoding',:count=>3assert_equalEncoding::UTF_8,result.encodingendendtest"interpolation: given a translations containing a reserved key it raises I18n::ReservedInterpolationKey"doassert_raise(I18n::ReservedInterpolationKey){interpolate(:default=>'%{default}',:foo=>:bar)}assert_raise(I18n::ReservedInterpolationKey){interpolate(:default=>'%{scope}',:foo=>:bar)}assert_raise(I18n::ReservedInterpolationKey){interpolate(:default=>'%{separator}',:foo=>:bar)}endprotecteddefcapture(stream)beginstream=stream.to_seval"$#{stream} = StringIO.new"yieldresult=eval("$#{stream}").stringensureeval("$#{stream} = #{stream.upcase}")endresultenddefeuc_jp(string)string.encode!(Encoding::EUC_JP)enddefinterpolate(*args)options=args.last.is_a?(Hash)?args.pop:{}key=args.popI18n.backend.translate('en',key,options)endendendend