i18n Pluralization i18n Pluralization ruby-on-rails ruby-on-rails

i18n Pluralization


Try this:

en.yml :

en:  misc:    kids:      zero: no kids      one: 1 kid      other: %{count} kids

In a view:

You have <%= t('misc.kids', :count => 4) %>

Updated answer for languages with multiple pluralization (tested with Rails 3.0.7):

File config/initializers/pluralization.rb:

require "i18n/backend/pluralization" I18n::Backend::Simple.send(:include, I18n::Backend::Pluralization)

File config/locales/plurals.rb:

{:ru =>   { :i18n =>     { :plural =>       { :keys => [:one, :few, :other],        :rule => lambda { |n|           if n == 1            :one          else            if [2, 3, 4].include?(n % 10) &&                ![12, 13, 14].include?(n % 100) &&                ![22, 23, 24].include?(n % 100)              :few             else              :other             end          end        }       }     }   } }#More rules in this file: https://github.com/svenfuchs/i18n/blob/master/test/test_data/locales/plurals.rb#(copy the file into `config/locales`)

File config/locales/en.yml:

en:  kids:    zero: en_zero    one: en_one    other: en_other

File config/locales/ru.yml:

ru:  kids:    zero: ru_zero    one: ru_one    few: ru_few    other: ru_other

Test:

$ rails c>> I18n.translate :kids, :count => 1=> "en_one">> I18n.translate :kids, :count => 3=> "en_other">> I18n.locale = :ru=> :ru>> I18n.translate :kids, :count => 1=> "ru_one">> I18n.translate :kids, :count => 3=> "ru_few"  #works! yay! >> I18n.translate :kids, :count => 5=> "ru_other"  #works! yay! 


I hope Russian-speaking Ruby on Rails programmers could find this. Just want to share my own very precise Russian pluralization formula. It based on Unicode Specs.Here is contents of config/locales/plurals.rb file only, everything else should be done as same as in answer above.

{:ru =>   { :i18n =>     { :plural =>       { :keys => [:zero, :one, :few, :many],        :rule => lambda { |n|           if n == 0            :zero          elsif            ( ( n % 10 ) == 1 ) && ( ( n % 100 != 11 ) )            # 1, 21, 31, 41, 51, 61...            :one          elsif            ( [2, 3, 4].include?(n % 10) \            && ![12, 13, 14].include?(n % 100) )            # 2-4, 22-24, 32-34...            :few          elsif ( (n % 10) == 0 || \            ![5, 6, 7, 8, 9].include?(n % 10) || \            ![11, 12, 13, 14].include?(n % 100) )            # 0, 5-20, 25-30, 35-40...            :many          end        }       }     }   } }

Native speakers may enjoy cases such as 111 and 121.And here the test results:

  • zero: 0 запросов/куриц/яблок
  • one: 1 запрос/курица/яблоко
  • few: 3 запроса/курицы/яблока
  • many: 5 запросов/куриц/яблок
  • one: 101 запрос/курица/яблоко
  • few: 102 запроса/курицы/яблока
  • many: 105 запросов/куриц/яблок
  • many: 111 запросов/куриц/яблок
  • many: 119 запросов/куриц/яблок
  • one: 121 запрос/курица/яблоко
  • few: 122 запроса/курицы/яблока
  • many: 125 запросов/куриц/яблок

Thanks for initial answer!


First, remember that number of plural forms depends on language, for English there are two, for Romanian there are 3 and for Arabic there are 6 !.

If you want to be able to properly use plural forms you have to use gettext.

For Ruby and rails you should check this http://www.yotabanana.com/hiki/ruby-gettext-howto-rails.html