i18n用于选择框 [英] i18n for select boxes

查看:88
本文介绍了i18n用于选择框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为Role的模型.我正在以表格形式使用下面的帮助程序.有没有办法将name属性的值更改为另一种语言?

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

locales/de.yml

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

推荐答案

在模型中:

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

在视图中:

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

这篇关于i18n用于选择框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆