对英文网站,我们常常需要显示一个名词的复数形式。
而Rails就提供了一个称为Inflector的工具来计算该逻辑,并且ActionView有一个wrapper方法来处理常见的复数形式,如:
[code]
There are <%= pluralize @recipes.size, "recipe" %>.
[/code]
当你的网站不是使用English或者有一些比较特殊的复数规则时,我们可以在config/environment.rb里定义这些规则,如:
[code]
Inflector.inflections do |inflect|
inflect.plural /^(ox)$/i, '\1en'
inflect.singular /^(ox)en/i, '\1'
inflect.irregular 'person', 'people'
inflect.uncountable %w( fish sheep )
end
[/code]
而Rails就提供了一个称为Inflector的工具来计算该逻辑,并且ActionView有一个wrapper方法来处理常见的复数形式,如:
[code]
There are <%= pluralize @recipes.size, "recipe" %>.
[/code]
当你的网站不是使用English或者有一些比较特殊的复数规则时,我们可以在config/environment.rb里定义这些规则,如:
[code]
Inflector.inflections do |inflect|
inflect.plural /^(ox)$/i, '\1en'
inflect.singular /^(ox)en/i, '\1'
inflect.irregular 'person', 'people'
inflect.uncountable %w( fish sheep )
end
[/code]