使用rails(I18n)Api翻译错误消息

时间:2017-12-29 14:53:35

标签: ruby-on-rails ruby ruby-on-rails-3 activerecord rails-i18n

我正在使用rails internationalization api进行activerecord翻译。我在翻译错误消息时遇到问题。 如何翻译我项目中不同文件的多条错误消息?我的application_draft.rb,team.rb,user.rb,todo.rb文件夹中有models文件。我想在其中翻译错误消息,这里是{{1文件:

en.yml

我已实现此代码并抛出错误(意味着它无效)。这是errors: models: -team: -application_draft: -conference: -todo: -user: attributes: roles: too_many_reviewers: too many reviewers multiple_sel_error: must not be selected twice must_accepted: must have been accepted one_app_allowed: Only one application may be lodged confirmed_email_address: Please make sure every student confirmed the email address. wrong_date_sel: must be a later date than start date no_more_than_two: "there cannot be more than 2 students on a team." cannot_changed: can't be changed application_draft.rb错误代码段中的一个:

application.rb中:

todo.rb

todo.rb

def different_projects_required
  if project1 && project1 == project2
    errors.add(:projects, :multiple_sel_error)
  end
end

如何翻译这些以避免重复错误?

1 个答案:

答案 0 :(得分:0)

从activerecord继承它:

en:
  activerecord:
    errors:
      models:
       -team:
       -application_draft:
       -conference:
       -todo:
       -user:
          attributes:
            roles:
              too_many_reviewers: too many reviewers
              multiple_sel_error: must not be selected twice
              must_accepted: must have been accepted
              one_app_allowed: Only one application may be lodged
              confirmed_email_address: Please make sure every student confirmed the email address.
              wrong_date_sel: must be a later date than start date
              no_more_than_two: "there cannot be more than 2 students on a team."
              cannot_changed: can't be changed

如果它不起作用。我有另一种方式。尝试在方法本身中输入翻译,如下所示

    application.rb:

    def different_projects_required
      if project1 && project1 == project2
        errors.add(:projects, I18n.t('activerecord.errors.models.user.attributes.roles.multiple_sel_error'))
      end
    end

    todo.rb

    def validate_number_of_reviewers
      errors.add(:user, I18n.t('activerecord.errors.models.todo.attributes.roles.too_many_reviewers')) if application.todos.count > 3
    end