插值阵列的I18n [英] Interpolation in I18n array
问题描述
我使用数组在区域文件能够生成各种输出方式的文本块(的ActionMailer模板,虾文件,HAML模板)是特定于语言环境。它完美地工作,但有时我想变量传递到这些调用的I18n。但是,这是行不通的。
I'm using arrays in a locale file to be able to generate blocks of text in various output methods (ActionMailer templates, Prawn documents, HAML templates) that are locale-specific. It works perfectly, but sometimes I want to pass variables into these I18n calls. However, this doesn't work.
说我的语言环境文件看起来像这样:
Say my locale file looks like this:
en:
my_array:
- "Line 1"
- "Line 2"
- "Line 3 has a %{variable}"
- "Line 4"
我想 I18n.t的输出('my_array',:变量=>'变量命名变量')
来如下:
["Line 1", "Line 2", "Line 3 has a variable named variable", "Line 4"]
但是,输出是:
["Line 1", "Line 2", "Line 3 has a %{variable}", "Line 4"]
我会做插值自己检索后阵?还是有更好的方式来做到这一点?
Will I have to do the interpolation myself after retrieving the array? Or is there a better way to do this?
推荐答案
我觉得这样的事情是不是国际化的宝石原生支持,但它使用一些元编程的魔法是可行的。
I think such thing is not supported natively in i18n gem, but it's doable using some meta-programming magic.
在一个初始化(例如,配置/初始化/ i18n.rb
)添加以下
In an initializer (say, config/initializers/i18n.rb
) add the following
I18n.backend.instance_eval do
def interpolate(locale, string, values = {})
if string.is_a?(::Array) && !values.empty?
string.map { |el| super(locale, el, values) }
else
super
end
end
end
这篇关于插值阵列的I18n的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!