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

Rails I18n翻译缺失错误排查:en.yml与en-US.yml的疑问

Rails I18n翻译缺失问题解答

问题场景

在Rails项目中使用I18n国际化功能时出现翻译缺失错误,相关代码及报错如下:

视图文件(views/ipl_mailer/confirm_doctor_form_submitted_complete.html.slim)

p Dear #{@doctor.display_name},

p #{t('.thank_you')}

本地化文件(locales/ipl/en.yml)

ipl: &ipl
  ipl:
    cases_review_forms:
      form: &form
        would_approve: "Approve(Yes/No)"
      edit:
        <<: *form
  ipl_mailer:
    confirm_doctor_form_submitted_complete:
      thank_you: 'Thank you for reviewing your ClinCheck treatment plans. We have received your response and noted that all plans are approved.'

报错信息

I18n.t("thank_you")
"translation missing: en-US.thank_you"

问题解答

这个错误不完全是因为使用en.yml而非en-US.yml导致,核心问题主要有两点:

  1. 翻译路径不匹配:
    报错显示代码中直接调用了I18n.t("thank_you")(全局路径查找),但你的翻译实际位于en.ipl.ipl_mailer.confirm_doctor_form_submitted_complete.thank_you层级下,两者路径完全不对应。而视图中使用的t('.thank_you')是相对路径,会自动匹配当前模板的命名空间(即ipl_mailer.confirm_doctor_form_submitted_complete),这部分是正确的,报错应该来自其他地方的全局调用。

  2. 区域回退机制:
    Rails I18n默认支持区域回退,当系统使用en-US locale时,如果找不到en-US.yml,会自动回退到en.yml的配置。所以只要你的en.yml配置正确,文件名本身不会直接导致翻译缺失。

修复建议

  • 检查代码中调用I18n.t("thank_you")的地方,改为相对调用t('.thank_you')(如果是在对应邮件模板中),或者使用完整路径t('ipl.ipl_mailer.confirm_doctor_form_submitted_complete.thank_you')。
  • 如果需要专门支持en-US locale,可以创建en-US.yml并复制en.yml的配置,或者在en.yml中添加en-US的节点继承en的配置。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.18 15:55:20