Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"
like image 461
rookieruby Avatar asked Jul 13 '11 17:07

rookieruby


1 Answers

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 -%>
like image 173
jimworm Avatar answered Sep 17 '22 13:09

jimworm