Search code examples
ruby-on-rails-3internationalizationautomationhtml-safe

How to make rails 3 I18n translation automatically safe?


I use rails 3. Is there any easy way to tell I18n to respect 'html safness' of string used in interpolation and make all translated string html safe by default? So if I have this en.yml:

en:
  user_with_name: 'User with name <em>%{name}</em>'

and I use t('user_with_name', :name => @user.name), I get users name html escaped, but <em> and </em> is left as is?


Solution

  • Old question, but if someone wants to achieve this, here's the monkey patch I came up with :

    module ActionView
      module Helpers
        module TranslationHelper
          private
          def html_safe_translation_key?(key)
            true
          end
        end
      end
    end
    

    Put this in an initializers and that's it! Works with Rails 3.2.6. Only marks the text in localization files as safe, not the interpolation parameters.