在Rails中本地化嵌套的虚拟属性

如何在Rails中本地化嵌套的虚拟属性

该模型:

class User < ActiveRecord::Base attr_accessor :company_information # This is used in callbacks etc end 

和观点:

 = simple_form_for(resource, :as => resource_name, :url => registration_path(resource_name), html: {class: 'form-horizontal'}) do |f| = devise_error_messages! = f.input :email = f.input :password = f.input :password_confirmation = f.simple_fields_for :company_information do |c| = c.input :name # This is what I want to localise = f.button :submit 

不会选择像activerecord.attributes.user.company_information.nameactiverecord.attributes.user.company_information_name这样的翻译键(来自en.yml )。

看来你正在使用simple_form gem来生成表单。 这对我有用。

 en: simple_form: labels: user: company_information: name: My Name 

简单forms的本地化章节的链接也可能很有用。

在rails 3中,我找到了一种方法。 在你的情况下,翻译将是helpers.label.user[company_information].name ,它看起来像这样

 en: helpers: label: "user[company_information]": name:"Name"