-
-
Notifications
You must be signed in to change notification settings - Fork 411
Pluralizations
(copypasta from Sven’s blog post)
Out-of-the-box pluralization for locales other than :en has been recurring requests for the I18n gem. Even though it was easy to extend the Simple backend to plug in custom pluralization logic and there are working backends doing that (e.g. in Globalize2) there does not seem to be a point in still rejecting a basic feature like this from being included to I18n.
I’ve thus added a Pluralization module that was largely inspired by Yaroslav’s work. It can be included to the Simple backend (or other compatible backend implementations) and will do the following things:
- overwrite the existing pluralize method
- try to find a pluralizer shipped with your translation data and if so use it
- call super otherwise and use the default behaviour
One can ship pluralizers (i.e. lambdas that implement locale specific pluralization algorithms) as part of any Ruby translation file anywhere in the I18n.load_path. The implementation expects to find them with the key :pluralize in a (newly invented) translation metadata namespace :i18n. E.g. you could store an Farsi (Persian) pluralizer like this:
# in locales/fa.rb
{ :fa => { :i18n => { :plural => { :rule => lambda { |n| :other } } } } }
And include the Pluralization module like this:
require "i18n/backend/pluralization"
I18n::Backend::Simple.send(:include, I18n::Backend::Pluralization)
We still have to figure out how to actually ship a bunch of default pluralizers best, but you can find a complete list of CLDR’s language plural rules compiled to Ruby here (part of ours test suite).