Laravel 5.7 Mail Localization Improvements
Laravel 5.7 is released and free for use now. Click here to install laravel. And in this post series we are going to talk what is new in laravel 5.7.
So in laravel 5.7 the improvement of mail localization can be recognized as a huge step for notification management.With the release of Laravel 5.7 the
Illuminate\Notifications\Notification
class started offering a locale method to set the desired language. The
application will change into this locale when the notification is being
formatted and then revert back to the previous locale when formatting
is complete.
Let's see an example:
$user->notify((new InvoicePaid($invoice))->locale('es'));
Now with Laravel 5.7.7 you can take this a step further and hook
directly into a “user” model. In a typical application you might have a
“locale” field on the user table and now you can define the User model
as follows,
use Illuminate\Contracts\Translation\HasLocalePreferences;
class User extends Model implements HasLocalePreference
{
public function preferredLocale(){
return $this->locale;
}
}
Now imagine that you create a user from Germany.
$user = User::create([
'email' => 'a@a.com',
'locale' => 'de',
]);
$user = User::create([
'email' => 'a@a.com',
'locale' => 'de',
]);
(here the code 'de' helps to translate the content to German).
The following four calls will send the notification to the user in Germany.
$user->notify(new OrderConfirmation($order));
Nortification::send($user, new OrderConfirmation($order));
Mail::to($user)->queue(new OrderConfirmation($order);
Mail::to($user)->send(new OrderConfirmation($order));
$user->notify(new OrderConfirmation($order));
Nortification::send($user, new OrderConfirmation($order));
Mail::to($user)->queue(new OrderConfirmation($order);
Mail::to($user)->send(new OrderConfirmation($order));
Of course, if you need to over-ride a user locale preference you can by specifying the
locale()
like this.
$user->notify((new OrderConfirmation($order))->locale('en'));
As you can see, the inclusion of this feature will allow you to simplify
a lot of your code when sending emails to people throughout the world. Mail localization help the translation process for developers to different languages. This reduce lot of coding stuffs for developers
.
Resources : https://laravel-news.com/mail-localization
No comments:
Post a Comment