在Rails中本地化嵌套的虚拟属性 [英] Localise nested virtual attribute in Rails

查看:77
本文介绍了在Rails中本地化嵌套的虚拟属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How is it possible to localise nested virtual attribute in 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).

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

推荐答案

似乎您正在使用simple_form gem来生成表单.这是对我有用的东西.

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.

这篇关于在Rails中本地化嵌套的虚拟属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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