Search code examples
ruby-on-railsdeviserails-i18ndevise-invitable

Rails 5.1 devise_invitable gem ignoring i18n translation file


I have added the devise_invitable gem and a custom mailer. It works and normal devise emails (eg. confirmation_instructions) are reading the i18n file correctly, but invitation_instructions is not.

Here is the 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"

The custom mailer 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

If I change the invitation_instructions method to this:

  def invitation_instructions(record, token, opts={})
    custom_options(record, opts, :invitation_instructions)
    opts[:subject] = "TEST"
    super
  end  

then the invitation email subject correctly changes to 'TEST'.

So why is it not reading the i18n file?

NOTE

I also generated the default invitation view and that is also not reading the i18n file. For example the first line of the default view is:

\app\views\devise\mailer\invitation_instructions.html.erb

<p>
  <%= t("devise.mailer.invitation_instructions.hello", email: @resource.email) %>
</p>

Which should render 'Oi' from the i18n file, but it renders the default 'Hello' instead.


Solution

  • It turns out that the devise_invitable gem does not read devise.en.yml because it creates its OWN locale file:

    devise_invitable.en.yml
    

    and so that is where I need to make the text customisations.