You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Rails 7应用中pluralize方法无法正确读取国际化复数配置问题

解决Rails 7本地化模型复数不生效的问题

问题原因

你用pluralize(10, Author.model_name.human)时,Author.model_name.human返回的是本地化的单数形式"AutorIn",而pluralize默认会对这个字符串执行英文复数规则(加s),完全没读取你YAML里定义的复数本地化值,所以才输出了"10 Autors"。

两种修复方案

方案1:直接用模型human方法的count参数(推荐)

Model.model_name.human本身支持count参数,会自动根据数量匹配YAML里的单复数本地化:

# 输出 "10 AutorInnen"
"10 #{Author.model_name.human(count: 10)}"

# 数量为1时,输出 "1 AutorIn"
"1 #{Author.model_name.human(count: 1)}"

也可以直接用I18n翻译助手调用:

t("activerecord.models.author", count: 10)

方案2:手动指定复数形式给pluralize

如果一定要用pluralize方法,需要手动传入本地化的复数值:

singular = Author.model_name.human
plural = t("activerecord.models.author.other")
# 输出 "10 AutorInnen"
pluralize(10, singular, plural: plural)

内容的提问来源于stack exchange,提问作者tkhobbes

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 04:53:10