Rails 5.1 devise_invitable gem 忽略 i18n 翻译文件
Rails 5.1 devise_invitable gem ignoring i18n translation file
我添加了 devise_invitable gem 和一个自定义邮件程序。它有效,正常设计的电子邮件(例如 confirmation_instructions)正在正确读取 i18n 文件,但 invitation_instructions 不是。
这里是 devise.en.yml:
en:
devise:
mailer:
confirmation_instructions:
subject: "Hi %{first_name}, please confirm your email to activate your account"
invitation_instructions:
subject: "%{first_name} has invited you to join their team!"
hello: "Oi"
someone_invited_you: "Pig"
自定义邮件程序 class:
class CustomDeviseMailer < Devise::Mailer
def confirmation_instructions(record, token, opts={})
custom_options(record, opts, :confirmation_instructions)
super
end
def invitation_instructions(record, token, opts={})
custom_options(record, opts, :invitation_instructions)
super
end
private
def custom_options(record, opts, key)
opts[:subject] = I18n.t('subject', scope: [:devise, :mailer, key], first_name: record.first_name)
end
end
如果我将 invitation_instructions 方法更改为:
def invitation_instructions(record, token, opts={})
custom_options(record, opts, :invitation_instructions)
opts[:subject] = "TEST"
super
end
然后邀请电子邮件主题正确更改为 'TEST'。
为什么它不读取 i18n 文件?
注意
我还生成了默认邀请视图,但也没有读取 i18n 文件。例如默认视图的第一行是:
\app\views\devise\mailer\invitation_instructions.html.erb
<p>
<%= t("devise.mailer.invitation_instructions.hello", email: @resource.email) %>
</p>
应该从 i18n 文件呈现 'Oi',但它呈现默认的 'Hello'。
事实证明 devise_invitable gem 没有读取 devise.en.yml 因为它创建了自己的语言环境文件:
devise_invitable.en.yml
这就是我需要进行文本自定义的地方。
我添加了 devise_invitable gem 和一个自定义邮件程序。它有效,正常设计的电子邮件(例如 confirmation_instructions)正在正确读取 i18n 文件,但 invitation_instructions 不是。
这里是 devise.en.yml:
en:
devise:
mailer:
confirmation_instructions:
subject: "Hi %{first_name}, please confirm your email to activate your account"
invitation_instructions:
subject: "%{first_name} has invited you to join their team!"
hello: "Oi"
someone_invited_you: "Pig"
自定义邮件程序 class:
class CustomDeviseMailer < Devise::Mailer
def confirmation_instructions(record, token, opts={})
custom_options(record, opts, :confirmation_instructions)
super
end
def invitation_instructions(record, token, opts={})
custom_options(record, opts, :invitation_instructions)
super
end
private
def custom_options(record, opts, key)
opts[:subject] = I18n.t('subject', scope: [:devise, :mailer, key], first_name: record.first_name)
end
end
如果我将 invitation_instructions 方法更改为:
def invitation_instructions(record, token, opts={})
custom_options(record, opts, :invitation_instructions)
opts[:subject] = "TEST"
super
end
然后邀请电子邮件主题正确更改为 'TEST'。
为什么它不读取 i18n 文件?
注意
我还生成了默认邀请视图,但也没有读取 i18n 文件。例如默认视图的第一行是:
\app\views\devise\mailer\invitation_instructions.html.erb
<p>
<%= t("devise.mailer.invitation_instructions.hello", email: @resource.email) %>
</p>
应该从 i18n 文件呈现 'Oi',但它呈现默认的 'Hello'。
事实证明 devise_invitable gem 没有读取 devise.en.yml 因为它创建了自己的语言环境文件:
devise_invitable.en.yml
这就是我需要进行文本自定义的地方。