Skip to content
Advertisement

Translate queued mails (localization)

I am looking for a working solution, to translate queued emails in . Unfortunately, all emails use the default locale (defined under app.locale).

Let’s assume, we have two emails in the pipeline, one for an English en user and another for an Japanese jp user.

What data should I pass to the Mail facade to translate (localize) the queued emails?

  // User model
  $user = User:find(1)->first();

  Mailer::queue($email, 'Party at Batman's cave (Batcave)', 'emails.party-invitation', [

    ...

    'locale' => $user->getLocale(), // value: "jp", but does not work
    'lang' => $user->getLocale(), // value: "jp", but does not work
    'language' => $user->getLocale(), // value: "jp", but does not work
  ]);

Advertisement

Answer

I have been struggling to get this done in a more efficient way. Currently I have it set up like this. Hopefully this helps someone in the future with this issue:

// Fetch the locale of the receiver.
$user = Auth::user();
$locale = $user->locale;
Mail::queue('emails.welcome.template', ['user' => $user, 'locale' => $locale], function($mail) use ($user, $locale) {
     $mail->to($user->email);
     $mail->subject(
          trans(
               'mails.subject_welcome',
               [], null, $locale
          )
     );
});

And use the following in your template:

{{ trans('mails.welcome', ['name' => ucfirst($user['first_name'])], null, $locale) }}

Note: do not forget to restart your queue

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement