[英]Translate error messages using rails (I18n) Api
我正在使用Rails国际化api进行ActiveRecord翻译。我在翻译错误消息时遇到问题。 如何在我的项目翻译不同文件中的多个错误消息?我有文件名为application_draft.rb,team.rb,user.rb,todo.rb
我的内部models
folder.I要翻译在他们的错误信息,这是我的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
和todo.rb
错误代码段之一:
def different_projects_required
if project1 && project1 == project2
errors.add(:projects, :multiple_sel_error)
end
end
def validate_number_of_reviewers
errors.add(:user, :too_many_reviewers) if application.todos.count > 3
end
如何翻译这两种方法以避免重复错误?
从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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.