You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Rails 5.1中devise_invitable gem忽略I18n翻译文件问题

Fixing Devise Invitable's Invitation Instructions Not Reading I18n Files

Let’s break down why your invitation_instructions isn’t pulling from your devise.en.yml while other Devise emails work, and how to fix it:

1. Check Your Custom Mailer’s Parent Class

This is the most common culprit. Regular Devise emails use Devise::Mailer, but Devise Invitable has its own mailer class that handles invitation-specific I18n logic. If your custom mailer inherits only from Devise::Mailer, it won’t pick up the invitation translations properly.

Update your custom mailer to inherit from Devise::Invitable::Mailer instead:

class CustomDeviseMailer < Devise::Invitable::Mailer
  # Your custom logic here (e.g., adding extra variables, modifying headers)
end

2. Verify Devise Configuration

Make sure your config/initializers/devise.rb points to your custom mailer for both general Devise and Invitable:

Devise.setup do |config|
  # Set the global Devise mailer
  config.mailer = 'CustomDeviseMailer'

  # Optional: Explicitly set the Invitable mailer if you need separate handling
  config.invitable.mailer = 'CustomDeviseMailer'
end

3. Don’t Break the Super Call If You’re Overriding the Method

If you’ve overridden the invitation_instructions method in your custom mailer, always call super to let the parent Invitable mailer handle the I18n lookup. Skipping this will bypass the built-in translation logic:

def invitation_instructions(record, token, opts={})
  # Add your custom tweaks here (e.g., set additional instance variables)
  @team_name = record.team.name

  # Let the parent class handle the translation and email structure
  super
end

4. Test I18n Directly in the Console

To rule out issues with your YAML file, test the translation directly in a Rails console:

I18n.t('devise.mailer.invitation_instructions.subject', first_name: 'John')

If this returns the correct string, your YAML is fine, and the problem is in your mailer setup. If not, double-check for typos in your devise.en.yml (e.g., misspelled keys, incorrect nesting).

5. Ensure Your Email Templates Use I18n Calls

If you’ve customized the invitation_instructions view template, make sure you’re using the correct I18n keys instead of hardcoded text. For example, in app/views/custom_devise_mailer/invitation_instructions.html.erb:

<h1><%= t('devise.mailer.invitation_instructions.hello') %></h1>
<p><%= t('devise.mailer.invitation_instructions.someone_invited_you') %></p>

内容的提问来源于stack exchange,提问作者rmcsharry

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 08:27:07