i18n for select boxes

此生再无相见时 提交于 2019-12-18 23:38:13

问题


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"

回答1:


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 -%>


来源:https://stackoverflow.com/questions/6683366/i18n-for-select-boxes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!