[5.7] Mailables can hook into LocaleUpdated #24451
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This makes
send()
of translatedMailable
(and eventuallyNotification
) objects dispatch the eventIlluminate\Foundation\Events\LocaleUpdated
to allow for more complete localization. For example:Carbon
-formatted timestampsroute()
country/region URI prefixesTranslatable mailable classes were added in 5.6.5 (#23178) to allow translated email subjects and view bodies, but the above localizable content types are left unchanged for queued items.
Example to localize mailable timestamps
When the default queue worker locale is
'en'
, callingMail::to($francophone)->locale('fr')->queue(new Schedule);
would show French formatting forCarbon
timestamps using this event listener.Flexibility in implementation
I chose the simplest solution (1. below) but this pull request to can be changed one of the following (or another approach entirely.)
Simplest: Assume by localizing a mailable, the app container instance has
getLocale()
/setLocale()
methods.Hack: Check explicitly for Laravel's concrete
Application
container.Custom Container-friendly Contract: Make Laravel's app container implement a new interface.
This changes the method signature of
Illuminate\Support\Traits\Localizable@withLocale()
so this pull request is proposed for 5.7.