Custom Rails I18n.

I am trying to implement pluralization language rules in I18n and Rails, but I'm out of luck. That'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: a few keys (it should return “Few users”), just to see if this dang pluralizer will work ... but not a cube :(

Here is the environment in which I run:

  • Rails 2.3.8
  • i18n 0.5.0 gem

Any ideas?

+5
source share
1 answer

Tried to replicate your problem and had the same problem. Moved the pluralization rule to the locale file and worked fine.

I switched the locale file to the Ruby style, because regular YAML for some reason did not like my lambda.

# 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"

,

+5

All Articles