Plugins - pluralization for gettext
Add to favoritesPluralizeForGettext
=============
This plugin inspects a single string to get pluralization with substitution. The reason I use a single string is to keep it recognizable for gettext or similar i12n plugins.
Although I created this plugin for gettext, it can be used whenever you like. If you’re not using gettext, _("string").pluralize_for becomes "string".pluralize_for… Simple enough…
Please visit http://infx.nl for more information and updates
Examples
A bit better looking:
<%=_('No posts ~~ Only one post ~~ You have {$N} posts!').pluralize_for(Post.count) %>
Clearer for the translator:
<%=_('~~$N==0: No posts ~~$N==1: Only one post ~~else: You have {$N} posts').pluralize_for(Post.count)%>
Note: The translator can change the logic for this, for languages with weird pluralization rules. ;) Caution: It’s being eval’ed, so always check language files (or ruby code) for any Ruby injections.
Substituting more variables
Why not use the default sprintf-like function from rails?
<%_('You have %d posts, view %s') % [ Post.count, link_to(_('more'), view_more_url)%>
You’d get multiple entries in you .po file, and just ‘more’ can’t be translated differently according to the context.
More advanced, for entering links
<%=_('No posts, create one by clicking {1:here} ~~ Only one post ~~ You have {$N} posts ~~$N>6: You have {$N} posts, view {2:more}').pluralize_for(Post.count) do |i|
i[1] = link_to(i[1], create_post_url) if i[1]
i[2] = link_to(i[2], view_more_url) if i[2]
%>
In the .po file:
#: app/controllers/some_controller.rb:102
msgid "No posts, create one by clicking {1:here} ~~ Only one post ~~ You have {$N} posts ~$N>6~ You have {$N} posts, view {2:more}"
msgstr "Geen artikelen, {1:plaats een artikel} ~~ Slechts een artikel ~~ Je hebt {$N} artikelen ~$N>6~ Je hebt {$N} artikelen ({2:bekijk meer...})"
The numbers in {1:foo} are the keys for the array you’re given. You can adjust it anyway you like.
Simple pluralizations
Perhaps you just want to specify a singular and plural for one word in the entire sentence:
<%=_('You have {$N} {post|posts}').pluralize_for(Post.count)%>
English is simpler than many other languages, so the translator could change it into the more elaborate version:
#: app/controllers/some_controller.rb:102
msgid "You have {$N} {post|posts}"
msgstr "~~$N==0: Helemaal geen artikelen ~~$N==1: U heeft een artikel ~~$N>1: U heeft {$N} artikelen"
===================
Copyright © 2008 Iain Hecker, released under the MIT license
http://code.google.com/p/pluralize-for-gettext/
http://pluralize-for-gettext.googlecode.com/svn/trunk/
Rails' (MIT)
Internationalization
