Blog Posts

Pluralizing translations in I18N.

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:

<script src="https://gist.github.com/AdrianNowik/bcebd3d4bfd9dfffc5a2b0b4ebfc202a.js"></script>

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:

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.

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

 


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