Adventures with Ruby

Pluralization Rails 2.2 style

View Comments

Railscast episode 132 talks about using helpers outside views. All in all a good and useful screencast. I only have one comment: In Rails 2.2 internationalized pluralization goes like this:

I18n.t(:people, :count => @people.size)

With these translation-files:

en:
  people:
    one: "one person"
    other: "%{count} people"
nl:
  people:
    one: "een persoon"
    other: "%{count} personen"

Written by Iain Hecker

October 20th, 2008 at 7:50 pm

Posted in iain.nl

  • Peter

    Nice! I don’t understand the syntax however. What does the REG3XP1!> part do?

  • Peter

    Nice! I don’t understand the syntax however. What does the REG3XP1!> part do?

  • http://iain.nl Iain Hecker

    Syntax highlighter is a bit broken… fixed

  • http://iain.nl Iain Hecker

    Syntax highlighter is a bit broken… fixed

  • http://rubyreddo.com/ Dmitry

    How I can use rules for pluralization? For example in Russian language there are three different forms (‘cups’ in English):
    1) count == 1 – ??????
    2) count >= 2 && count = 5 – ??????

  • http://rubyreddo.com/ Dmitry

    How I can use rules for pluralization? For example in Russian language there are three different forms (‘cups’ in English):
    1) count == 1 – ??????
    2) count >= 2 && count = 5 – ??????

  • http://rubyreddo.com/ Dmitry

    Ops, encoding and < problems.

  • http://rubyreddo.com/ Dmitry

    Ops, encoding and < problems.

  • http://iain.nl Iain Hecker

    Yaroslav made a gem for Russion: http://github.com/yaroslav/russian/tree/master

    With that gem, you can add lambdas to date translations.

  • http://iain.nl Iain Hecker

    Yaroslav made a gem for Russion: http://github.com/yaroslav/russian/tree/master

    With that gem, you can add lambdas to date translations.

  • http://rubyreddo.com/ Dmitry

    But what about :count value? Were I can define a rule for a specific language?

  • http://rubyreddo.com/ Dmitry

    But what about :count value? Were I can define a rule for a specific language?

  • Dmitry

    OK, I have found the pluralization rule in the i18n, but it’s not so cool, that it might be:

    Comment:
    # Note that I18n::Backend::Simple only supports an algorithm for English
    # pluralization rules. Other algorithms can be supported by custom backends.

    Why different backends, why not just like it done in the GetText with a rule at the begining of the file? Also we can create a yaml file with the default rule data:
    http://unicode.org/cldr/data/charts/supplemental/language_plural_rules.html

    Why not?

  • Dmitry

    OK, I have found the pluralization rule in the i18n, but it’s not so cool, that it might be:

    Comment:
    # Note that I18n::Backend::Simple only supports an algorithm for English
    # pluralization rules. Other algorithms can be supported by custom backends.

    Why different backends, why not just like it done in the GetText with a rule at the begining of the file? Also we can create a yaml file with the default rule data:
    http://unicode.org/cldr/data/charts/supplemental/language_plural_rules.html

    Why not?

  • http://iain.nl Iain Hecker

    Rails I18n wasn’t meant to be a solution for all languages. It was focused on being flexible and just to make sure that the default Rails behavior could be replicated. If your language has something special (it sucks to live in eastern Europe for that matter) you’d have to roll your own, usually in the form of a new backend. Rails i18n tried to keep it compatible with different pluralization rules by using one and other as keys. Other languages might use ‘few’ and ‘many’ too.

    But there is hard work going on to implement this in the form of a gem. Have a look at link [1] for the work for getting the entire CLDR in a gem and globalize2 [2] is also comming along nicely. These things take time though.

    [1] http://groups.google.com/group/rails-i18n/browse_thread/thread/439f2a42a215e973/4013e7dc5b30aa24?lnk=gst&q=cldr#4013e7dc5b30aa24

    [2] http://github.com/joshmh/globalize2

  • http://iain.nl Iain Hecker

    Rails I18n wasn’t meant to be a solution for all languages. It was focused on being flexible and just to make sure that the default Rails behavior could be replicated. If your language has something special (it sucks to live in eastern Europe for that matter) you’d have to roll your own, usually in the form of a new backend. Rails i18n tried to keep it compatible with different pluralization rules by using one and other as keys. Other languages might use ‘few’ and ‘many’ too.

    But there is hard work going on to implement this in the form of a gem. Have a look at link [1] for the work for getting the entire CLDR in a gem and globalize2 [2] is also comming along nicely. These things take time though.

    [1] http://groups.google.com/group/rails-i18n/browse_thread/thread/439f2a42a215e973/4013e7dc5b30aa24?lnk=gst&q=cldr#4013e7dc5b30aa24

    [2] http://github.com/joshmh/globalize2

blog comments powered by Disqus
Fork me on GitHub