简体   繁体   中英

Localise nested virtual attribute in Rails

How is it possible to localise nested virtual attribute in Rails?

The model:

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

and the view:

= 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

The translation keys (from en.yml ) like activerecord.attributes.user.company_information.name and activerecord.attributes.user.company_information_name aren't picked up.

It seems you are using simple_form gem for generation of forms. Here is what worked for me.

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

The link to simple form's localization chapter may be also useful.

In rails 3 i found one way. In your case translation would be helpers.label.user[company_information].name in en.yml it would look like this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM