-
Notifications
You must be signed in to change notification settings - Fork 11k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[5.7] Mail recipient and notifiable preferred locale #25752
[5.7] Mail recipient and notifiable preferred locale #25752
Conversation
Thank you @derekmd :) |
This is fantastic - thank you so much! |
Greate Work! thanks |
This is amazing!! Thank you |
Great work, |
I noticed this actually allows route-based queued notification translations even without storing locale in the database: use Illuminate\Contracts\Translation\HasLocalePreference;
class User extends Model implements HasLocalePreference
{
/**
* Use the application's locale when it's secondary.
*
* @return string|null
*/
public function preferredLocale()
{
return collect(config('app.supported_locales'))
->keys()
->intersect(app()->getLocale())
->first();
}
} So if you're not worried about the app sending cron-job or admin-triggered notifications, this will always include the application's non-default locale in the queued payload. Useful for a translated landing page (w/ email capture) or marketing-only apps without much of a PHP backend. |
Nice, but what about standard mails like email verification? Will they be translated? |
Great work, |
I'm getting error |
This update removes the need to call the However, I'm using the I could just add the |
Yes, there can be multiple locales sent in one call when many users have different settings. That (temporary) locale isn't copied into the Instead of |
This wraps up localized notifications introduced by #23178, #24451, and #24919.
Users can have a preferred locale (likely stored in the
users
database table) which theMail
andNotification
services will use when queuing jobs.Why store a user locale preference?
Mail::locale()
andNotification::locale()
. A typical localized scenario:'en'
App::setLocale('de')
for German translations'en'
instead of 'de
'Mail::to()->locale()
Notification::locale()
Example
User
model with stored locale preferenceThese four calls will send the notification in German:
$user->notify(new OrderConfirmation($order))
Notification::send($user, new OrderConfirmation($order))
Mail::to($user)->queue(new OrderConfirmation($order)
Mail::to($user)->send(new OrderConfirmation($order)
Notifying multiple recipients with mixed locale preferences
This call will notify Taylor in English and Mohamed in Arabic:
Notification::send($users, new OrderConfirmation($order))
This call will send the email in the application's default locale:
Mail::to($users)->queue(new OrderConfirmation($order))
Back in February I wrote a rambling explanation here: #23178 (comment) In summary, different message bodies would deviate from the email spec. If user-land wishes to breakup mailables into multiple translated emails being sent out,
Illuminate\Mail\Mailer
is macroable. e.g.,Overriding user preferred locale
If admins sending email wish to choose the locale, the
locale()
method call is used instead of the recipient's preference. These four calls will send the notification in English even if$user
has a German preference:$user->notify((new OrderConfirmation($order))->locale('en'))
Notification::locale('en')->send($user, new OrderConfirmation($order))
Mail::to($user)->locale('en')->queue(new OrderConfirmation($order))
Mail::to($user)->locale('en')->send(new OrderConfirmation($order))
Auth scaffolding
User
model classIt might be worth introducing an empty implementation of this contract to https://github.com/laravel/laravel/blob/master/app/User.php?
Returning
null
has no effect on theMail
andNotification
services (the app default locale is used instead) so new projects are aware they can implement this feature. However I imagine a small percentage of Laravel projects actually use translations so it could be considered more scaffolding noise to strip out. Maybe just some laravel.com docs will do.