I want to be able to translate pluralized strings in i18n in rails. A string can be :
You have 2 kids
or
You have 1 kid
I know that I can use pluralize helper method, but I want to embed this in i18n translations so that I don't have to mess up with my views at any point in the future. I read that :count
is somehow used in translations for plural, but I can't find any real resources on how it gets implemented.
Notice that I know that I can pass a variable in a translation string. I also tried something like :
<%= t 'misc.kids', :kids_num => pluralize(1, 'kid') %>
Which works fine, but has a fundamental problem of the same idea. I need to specify the string 'kid'
in the pluralize helper. I don't want to do that because it will lead to view problems in the future. Instead I want to keep everything in the translation and nothing in the view.
How can I do that ?
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.Native speakers may enjoy cases such as
111
and121
. And here the test results:Thanks for initial answer!
Try this:
en.yml
:In a view:
Updated answer for languages with multiple pluralization (tested with Rails 3.0.7):
File
config/initializers/pluralization.rb
:File
config/locales/plurals.rb
:File
config/locales/en.yml
:File
config/locales/ru.yml
:Test:
English
It just works out of the box
en.yml:
Usage (you can skip I18n in a view file, of course):
Russian (and other languages with multiple plural forms)
Install rails-18n gem and add translations to your
.yml
files as in the example:ru.yml:
Usage:
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
About Redmine. If you copy pluralization file rules in config/locales/ as plurals.rb and other not same as locale name (ru.rb, pl.rb .. etc) these not work. You must rename file rules to 'locale'.rb or change method in file /lib/redmine/i18n.rb
and if you have older redmine, add
Rails 3 handles this robustly with CLDR consideration and count interpolation variable. See http://guides.rubyonrails.org/i18n.html#pluralization