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

时间:2015-01-05 10:45:19

标签: ruby-on-rails string internationalization translation

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

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

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

Email Die E-Mail Adresse wird bereits benutzt.

那么有人知道为什么翻译后的字符串前面会有另一个“电子邮件”吗?

1 个答案:

答案 0 :(得分:0)

正确的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."

请注意,有两个“错误”键,一个位于activerecord内,另一个位于外部。使用后面的验证消息。

您可以从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:我在我的视图上创建的消息不属于其他宝石(像“simple_form”这样的宝石通常也有自己的文件)

修改

Rails Internationalization guide开始,将按以下顺序搜索验证消息:

  • activerecord.errors.models.user.attributes.name.blank
  • activerecord.errors.models.user.blank
  • activerecord.errors.messages.blank
  • errors.attributes.name.blank
  • errors.messages.blank

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

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