Tim*_*van 8 ruby-on-rails pluralize
我有一个案例,我需要使用复数来正确拼写一些东西.但是,我需要像这样呈现html:
<span>1</span> thing
Run Code Online (Sandbox Code Playgroud)
要么,
<span>3</span> things
Run Code Online (Sandbox Code Playgroud)
我可以写一个帮助方法,但我只是确保盒子里没有东西可以做到这一点.
这使用Rails类TextHelper,如果需要,它使用Inflector进行复数化.
def pluralize_with_html(count, word)
"<span>#{count}</span> #{TextHelper.pluralize(count, word)}"
end
Run Code Online (Sandbox Code Playgroud)
在此期间,我创建了这个辅助方法,因为它看起来没有我要找的东西:
def pluralize_word(count, singular, plural = nil)
((count == 1 || count == '1') ? singular : (plural || singular.pluralize))
end
Run Code Online (Sandbox Code Playgroud)
它本质上与pluralize方法相同,只是它删除了前面的数字。这允许我这样做(haml):
%span.label= things.size.to_s
%description= pluralize_word(things.size, 'thing')
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1317 次 |
| 最近记录: |