#22 ✓resolved

Incorrect underscore removal before localization in error_messages_for

Reported by iwakura-taro | January 20th, 2009 @ 05:42 PM

There are model, for example, FamilyRole. For tranlating it name and attributes using various conventions

activerecord: models:

family role: familia     #here


family_role:              #and here
  name: Namae

Seems, this is not right.

Comments and changes to this ticket

  • Sven Fuchs

    Sven Fuchs January 22nd, 2009 @ 12:00 AM

    iwakura-taro, I'm not sure I get the question or problem :)

    Could you please try to rephrase it?


  • iwakura-taro

    iwakura-taro January 22nd, 2009 @ 10:07 AM

    We have model, which name has more than one world. For example FamilyRole. In order to translate application to some language, we make YAML file config/locales/hz.yaml. In that YAML file we must have references to our model, when we describe model itself, we must use its name in lovercase without underscore (in our sample, family role), but when we describe its model attributes, we using other convention in naming some model, in lovercase, without space, with space (in our sample, family_role). Seems, it is not good to have various conventions here.

  • Yves-Eric Martin

    Yves-Eric Martin April 1st, 2009 @ 12:53 AM

    Posting under this ticket as I think the problem I am going to suggest fixing is the one iwakura-taro was referring to.

    For reference, the problem has been already mentioned twice on the mailing list: here and here.

    The problem is in (looking at Rails 2.3.2): vendor/rails/actionpack/lib/action_view/helpers/active_record_helper.rb

    Snippet from that file, in the error_messages_for method:

    192:           I18n.with_options :locale => options[:locale], :scope => [:activerecord, :errors, :template] do |locale|
    193:             header_message = if options.include?(:header_message)
    194:               options[:header_message]
    195:             else
    196:               object_name = options[:object_name].to_s.gsub('_', ' ')
    197:               object_name = I18n.t(object_name, :default => object_name, :scope => [:activerecord, :models], :count => 1)
    198:               locale.t :header, :count => count, :model => object_name

    On line 196, the underscores are replaced by spaces in the object_name. The result is then used as the key for our localization call. That is why people report having to use "object name" (with spaces) instead of the expected "object_name" (with underscores) in their translation files.

    It's 8AM and I just pulled and allnighter, so forgive me for now writing and testing a proper patch, but I'll wager that a fix would look something like:

    196:               object_name = options[:object_name].to_s
    197:               object_name = I18n.t(object_name, :default => object_name.gsub('_', ' '), :scope => [:activerecord, :models], :count => 1)
  • Yves-Eric Martin

    Yves-Eric Martin April 1st, 2009 @ 12:55 AM

    • Title changed from “AR model name with probel” to “Incorrect underscore removal before localization in error_messages_for”
  • Yves-Eric Martin

    Yves-Eric Martin April 1st, 2009 @ 12:20 PM

    Jose Valim has submitted a patch on the Rails tracker issue #2390.

    I believe this issue does indeed belongs to the Rails tracker rather than here, as it is a problem with Rails not using properly the I18n framework, and not an issue with the I18n framework itself.

    So, this ticket can probably be closed.

  • Sven Fuchs

    Sven Fuchs April 8th, 2009 @ 01:59 PM

    • State changed from “new” to “resolved”

    Closing this as it seems resolved with #2390 as of http://github.com/rails/rails/co...

    Please reopen if I'm getting things wrong.

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

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

Repository for collecting Locale data for Ruby on Rails I18n as well as other interesting, Rails related I18n stuff