Skip to content

html_safe + interpolate (%) == boom #107

Open
grosser opened this Issue Sep 16, 2011 · 4 comments

3 participants

@grosser
grosser commented Sep 16, 2011
require 'i18n/core_ext/string/interpolate'
require 'active_support/core_ext/string/output_safety'
"%{a}" %{:a => 1} == '1' # fine
"%{a}".html_safe % {:a => 1} == '1' # BOOOM

NoMethodError: undefined method `to_sym' for nil:NilClass
    from /usr/local/rvm/gems/ree-1.8.7-2011.03/gems/i18n-0.6.0/lib/i18n/core_ext/string/interpolate.rb:92:in `%'
    from /usr/local/rvm/gems/ree-1.8.7-2011.03/gems/activesupport-3.1.0/lib/active_support/core_ext/string/output_safety.rb:140:in `gsub'
    from /usr/local/rvm/gems/ree-1.8.7-2011.03/gems/activesupport-3.1.0/lib/active_support/core_ext/string/output_safety.rb:140:in `gsub'
    from /usr/local/rvm/gems/ree-1.8.7-2011.03/gems/i18n-0.6.0/lib/i18n/core_ext/string/interpolate.rb:88:in `%'
    from (irb):4
@egze
egze commented Sep 18, 2011

How dangerous is putting this in the initializers? From the looks of it, it fixes the error.

module ActiveSupport
  class SafeBuffer < String

    def %(*args)
      to_str.% *args
    end
  end
end
@grosser
grosser commented Sep 19, 2011

works for me :D

@grosser
grosser commented Nov 18, 2011

would be nice to have a real solution to this issue...

@brodock
brodock commented Dec 22, 2011

Another temporary workaround would be to place .html_safe after the translate method, like:

 I18n.t('translation.key', :default => 'sometext %{interp}', :interp => '<b>stuff</b>').html_safe
@kurtzhan kurtzhan pushed a commit to kurtzhan/jugglingchina that referenced this issue Mar 28, 2015
@brodock brodock Fixing I18n errors related to html_safe. There is already a bug repor…
…t for this problem here: svenfuchs/i18n#107 but no fix released yet, so going to workaround as it has no side effect.
c896270
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.