简体   繁体   中英

Use custom locale in devise emails

I am using devise_invitable gem in rails and I have configured a number of locales in my rails app. What I would like to do is to invite a user with a locale I would specify myself. I t could look something like this:

User.invite!(:email => "test@example.com", :locale => 'fr')

This would send an email with the 'fr' as locale , even when I18n.locale would be en .

Is this possible, even with a complete different syntax than the one I am using above ?

Digging, I found that devise_invitable uses the devise mailer, see source . I'm not 100% sure how I18n.t exactly works, but I suppose you know that. So use alias_method_chain to monkey-patch the translate method. To pass the other language, you can choose one:

  1. modify the whole call stack to pass down the variable
  2. use a pseudo-global variable via Thread.current[] .

I propose to use 2., make your choice. Then use that variable to return the correct translation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM