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

[6.x] Queued notifications respect user's preferred locale #30429

Closed

Conversation

thijskok
Copy link
Contributor

We use both queued and non-queued notifications and rely on the preferredLocale method for determining the notification's locale. While the non-queued one's are translated using the correct locale, the queued one's still used the default app locale.

It turns out queued notifications can only be configured using the locale fluent method. The preferredLocale method isn't referenced or available in this case. The documentation doesn't mention this exception.

This allows you use the preferredLocale method for notifications, whether you use queuing or not.

@taylorotwell
Copy link
Member

@derekmd does this look correct to you?

@taylorotwell
Copy link
Member

@thijskok I guess I'm a bit confused because the queued job is fired, the queued job calls sendNow on NotificationSender and sendNow already calls the preferredLocale method. So, why is that not working already for you?

@derekmd
Copy link
Contributor

derekmd commented Oct 27, 2019

Hey Taylor, I just tried a manual test with existing Laravel code and I see the same as you. The queue successfully translates Notification email copy in a secondary language set through App\User@preferredLocale(). Example code below:

namespace App;

use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
use Illuminate\Contracts\Translation\HasLocalePreference;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;

class User extends Model implements
    AuthenticatableContract,
    AuthorizableContract,
    CanResetPasswordContract,
    HasLocalePreference,
    Notifiable
{
    // ...


    public function preferredLocale()
    {
        return 'fr';
    }
}
namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;

class FrenchTest extends Notification implements ShouldQueue
{
    use Queueable;

    public function via()
    {
        return ['mail'];
    }

    public function toMail()
    {
        return (new MailMessage)
            ->subject(trans('app.french'))  // "Français" is sent
            ->line(app()->getLocale()); // "fr" is shown
    }
}
app()->isLocale('en');
// true

App\User::first()->notify(new App\Notifications\FrenchTest);
// Sets FrenchTest@locale = 'fr' in the queue payload

Command line:

php artisan queue:work

This sends the email in French.

Keep in mind the queued locale switch only wraps toMail(), toNexmo(), toSlack(), etc. methods. Localization-related code in the constructor will be the original locale when notify was first called.

Can you provide a failing example?

  • Example Notification class that builds a request for an external service (email, SMS, Slack, etc.)
  • How is the notification delivery being calling?
    • Through the Notification facade? Notification::send($user, $notification)
    • User trait method Notifiable@notify()? $user->notify($notification)

@thijskok
Copy link
Contributor Author

Ugh, you guys are absolutely right. My preferredLocale somehow randomly returned a cached copy from another tenant (yes... multi-tenancy... 😢) which threw me completely off-track.

Sorry for wasting your time @taylorotwell and @derekmd, I'm glad quality control is in perfect working order 👍

@thijskok thijskok closed this Oct 27, 2019
@thijskok thijskok deleted the queued-notification-preferred-locale branch October 28, 2019 14:33
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.

3 participants