models name and underscore

173 views
Skip to first unread message

Andrea

unread,
Mar 29, 2009, 10:04:20 AM3/29/09
to rails-i18n
Hello guys, i've noticed a problem with models name translation with
underscore... in my forms in case of errors my model "web_site" dont
get translated to "sito web". If instead i write in the locale file
"web site", without underscore, the model name get translated. The
main problem is with error_form_for.

Dont Work:

activerecord:
models:
web_site: "sito web" <-- error because there is the _underscore

Work:

activerecord:
models:
web site: "sito web" <-- work because web_site model name is
without _underscore

Someone know a workaround? Actually my solution is to write 2 times
the model name translation, one with underscore, and one without for
error_message_for

Redd Vinylene

unread,
Mar 30, 2009, 10:15:53 AM3/30/09
to rails...@googlegroups.com

Yves-Eric

unread,
Mar 31, 2009, 6:32:28 PM3/31/09
to rails-i18n
Hi all,


I just ran into the same problem. For the record, this issue was
already mentioned in Nov 2008, without a solution:
http://groups.google.com/group/rails-i18n/browse_thread/thread/fbc0686719e56576/4672753e697e424c?lnk=gst&q=model+name+underscore#4672753e697e424c

From a quick look at the code (Rails 2.3.2), I think I have found the
cause, looks like a bug with a simple fix.

The "error_messages_for" is in the file:
# File vendor/rails/actionpack/lib/action_view/helpers/
active_record_helper.rb

Here is a short snippet:

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 underscore of the object_name is replaced by a space,
*before* the translation happens. That is why it worked when you used
"web site" as a key, instead of an underscore.

I am pretty sure we can call this a bug. The proper code should be
something like:


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


I'll open a bug report.

Meanwhile, the temporary workaround is: simply define your translation
twice: with the underscore (used human_name and other {{model}}
interpolations), and with spaces instead (used by the above buggy
code):

activerecord:
models:
web_site: "sito web"
web site: "sito web"


HTH.


--
Yves-Eric


On Mar 29, 11:04 pm, Andrea <Andre...@gmail.com> wrote:
> Hello guys, i've noticed a problem with modelsnametranslation withunderscore... in my forms in case of errors mymodel"web_site" dont
> get translated to "sito web". If instead i write in the locale file
> "web site", withoutunderscore, themodelnameget translated. The
> main problem is with error_form_for.
>
> Dont Work:
>
>   activerecord:
>     models:
>       web_site: "sito web" <-- error because there is the _underscore
>
> Work:
>
>   activerecord:
>     models:
>       web site: "sito web" <-- work because web_sitemodelnameis
> without _underscore
>
> Someone know a workaround? Actually my solution is to write 2 times
> themodelnametranslation, one withunderscore, and one without for
> error_message_for

José Valim

unread,
Apr 1, 2009, 5:31:46 AM4/1/09
to rails-i18n
Guys,

Already created a patch:

https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/2390-object-names-with-underscore-do-the-wrong-lookup-in-i18n-on-error_messages_for

+1 would be appreciated to get it applied.

On Apr 1, 12:32 am, Yves-Eric <yemar...@gmail.com> wrote:
> Hi all,
>
> I just ran into the same problem. For the record, this issue was
> already mentioned in Nov 2008, without a solution:http://groups.google.com/group/rails-i18n/browse_thread/thread/fbc068...

Yves-Eric

unread,
Apr 1, 2009, 7:10:57 AM4/1/09
to rails-i18n
Awesome! Thanks for the quick patch. Definitely gets a +1 from me.

FYI, I also modified bug #22 on the I18n tracker
http://i18n.lighthouseapp.com/projects/14948/tickets/22-ar-model-name-with-probel#ticket-22-4
I guess it can be closed now.

Cheers,

--
Yves-Eric

On Apr 1, 6:31 pm, José Valim <jose.va...@gmail.com> wrote:
> Guys,
>
> Already created a patch:
>
> https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/2...

Erik Dahlstrand

unread,
Apr 1, 2009, 11:27:43 AM4/1/09
to rails-i18n
Excellent. Thanks!


On 1 Apr, 13:10, Yves-Eric <yemar...@gmail.com> wrote:
> Awesome! Thanks for the quick patch. Definitely gets a +1 from me.
>
> FYI, I also modified bug #22 on the I18n trackerhttp://i18n.lighthouseapp.com/projects/14948/tickets/22-ar-model-name...

Sven Fuchs

unread,
Apr 8, 2009, 8:00:24 AM4/8/09
to rails...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages