我正在尝试在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复数器是否会起作用......但是没有骰子:(
这是我正在运行的环境:
任何想法?
答案 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"
可能会试一试,看看是否有帮助