Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how change locale in email being sent from controller - laravel 5.4

I am successfully sending emails with this piece of code:

 \App::setLocale('pl');
 Mail::send('emails.new_'.$itemtype, ['object' => $object, 'email' =>     $email_recipee], function ($m) use ( $email_recipee, $object, $itemtype) {
 $m->to($email_recipee, 'Title')->subject(' Subject of email');
 //
 });

But the emails are translated to en, the default language of the app.

My question

How make Laravel send email with a locale declared just for a particular email (each recipient has a different language set).

What I tried:

  • \App::setLocale('pl'); just before the Mail command
  • setting my working middleware in the controller globally in __construct():

    $this->middleware('setLocale'); // sets the locale to the recipee locale

dirty (?) solution

For now I just add a line inside the email view:

{{ \App::setLocale($lead->client->lang)}}

Any better way to do it? Thank you.

like image 459
Peter Avatar asked Feb 09 '17 08:02

Peter


People also ask

How do I change my locale in Laravel?

Laravel's gives you the option to change the locale for a single Http Request by executing the setLocale method on App Facade App::setLocale($locale); , But what if once the language is changed you don't want to worry about setting the Locale and this should be taken care by an automatic code logic.

What is @lang in Laravel?

Laravel-lang is a collection of over 68 language translations for Laravel by developer Fred Delrieu (caouecs), including authentication, pagination, passwords, and validation rules. This package also includes JSON files for many languages.

Can we send Mail from localhost in Laravel?

In the same way, you can also set up other Mail providers like Mailgun, Sendgrid, Mandrill, Mailchimp, etc. to send the mail from Localhost in Laravel.


1 Answers

TL;DR: Just set a $locale field in an object of Mailable class:

class ResetPassword extends Mailable
{
   // ...

    /**
     * Create a new message instance.
     */
    public function __construct(User $user, string $token)
    {
        $this->user = $user;
        $this->token = $token;
        /*
         * Set locale of mail to User's locale
         */
        $this->locale = $this->user->locale; // <-- of course adapt this line to YOUR User model :]
    }

Long story:

There is a trait Localizable. It declares a method withLocale that calls a given function "enveloped" in a specified locale. By "enveloping" I mean: remember a current locale, set a new locale, call a function, restore the remembered locale.

An example of usage can be found in \Illuminate\Mail\Mailable::send:

public function send(MailerContract $mailer)
{
    return $this->withLocale($this->locale, function () use ($mailer) {
        Container::getInstance()->call([$this, 'build']);

        return $mailer->send($this->buildView(), $this->buildViewData(), function ($message) {
            $this->buildFrom($message)
                 ->buildRecipients($message)
                 ->buildSubject($message)
                 ->runCallbacks($message)
                 ->buildAttachments($message);
        });
    });
}

However, this method gives us a clue how to change a locale for a mail - in the first line there is $this->locale. Indeed, Mailable has this field defined:

class Mailable implements MailableContract, Renderable
{
   // ...

    /**
     * The locale of the message.
     *
     * @var string
     */
    public $locale;
like image 60
jacek.ciach Avatar answered Oct 28 '22 05:10

jacek.ciach