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.

Error_messages_for i18n not working as expected

#1545

I've got some trouble translating the model name in the error string of error_messages_for. I'm not sure if it is a bug or me not understanding the inner workings of rails. The situation is as follows.


nl:
  activerecord:
    models:
      changed_organization: bedrijf
    errors:
      template:
        header:
          one: "1 error on {{model}}"
          other: "{{count}} errors on {{model}}"

Now when I call


error_message_for :changed_organization

I expect to get the following result "1 error on bedrijf" instead I get "1 error on changed organization"

When we look at the code in active_record_helper (lines 196,197)


object_name = options[:object_name].to_s.gsub('_', ' ')
object_name = I18n.t(object_name, :default => object_name, :scope => [:activerecord, :models], :count => 1)

we see that rails first gsubs the underscore to a space and then tries to find a translation for this gsubbed string. I would have expected to first try to find the translation for the non gsubbed string and use the gsubbed string as the default value. YAML files, as far as I know, can't handle keys with spaces so this won't ever work would it? My suggestion would be something like the following


object_name = options[:object_name].to_s.gsub('_', ' ')
object_name = I18n.t(options[:object_name].to_s, :default => object_name, :scope => [:activerecord, :models], :count => 1)

Can somebody help me out here, is it a bug or not?

Reported by Maran Hidskes · December 10th, 2008 @ 12:22 PM

State: resolved
Milestone: 2.x
Assigned to: Pratik Pratik
Importance: none

Activity

  1. Yaroslav Markin
    Yaroslav Markin
    • Assigned user set to Pratik

    Could you please ask in http://groups.google.com/group/r... ?

    Most of us got this stuff successfully working.

    I think we should close for now

    December 25th, 2008 @ 09:18 PM

  2. Stefan Achatz
    Stefan Achatz

    I want to clarify the initial statement: ActiveRecord has two methods to get the model- and attribute-names i18n'ed: :human_name and :human_attribute_name both use klass.name.underscore to get the default. Latter is used by :full_messages, which is used in :error_messages_for.

    So, why should :error_messages_for itself search with "model name" whereas every other code in the app uses "model_name"?

    The locale would have to look like this: en: activerecord:

    models:
      model name: "Thingy" # just to satisfy error_messages_for
      model_name: "Thingy" # used by the rest of the app
    attributes:
      model_name:
        column: "Something"
    
    

    My selfmade solution is similar to above suggestion: overwrite :error_messages_for and change the two lines in question to something like:

    object_name = options[:object_name].to_s
    object_name = I18n.t(object_name, :default => object_name.gsub('_', ' '), :scope => [:activerecord, :models], :count => 1)
    

    February 23rd, 2009 @ 07:44 AM

  3. Lukas Rieder
    Lukas Rieder

    Hy,

    I can confirm this strange behaviour. In my opinion the translation file should be clean and straightforward (no doubles with & without spaces). And I've learned the convention is that keys are underscored_names and not spaced names.

    The attachment contains a diff that fixes the issue. (I've made the same correction like Stefan Achatz did)

    Cheers, Lukas Rieder

    March 23rd, 2009 @ 06:02 PM

  4. José Valim
    José Valim
    • State changed from new to resolved

    This was fixed some time ago (yes!), you can see that this behavior cannot be reproduced on master.

    August 9th, 2009 @ 06:37 PM