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.
How make Laravel send email with a locale declared just for a particular email (each recipient has a different language set).
\App::setLocale('pl');
just before the Mail
commandsetting my working middleware in the controller globally in __construct()
:
$this->middleware('setLocale'); // sets the locale to the recipee locale
For now I just add a line inside the email view:
{{ \App::setLocale($lead->client->lang)}}
Any better way to do it? Thank you.
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.
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.
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.
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With