在浏览器中的翻译字符串前面显示翻译名称的 Rails 翻译



在我的语言环境中,有以下翻译:

de:
  activerecord:
    errors:
      models:
        user:
          attributes:
            email:
              taken: "Die E-Mail Adresse wird bereits benutzt."

当我在浏览器中打开所需的页面时,错误消息如下所示:

Email Die E-Mail Adresse wird bereits benutzt.

那么有谁知道为什么翻译后的字符串前面还有另一个"电子邮件"?

正确的 yml 结构应该是:

de:
  activerecord:
    models:
      user: Dude
    attributes:
      user:
        email: "mail"
    errors:
      template:
        header:
          one:   "1 error prohibited this %{model} from being saved"
          other: "%{count} errors prohibited this %{model} from being saved"
      body: "There were problems with the following fields:"
  errors:
    format: ! '%{attribute} %{message}'
    messages:
      taken: "Email Die E-Mail Adresse wird bereits benutzt."

请注意,有两个"错误"键,一个在活动记录内部,另一个在外部。将后者用于验证消息。

您可以从 https://github.com/svenfuchs/rails-i18n/blob/master/rails/locale/en.yml 获得更完整的详细翻译文件

您可以在 https://github.com/mcasimir/devise-i18n-views/blob/master/locales/en.yml 找到设计翻译文件

对于我的项目,我通常为每种语言准备了几个翻译文件:

  • rails.en.yml:rails使用的消息(灵感来自svenfuchs文件)
  • devise.en.yml:与身份验证相关的消息(来自设计项目本身)
  • en.yml:我在视图上创建的消息不属于其他 gem(像"simple_form"这样的 gem 通常也有自己的文件)

编辑

从 Rails 国际化指南中,验证消息将按以下顺序搜索:

  • activerecord.errors.models.user.attributes.name.blank
  • activerecord.errors.models.user.blank
  • activerecord.errors.messages.blank
  • 错误.属性.名称.空白
  • 错误.消息.空白

因此,使用您在该问题上发布的内容是正确的:

de:
  activerecord:
    errors:
      models:
        user:
          attributes:
            email:
              taken: "http://guides.rubyonrails.org/i18n.html#translations-for-active-record-models"

最新更新