对英文网站,我们常常需要显示一个名词的复数形式。
而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]
本文介绍了Rails中Inflector工具的使用方法,特别是如何处理名词的复数形式。针对非英语环境或特殊情况,提供了自定义复数规则的方法。
7199

被折叠的 条评论
为什么被折叠?



