自定义Rails I18n Locale Pluralization帮助

时间:2010-12-05 02:09:59

标签: ruby-on-rails ruby internationalization

我正在尝试在I18n&amp ;;中实现特定于语言环境的复数规则。 Rails,但我没有运气。这就是我正在做的事情:

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

正如你所看到的,我正试图强迫:几个键(它应该返回“少数用户”),只是为了看看这个dang复数器是否会起作用......但是没有骰子:(

这是我正在运行的环境:

  • Rails 2.3.8
  • i18n 0.5.0 gem

任何想法?

1 个答案:

答案 0 :(得分:5)

尝试复制你的问题,并遇到同样的问题。将复数规则移动到语言环境文件中,并且工作正常。

将语言环境文件切换为Ruby风格,因为常规的YAML似乎不喜欢我的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"

可能会试一试,看看是否有帮助