Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Rails I18n Locale Pluralization Help

I'm trying to implement locale-specific pluralization rules in I18n & Rails, but I'm having no luck. Here's what I'm doing:

# in config/initializers/locale.rb
I18n::Backend::Simple.send(:include, I18n::Backend::Pluralization)
{
   # Force Use of :few key 
   :ru =>  {:i18n => {:plural => {:rule => lambda { |n| :few}}}}
}

# in config/locales/ru.yml
ru:
  user: 
    one: One User 
    few: Few Users
    many: Many Users
    other: Other Users

# Testing
script/console
>> I18n.locale = :ru ; I18n.t("user", :count => 20)
=> "Other Users"

As you can see, i'm trying to force the :few key(it should return "Few Users"), just to see if this dang pluralizer will work...but no dice :(

Here's the environment I'm running:

  • Rails 2.3.8
  • i18n 0.5.0 gem

Any Ideas?

like image 245
dhulihan Avatar asked Dec 05 '10 02:12

dhulihan


1 Answers

Tried replicating your problem, and had the same issue. Moved the pluralization rule into the locale file, and worked fine.

Switched the locale file over to the Ruby-style, as the regular YAML didn't seem to like my lambda for some reason.

# config/initializers/locale.rb
I18n::Backend::Simple.send(:include, I18n::Backend::Pluralization)

# config/locales/ru.rb
{
  :ru => {
    :user => {
      :one   => "One User",
      :few   => "Few Users",
      :many  => "Many Users",
      :other => "Other Users"
    },
    :i18n => {
      :plural => {
        :rule => lambda { |n| :few }
      }
    }
  }
}

# Testing
$ script/console 
  Loading development environment (Rails 2.3.8)
  >> I18n.locale = :ru; I18n.t("user", :count => 20) #=> "Few Users"

might give that a try and see if it helps

like image 95
PerfectlyNormal Avatar answered Nov 17 '22 12:11

PerfectlyNormal