Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

autosave error messages cannot be translated (i18n)

#4273

This seems to be similar to #3147 ActiveRecord::Error#full_message broken when used with I18n and autosave asso....

ActiveRecord::Schema.define do
  create_table(:owners) { |o| o.string :username }
  create_table(:items)  { |i| i.string :property }
end

class Owner < ActiveRecord::Base
  has_one :item, :autosave => true
  validates_presence_of :username
end

class Item < ActiveRecord::Base
  validates_presence_of :property
end

I18n.backend.store_translations :en, :"activerecord.attributes.item.property" => "Testing1"

owner = Owner.new
owner.build_item
owner.valid?
owner.errors.full_messages

item = Item.create
item.errors.full_messages

This code above will result in
["Item property can't be blank", "Username can't be blank"] and
["Testing1 can't be blank"]

The first output is not as expected. It should be like ["Testing1 can't be blank", "Username can't be blank"].

With the following patch, it works as expected.

Maybe there is a better fix, but this works for my project.

Reported by Raffael Schmid · March 26th, 2010 @ 12:05 PM

State: invalid
Milestone: none
Assigned to: nobody
Importance: Low

Activity

  1. Raffael Schmid
    Raffael Schmid
    • Title changed from nested attributes error messages cannot be looked up to autosave error messages cannot be translated (i18n)

    Sorry, patch works for my test in the description but not in my rails-project.

    Following patch works for both.

    March 26th, 2010 @ 12:27 PM

  2. José Valim
    José Valim
    • State changed from new to invalid

    You need to have the translation under "activerecord.attributes.item.property" and "activerecord.attributes.owner.item.property". You can use a symbol to symlink translations (check I18n documentation for more information). Notice that we cannot rely on constantize, since it would fail in some cases like:

    has_many :super_projects, :class_name => "Project"
    

    March 26th, 2010 @ 11:01 PM