rails i18n accepted_nested_attributes_for how-to translate?

时间:2013-01-05 21:09:20

标签: ruby-on-rails-3 activerecord internationalization nested-attributes rails-i18n

我正在使用Rails 3.2.6,这是一个示例案例:

class Man < ActiveRecord::Base
  has_many :eyes
  accepts_nested_attributes_for :eyes
end
class Eye < ActiveRecord::Base
  belongs_to :man
  validates_inclusion_of :color, in: { %w[brown green blue] }
end

观点(在HAML中):

= form_for @man do |f|
  - if @man.errors.any?
    #error_explanation
      %h2= t 'errors.messages.record_invalid', count: @man.errors.count
      %ul
        - @man.errors.full_messages.each do |msg|
          %li= msg

  = f.fields_for(:eyes) do |b|
    .field
      = b.label :color
      = b.text_field :color

  .actions
    = f.submit :submit

it.yml:

it:
  activerecord:
    attributes:
      customer:
        eyes: Occhi
      customer/eyes:
        color: Colore
  errors:
    models:
      man/eyes:
        attributes:
          color:
            inclusion: non valido

然而,颜色标签未翻译(但是使用'actviterecord.attributes.eye.color'),错误消息中的属性仅为“Occhi”,其余为errors.model.eyes.attributes.color.inclusion而不是errors.models.man/eyes.attributes.color.inclusion {1}}

错误消息是errors.model.man.attributes.eyes.inclusion,但我该如何区分呢?应该是“Occhi Colore non valido”而不是“Occhi non valido”

1 个答案:

答案 0 :(得分:6)

试试这个:

it:
  activerecord:
    attributes:
      # set the name used in nested attribute error messages
      customer/eyes:
        color: Occhi Colore
  errors:
    models:
      # change the error message for eye color not included in the list
      eye:
        attributes:
          color:
            inclusion: non valido
    messages:
      # change the inclusion message globally
      inclusion: non valido
  helpers:
    label:
      # set the label used by form builder for labels
      man[eyes_attributes]:
        color: Occhi Colore