I'm having trouble using I18n.translate
to translate an array.
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.
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).
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?
Many thanks,
D.
Use scope
option for your I18n.t
call:
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"]
jdoe's answer worked for me! I used this on a form input field.
f.input :state, as: :radio, collection: t(Model.states.map(&:name), scope: 'attributes.states')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With