I'm having trouble using I18n.translate
to translate an array.
我在使用I18n.translate翻译数组时遇到了麻烦。
Specifically, I've got this array,
具体来说,我有这个阵列,
module TaskEnums
OCTAVE_BANDS = [:hz63, :hz125, :hz250, :hz500, :hz1000, :hz2000, :hz4000, :hz8000, :hz16000]
end
and I have the following translation in a YAML file.
我在YAML文件中有以下翻译。
en:
TaskEnums:
OCTAVE_BANDS:
hz63: "63 Hz"
hz125: "125 Hz"
hz250: "250 Hz"
hz500: "500 Hz"
hz1000: "1000 Hz"
hz2000: "2000 Hz"
hz4000: "4000 Hz"
hz8000: "8000 Hz"
hz16000: "16000 Hz"
In my view, I'd like to display a dropdown menu that allows users to select a frequency.
在我看来,我想显示一个允许用户选择频率的下拉菜单。
<%= form_for(@task) do |f| %>
<%= f.select :frequency, TaskEnums::OCTAVE_BANDS %>
<% end %>
I know I can translate individual symbols with t :symbol
, but this approach doesn't seem to work with arrays (e.g. t TaskEnums::OCTAVE_BANDS
doesn't do what I need).
我知道我可以使用t:符号翻译单个符号,但这种方法似乎不适用于数组(例如,t TaskEnums :: OCTAVE_BANDS不能满足我的需要)。
Does anyone know how I can translate the OCTAVE_BANDS
array, so that the translations appear in the dropdown? This seems like it must be a common problem, so I assume (and hope!) that there's a simple solution... can anyone suggest how to get it done?
有谁知道如何翻译OCTAVE_BANDS数组,以便翻译出现在下拉列表中?这似乎是一个常见的问题,所以我假设(并希望!)有一个简单的解决方案......任何人都可以建议如何完成它?
Many thanks,
非常感谢,
D.
D.
2 个解决方案
#1
17
Use scope
option for your I18n.t
call:
使用范围选项为您的I18n.t电话:
t TaskEnums::OCTAVE_BANDS, scope: 'TaskEnums.OCTAVE_BANDS'
# => ["63 Hz", "125 Hz", "250 Hz", "500 Hz", "1000 Hz", "2000 Hz", "4000 Hz", "8000 Hz", "16000 Hz"]
#2
2
jdoe's answer worked for me! I used this on a form input field.
jdoe的回答对我有用!我在表单输入字段上使用了它。
f.input :state, as: :radio, collection: t(Model.states.map(&:name), scope: 'attributes.states')
#1
17
Use scope
option for your I18n.t
call:
使用范围选项为您的I18n.t电话:
t TaskEnums::OCTAVE_BANDS, scope: 'TaskEnums.OCTAVE_BANDS'
# => ["63 Hz", "125 Hz", "250 Hz", "500 Hz", "1000 Hz", "2000 Hz", "4000 Hz", "8000 Hz", "16000 Hz"]
#2
2
jdoe's answer worked for me! I used this on a form input field.
jdoe的回答对我有用!我在表单输入字段上使用了它。
f.input :state, as: :radio, collection: t(Model.states.map(&:name), scope: 'attributes.states')