首页 > 解决方案 > 将自定义参数传递给 devise.en.yml 以在错误消息中使用

问题描述

如果您在尝试登录应用程序时被锁定,我必须使用 Devise 对 Rails 应用程序进行更新,以显示剩余分钟数。

在 devise.en.yml 文件中,我有:

en:
  devise:
    confirmations:
      confirmed: "Your email address has been successfully confirmed."
      send_instructions: "You will receive an email with instructions for how to confirm your email address in a few minutes."
      send_paranoid_instructions: "If your email address exists in our database, you will receive an email with instructions for how to confirm your email address in a few minutes."
    failure:
      already_authenticated: "You are already signed in."
      inactive: "Your account is not activated yet."
      invalid: "Invalid %{authentication_keys} or password."
      locked: "Your account is locked. Please try again in %{number_of_minutes} minutes"
      last_attempt: "You have one more attempt before your account is locked."
      not_found_in_database: "Invalid %{authentication_keys} or password."
      timeout: "Your session expired. Please sign in again to continue."
      unauthenticated: "You need to sign in or sign up before continuing."

我在 locked: 错误消息中有名为“number_of_minutes”的变量,但无论我尝试什么,我都会得到:

missing interpolation argument :number_of_minutes in "Your account is locked. Please try again in %{number_of_minutes} minutes" ({:resource_name=>:user, :authentication_keys=>"E-mail address"} given)

我已经将它添加到视图中,覆盖了 SessionsController,设置了默认语言环境,尝试了一大堆在线推荐,但没有运气。

有任何想法吗?

干杯,阿德里安

标签: ruby-on-railsdeviserails-i18n

解决方案


变量必须作为哈希传递,而不仅仅是一个值。

在您看来,您应该尝试使用以下方式调用翻译:

I18n.t 'devise.failure.locked', number_of_minutes: <value>

替换为您需要的变量


推荐阅读