When i found in our project some messy translations like 'You assigned: 0 documents to categories' i started searching for some easy and smooth sollution - and i found I18n pluralize feature.

I18n pluralizing translations is individual thing for different languages, because it depend on plural form for given word. In this short post i will focus on two languages: polish and english. In english there are only one singular and one plural form for a given word, but in polish we have more plural forms - let me show you how to easy handle it using I18n .

I will start from english, so we should create translation file (called en.yml). In this file we should set translations like this:

en:
  books:
    zero: 'No books.'
    one: '1 book.'
    other: '%{count} books.'

Pluralization feature uses variable named 'count'. I18n picks translations according to value of it and pluralization rules defined for different languages. Now we just use it e. g. in a controller for notice:

flash[:notice] = I18n.t('books', count: 3) # => '3 books'

Okey, it is time to do same thing in polish which is little more complicated, because polish have different plural form. Lets see how pl.yml file will look like.

pl:
  books:
    zero: 'Zero książek'
    one: 'Jedna książka'
    few: '%{count} książki'
    other: '%{count} książek'

Like we did in english version, we can use it for example in notice:

flash[:notice] = I18n.t('books', count: 1) # => 'Jedna książka'
flash[:notice] = I18n.t('books', count: 2) # => '2 książki'
flash[:notice] = I18n.t('books', count: 5) # => '5 książek'
flash[:notice] = I18n.t('books', count: 22) # => '22 książki'

P.S. If you have troubles with solve some language grammar and imagine what possible form that language have i suggest you to check:

https://github.com/svenfuchs/i18n/blob/master/test/test_data/locales/plurals.rb . Also if for some reason you have to overwrite those rules check solution there:

http://stackoverflow.com/a/6166091