您可以让I18n为您处理多元化,只需使用count参数即可。
您需要像这样设置区域设置文件:
# config / locales / en.yml
en:
online_users:
one: "1 user is online"
other: "%{count} users are online"然后通过将count参数传递给I18n.thelper来使用刚创建的密钥:
I18n.t("online_users", count: 1)
#=> "1 user is online"
I18n.t("online_users", count: 4)
#=> "4 users are online"