Ruby on Rails:How to translate options of select tag? -
i created select element in 1 edit user form with:
t.select :identification_type, user.identification_type_hash the hash is
{ :type_1 => 1, :type_2 => 2, ... } i need localize type name. didn't find correct way add translation yml file in locale directory.
i have tryed
en_us: type_1: translationg of type 1 type_2: translationg of type 1 active_record: attributes: identification_type: type_1: translationg of type 1 type_2: translationg of type 1 identification_type: type_1: translationg of type 1 type_2: translationg of type 1 no 1 above works. (while translation of other things still works.) , failed find solution in ror document.
so correct way localize these hash value?
you need translate keys yourself, easy. assume have following locale file:
en_us: identification_type: type_1: translationg of type 1 type_2: translationg of type 1 then select tag can following:
t.select(:identification_type, user.identification_type_hash.map { |k,v| [t(k, scope: :identification_type), v] }) of course looks quite complicated view, move code view helper:
module applicationhelper def user_identification_type_options user.identification_type_hash.map |k,v| [t(k, scope: :identification_type), v] end end end so select tag looks this:
t.select(:identification_type, user_identification_type_options)
Comments
Post a Comment