Active Record有默认的英语翻译文件吗?

7

我正在将一个Rails应用程序升级到2.3.2版本,但是我发现无法显示ActiveRecord的默认验证错误信息,因为我没有相应的翻译文件。

以下是报告的错误信息:

translation missing: en-US, activerecord, errors, template, header
translation missing: en-US, activerecord, errors, template, body
Email translation missing: en-US, activerecord, errors, models, user, attributes, email, taken

有人知道在哪里可以找到默认的英文翻译文件,包括验证可能使用的所有字符串吗?

3个回答

15
这是因为我的语言设置是'en-US'而不是'en'。在activerecord/lib/locale下有翻译文件。我把这些翻译复制到了一个新的文件en_US.yml中。
"en-US": 
  activerecord:
    errors: 
        template: 
            body: There were problems with the following fields
            header: 
                one: 1 error prohibited this {{model}} from being saved
                other: "{{count}} errors prohibited this {{model}} from being saved"  
        messages:
            inclusion: "is not included in the list"
            exclusion: "is reserved"
            invalid: "is invalid"
            confirmation: "doesn't match confirmation"
            accepted: "must be accepted"
            empty: "can't be empty"
            blank: "can't be blank"
            too_long: "is too long (maximum is {{count}} characters)"
            too_short: "is too short (minimum is {{count}} characters)"
            wrong_length: "is the wrong length (should be {{count}} characters)"
            taken: "has already been taken"
            not_a_number: "is not a number"
            greater_than: "must be greater than {{count}}"
            greater_than_or_equal_to: "must be greater than or equal to {{count}}"
            equal_to: "must be equal to {{count}}"
            less_than: "must be less than {{count}}"
            less_than_or_equal_to: "must be less than or equal to {{count}}"
            odd: "must be odd"
            even: "must be even"

然后我只需在这些字符串后添加我的自定义字符串。

1
当我将默认语言更改为en-US时,我还需要在此处找到的键:https://github.com/rails/rails/blob/master/actionpack/lib/action_view/locale/en.yml。 - Jared

1
你可以通过告诉 I18n 在找不到 :en_US 翻译时回退到 :en 来避免复制和粘贴标准翻译。下面的示例初始化程序显示了我们如何处理从 'en-GB' 和 'it-IT' 回退到标准的 'en',并为了好玩加入了复数形式。
# config/initializer/i18n.rb 

I18n.backend = I18n::Backend::Chain.new(I18n.backend)
I18n::Backend::Chain.send(:include, I18n::Backend::Fallbacks)
I18n.fallbacks[:'en-GB'] << :en
I18n.fallbacks[:'it-IT'] << :en

require "i18n/backend/pluralization" 
I18n::Backend::Chain.send(:include, I18n::Backend::Pluralization)

0

如果你要在旧格式的哈希中包含这些内容,请使用以下方式;

:activerecord => {
  :errors => {
      :template => {
          :body => 'There were problems with the following fields',
          :header => { 
              :one => '1 error prohibited this {{model}} from being saved',
              :other => "{{count}} errors prohibited this {{model}} from being saved"
          }
      },
      :messages => {
          :inclusion => "is not included in the list",
          :exclusion => "is reserved",
          :invalid => "is invalid",
          :confirmation => "doesn't match confirmation",
          :accepted => "must be accepted",
          :empty => "can't be empty",
          :blank => "can't be blank",
          :too_long => "is too long (maximum is {{count}} characters)",
          :too_short => "is too short (minimum is {{count}} characters)",
          :wrong_length => "is the wrong length (should be {{count}} characters)",
          :taken => "has already been taken",
          :not_a_number => "is not a number",
          :greater_than => "must be greater than {{count}}",
          :greater_than_or_equal_to => "must be greater than or equal to {{count}}",
          :equal_to => "must be equal to {{count}}",
          :less_than => "must be less than {{count}}",
          :less_than_or_equal_to => "must be less than or equal to {{count}}",
          :odd => "must be odd",
          :even => "must be even"
      }
  }

}


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接