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

发布于 2025-01-05 04:03:40 字数 778 浏览 1 评论 0原文

如何在 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.yml),例如 activerecord.attributes.user.company_information.nameactiverecord.attributes.user。未选取company_information_name

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

顾铮苏瑾 2025-01-12 04:03:40

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

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

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

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.

昔日梦未散 2025-01-12 04:03:40

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

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

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"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文