This project is archived and is in readonly mode.

#1545 ✓resolved
Maran Hidskes

Error_messages_for i18n not working as expected

Reported by Maran Hidskes | December 10th, 2008 @ 12:22 PM | in 2.x

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?

Comments and changes to this ticket

  • Yaroslav Markin

    Yaroslav Markin December 25th, 2008 @ 09:18 PM

    • 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

  • Stefan Achatz

    Stefan Achatz February 23rd, 2009 @ 07:44 AM

    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)
    
  • Lukas Rieder

    Lukas Rieder March 23rd, 2009 @ 06:02 PM

    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

  • José Valim

    José Valim August 9th, 2009 @ 06:37 PM

    • State changed from “new” to “resolved”

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

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Attachments

Pages