Search code examples
ruby-on-railsinternationalizationrails-i18n

i18n for select boxes


I have a model named Role. And i am using the helper below in a form. Is there a way to change the value of name attribute to another language?

<%= f.collection_select :role_id, Role.all, :id, name, {} -%>

locales/de.yml

de:
  role:
   admin: "something"
   editor: "something something"

Solution

  • In the model:

    class Role < ActiveRecord::Base
      def translated_name
        I18n.t(name, :scope => 'role')
      end
    end
    

    In the view:

    <%= f.collection_select :role_id, Role.all, :id, :translated_name -%>