Override default value for model name in rails3

my locale is: de and I like to do this:

Sheet.model_name.human.pluralize # => Belegs 

to add me with the back "e" instead of "s"

 Sheet.model_name.human.pluralize # => Belege 

only for the sheet class. Can I add it in any way to my / locales / models / de.yml configuration?

+26
ruby-on-rails localization pluralize
May 30 '11 at 16:56
source share
3 answers

First of all, you need to stop using .pluralize . It uses Inflector (which is mainly used for internal Rails elements, for example, fortune telling table names for model sheets →).

 Sheet.model_name.human # => "Beleg" "Beleg".pluralize # => "Belegs" 

What you need to do is use the parameter :count .

 Sheet.model_name.human(:count => 2) # => "Belege" 

This requires you to modify your de.yml as such:

 de: ... activerecord: ... models: sheet: one: Beleg other: Belege 
+52
May 30 '11 at 18:40
source share

You can override pluralization as follows:

In config/initializers/inflections.rb

do:

 ActiveSupport::Inflector.inflections do |inflect| inflect.irregular 'Beleg', 'Belege' end 
+11
May 30 '11 at 18:31
source share

If you don't like the explicit number of counters (e.g. 2), you can use :many , for example.

 Sheet.model_name.human(count => :many) 

or without a hash rocket (for Ruby> = 1.9):

 Sheet.model_name.human(count: :many) 
+1
Mar 27 '15 at 11:17
source share



All Articles