Ruby on rails 如何使Rails I18n字符串部分安全

Ruby on rails 如何使Rails I18n字符串部分安全,ruby-on-rails,security,rails-i18n,Ruby On Rails,Security,Rails I18n,在RubyonRails框架中使用i18ngem时,如何仅将传递到转换方法中的变量标记为不安全 例如: t( 'safe', default: 'Unsafe <b>%{unsafe_variable}</b> and safe %{safe_variable}', unsafe_variable: "<script>alert('unsafe');</script>", safe_variable: '<strong&

在RubyonRails框架中使用
i18n
gem时,如何仅将传递到转换方法中的变量标记为不安全

例如:

t(
  'safe', 
  default: 'Unsafe <b>%{unsafe_variable}</b> and safe %{safe_variable}', 
  unsafe_variable: "<script>alert('unsafe');</script>", 
  safe_variable: '<strong>safe</strong>'
)
t(
“安全”,
默认值:“不安全%{safe_variable}和安全%{safe_variable}”,
不安全_变量:“警报(‘不安全’;”,
安全变量:'safe'
)
应该回来

Unsafe <b>&lt;script&gt;alert('unsafe');&lt;/script&gt;</b> and safe <strong>safe</strong>
不安全脚本警报(“不安全”)/脚本和安全safe

如果您事先知道哪一个是安全的,哪一个是不安全的,您只需使用
h
帮助器强制转义不安全变量即可

t('safe', default: 'Unsafe <b>%{unsafe_variable}</b> and safe %{safe_variable}', 
  unsafe_variable: h("<script>alert('unsafe');</script>"), 
  safe_variable: '<strong>safe</strong>'
)
t('safe',默认值:'safe%{safe_variable}和safe%{safe_variable}',
不安全_变量:h(“警报(‘不安全’;”),
安全变量:'safe'
)