【问题标题】:Pass in Custom Parameter to devise.en.yml to use in Error Message将自定义参数传递给 devise.en.yml 以在错误消息中使用
【发布时间】:2020-01-27 00:26:25
【问题描述】:

如果您在尝试登录应用程序时被锁定,我必须使用 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,设置了默认语言环境,尝试了一大堆在线推荐,但没有运气。

有什么想法吗?

干杯, 阿德里安

【问题讨论】:

  • 嗨,阿德里安,请出示您的查看代码吗?
  • Yml 文件在上面,在我的视图中:<%= I18n.t 'devise.failure.locked', number_of_minutes: '1' %>
  • 嗯似乎是正确的。你还面临这个错误吗?
  • 是的,它发生在帐户被锁定时(例如 3 次错误密码尝试)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) 发生在这里 @@missing_interpolation_argument_handler ||= lambda do |missing_key, provided_hash, string| raise MissingInterpolationArgument.new(missing_key, provided_hash, string) 并且提供的哈希变量包含 {:resource_name=>:user, :authentication_keys=> "电子邮件地址"}

标签: ruby-on-rails devise rails-i18n


【解决方案1】:

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

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

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

替换成你需要的变量

【讨论】:

    猜你喜欢
    • 2019-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-13
    • 2013-02-19
    • 1970-01-01
    • 1970-01-01
    • 2013-03-15
    相关资源
    最近更新 更多