2010-12-05 49 views
5

我试图在I18n & Rails中实现语言环境特定的多元化规则,但我没有运气。下面是我在做什么:定制Rails I18n语言环境多元化帮助

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

正如你所看到的,我试图迫使:一些关键的(它应该返回“少数用户”),只是为了看看这个党pluralizer将工作.. 。但没有骰子:(

这里是我运行环境:

  • 的Rails 2.3.8
  • 国际化0.5.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" 

可能给一个尝试,看看它是否有助于

+0

甜。这工作。多谢,伙计! – dhulihan 2010-12-05 07:52:04

相关问题