试试这个:
en.yml
:
en:
misc:
kids:
zero: no kids
one: 1 kid
other: %{count} kids
在视图中:
You have <%= t('misc.kids', :count => 4) %>
更新了具有多种复数语言的答案(已通过Rails 3.0.7测试):
档案 config/initializers/pluralization.rb
:
require "i18n/backend/pluralization"
I18n::Backend::Simple.send(:include, I18n::Backend::Pluralization)
档案 config/locales/plurals.rb
:
{:ru =>
{ :i18n =>
{ :plural =>
{ :keys => [:one, :few, :other],
:rule => lambda { |n|
if n == 1
:one
else
if [2, 3, 4].include?(n % 10) &&
![12, 13, 14].include?(n % 100) &&
![22, 23, 24].include?(n % 100)
:few
else
:other
end
end
}
}
}
}
}
档案 config/locales/en.yml
:
en:
kids:
zero: en_zero
one: en_one
other: en_other
档案 config/locales/ru.yml
:
ru:
kids:
zero: ru_zero
one: ru_one
few: ru_few
other: ru_other
测试:
$ rails c
>> I18n.translate :kids, :count => 1
=> "en_one"
>> I18n.translate :kids, :count => 3
=> "en_other"
>> I18n.locale = :ru
=> :ru
>> I18n.translate :kids, :count => 1
=> "ru_one"
>> I18n.translate :kids, :count => 3
=> "ru_few"
>> I18n.translate :kids, :count => 5
=> "ru_other"
"#{....}"
上面的代码中不需要“插值器”和引号。