【发布时间】:2018-06-10 00:00:22
【问题描述】:
我正在使用 rails 国际化 api 进行 activerecord 翻译。我在翻译错误消息时遇到了问题。
如何翻译来自我项目中不同文件的多个错误消息?我的models 文件夹中有名为application_draft.rb,team.rb,user.rb,todo.rb 的文件。我想翻译其中的错误消息,这是我的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 错误代码sn-ps 之一:
application.rb:
def different_projects_required
if project1 && project1 == project2
errors.add(:projects, :multiple_sel_error)
end
end
todo.rb
def validate_number_of_reviewers
errors.add(:user, :too_many_reviewers) if application.todos.count > 3
end
如何翻译这两者避免重复错误?
【问题讨论】:
-
你能分享确切的错误信息吗?另外,
todo.rb文件 sn-p 似乎在底部有额外的end但我想你只是复制错了 -
@mbajur ,是的,todo.rb 中有额外的“结束”,对此感到抱歉。我收到重复错误。我的问题是如何翻译 applcation_draft.rb 和 todo.rb 中的错误日志没有任何重复错误?我使用了上面提到的 en.yml 代码但抛出了这个错误:
Failure/Error: expect(todo.errors.messages).to eq({ user: ['too many reviewers'] }) expected: {:user=>["too many reviewers"]} got: {:user=>["translation missing: en.activerecord.errors.models.todo.attributes.user.too_many_reviewers"]} -
@mbajur 如何同时翻译多个模型中的消息?
标签: ruby-on-rails ruby ruby-on-rails-3 activerecord rails-i18n