Skip to content
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

[9.x] Use preferredLocale when notification is queued #42527

Conversation

welderlourenco-candc
Copy link

@welderlourenco-candc welderlourenco-candc commented May 25, 2022

Description

Laravel's documentation states

[...] Laravel allows you to send mailables in a locale other than the request's current locale, and will even remember this locale if the mail is queued.

[...] Once you have implemented the interface, Laravel will automatically use the preferred locale when sending mailables and notifications to the model.

however, this is only functioning for synced notifications, this PR fixes this by adding the functionality of sending a notification in the user's preferred locale when a notification is queued.

Example

// config/app.php
    'locale' => 'en',
    'fallback_locale' => 'en',

// en/notifications/example.php
return [
    'message' => 'English message'
];

// pt-br/notifications/example.php
return [
    'message' => 'Mensagem em português'
];
class User extends Authenticatable implements HasLocalePreference
{
     // ...

     public function preferredLocale(): string
     {
         return 'pt-br';
     }

     // ...
}
class ExampleNotification extends BaseNotification implements ShouldQueue
{
    use Queueable;
 
    /**
     * Method called by base notification, running inside worker, that fetches/formats a message.
     */
    protected function getMessage()
    {
        return __('notifications/example/message');
    }
}
$user->notify(new ExampleNotification());

// expected message: 'Mensagem em português'
// rendered message: 'English message'

@derekmd
Copy link
Contributor

derekmd commented May 26, 2022

however, this is only functioning for synced notifications

Queued notifications do support the HasLocalePreference contract: #25752

Where is the ExampleNotification::getMessage() method being called? The details of the example's BaseNotification class need to be known to give enough context about what this is attempting to fix.

Related previous reports: #30429 #38486

@welderlourenco-candc
Copy link
Author

Thanks for replying, you're absolutely right. It's gotta be some heavy caching with out current queue setup, tested on a fresh laravel install and it works fine. Please disregard.

@welderlourenco-candc welderlourenco-candc deleted the update-notification-queue-with-preferred-locale branch May 26, 2022 15:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants