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

时间:2012-02-13 10:10:15

标签: ruby-on-rails ruby localization globalization

如何在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

en.ymlactiverecord.attributes.user.company_information.name等翻译键(来自activerecord.attributes.user.company_information_name)未被提取。

2 个答案:

答案 0 :(得分:1)

您似乎正在使用simple_form gem来生成表单。这对我有用。

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

简单表单localization chapter的链接也可能有用。

答案 1 :(得分:0)

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

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