rasmusrn
2009-Oct-19 12:25 UTC
Issue with translating model names with underscore in error_messages_for
Hi everybody, In ActionView::Helpers::ActiveRecordHelper''s error_messages_for method, the translation key used for getting a model''s human name (used in "1 error prohibited this {{model}} from being saved") is resolved like this: object_name = options[:object_name].to_s.gsub(''_'', '' '') object_name = I18n.t(object_name, :default => object_name, :scope => [:activerecord, :models], :count => 1) ... where options[:object_name] is the underscore version of the model''s name. This means that if I write this in my locale file, da:yml: activerecord: models: item_type: ''ting type'' ... it won''t work as expected because the above mentioned code will generate ''item type'' instead of ''item_type'' as the first argument when calling the I18n.t method. ItemType.human_name works as expected though. Instead, if I change my locale file to this: activerecord: models: item type: ''ting type'' ... error_messages_for works as expected, but ItemType.human_name breaks (giving me the default name instead of the translated). Thus I''m forced to define "item type" and "item_type" in my locale file which isn''t very dry. My suggestion for a fix, would be to replace the above mentioned lines with this one line: object_name = I18n.t(object_name, :default => options [:object_name].to_s.gsub(''_'', '' ''), :scope => [:activerecord, :models], :count => 1) This fix should be considered temporarily, as it would be much cooler if the procedure of resolving the translation key was defined only one place (in ActiveRecord::Base#human_name). So, am I overlooking something or is this to be considered a bug? Thank you.
Sven Fuchs
2009-Oct-21 07:07 UTC
Re: Issue with translating model names with underscore in error_messages_for
Hi Rasmus, I agree this is a bug. At a first glimpse your solution looks good. It should always use :item_type instead of :"item type". Could you add a ticket w/ a failing test and the fix and notify me about it? The reason why some of the implementation still is so convoluted is that we needed to maintain backwards compat during 2.3.x Thanks! On Oct 19, 2009, at 2:25 PM, rasmusrn wrote:> > Hi everybody, > > In ActionView::Helpers::ActiveRecordHelper''s error_messages_for > method, the translation key used for getting a model''s human name > (used in "1 error prohibited this {{model}} from being saved") is > resolved like this: > > object_name = options[:object_name].to_s.gsub(''_'', '' '') > object_name = I18n.t(object_name, :default => object_name, :scope => > [:activerecord, :models], :count => 1) > > ... where options[:object_name] is the underscore version of the > model''s name. > > This means that if I write this in my locale file, da:yml: > > activerecord: > models: > item_type: ''ting type'' > > ... it won''t work as expected because the above mentioned code will > generate ''item type'' instead of ''item_type'' as the first argument when > calling the I18n.t method. ItemType.human_name works as expected > though. > > Instead, if I change my locale file to this: > > activerecord: > models: > item type: ''ting type'' > > ... error_messages_for works as expected, but ItemType.human_name > breaks (giving me the default name instead of the translated). > > Thus I''m forced to define "item type" and "item_type" in my locale > file which isn''t very dry. > > My suggestion for a fix, would be to replace the above mentioned lines > with this one line: > > object_name = I18n.t(object_name, :default => options > [:object_name].to_s.gsub(''_'', '' ''), :scope => > [:activerecord, :models], :count => 1) > > This fix should be considered temporarily, as it would be much cooler > if the procedure of resolving the translation key was defined only one > place (in ActiveRecord::Base#human_name). > > So, am I overlooking something or is this to be considered a bug? > > Thank you. > >