Design and localize your own mail template

I am using devem gem and want to translate a confirmation email. I already have my own template and overridden mail method:

class LocalizedDeviseMailer < Devise::Mailer def confirmation_instructions(record, locale) @locale = locale super end end 

So, in my template, I can do something like this:

 I18n.locale = @locale 

And then:

 t("it.really.works") 

But I don’t know how to pass my language variable to the mailer method. What is the best way to do this? Any help would be appreciated.

+8
ruby ruby-on-rails internationalization devise actionmailer
source share
4 answers

Devise offers localization of the mail template "natively".

look at the development source code

https://github.com/plataformatec/devise/blob/master/lib/devise/mailers/helpers.rb This file explains how to localize a theme (to be added to locale files)

  # Setup a subject doing an I18n lookup. At first, it attemps to set a subject # based on the current mapping: # # en: # devise: # mailer: # confirmation_instructions: # user_subject: '...' # 

This is a body template that you need to localize like any other html.erb

https://github.com/plataformatec/devise/blob/master/app/views/devise/mailer/confirmation_instructions.html.erb

Depending on whether your new sign_up user will use http://yoursite/it/users/sign_up or http://yoursite/en/users/sign_up (as you would normally do on your routes for your localized application), a good localized subject and mail (in the first case in Italian, in the latter in English).

+8
source share

I recommend adding the locale column to the user model and using your own mailer. Thus, you also have more flexibility if you plan to set your own stylesheets and from or add additional letters.

in config/initializer/devise.rb :

 Devise.setup do |config| ... config.mailer = "UserMailer" ... end 

in app/mailers/user_mailer.rb

 class UserMailer < Devise::Mailer default from: "noreply@yourdomain.com" def confirmation_instructions(user) @user = user set_locale(@user) mail to: @user.email end def reset_password_instructions(user) @user = user set_locale(@user) mail to: @user.email end def unlock_instructions(user) @user = user set_locale(@user) mail to: @user.email end private def set_locale(user) I18n.locale = user.locale || I18n.default_locale end end 
+6
source share

I think the easiest way to do this is to add it to the record. That way you can add a locale column to your User or juste add attr_accessor :locale to your user model

So, you just need to define this language in your entry and use it with I18n.locale = record.locale

+1
source share

Another way is to add an initializer:

 require 'devise/mailer' module Devise class Mailer module Localized %w( confirmation_instructions reset_password_instructions unlock_instructions ).each do |method| define_method(method) do |resource, *args| I18n.with_locale(resource.try(:locale)) do super(resource, *args) end end end end prepend Localized end end 

For ruby ​​<2.1, you can use the problem with alias_method_chain .

0
source share

All Articles