【问题标题】:Notification locale ignored for Twilio in Laravel 5.7Laravel 5.7 中 Twilio 的通知区域被忽略
【发布时间】:2019-06-23 14:39:38
【问题描述】:

我正在编写测试来验证是否为通知设置了区域设置,并断言通知内容中使用了正确的翻译。使用 Twilio 时,我无法在通知上设置区域设置:https://github.com/laravel-notification-channels/twilio

我有一个使用Mailable 的邮件通道的通知测试,它按预期工作:

public function test_mail_notifications_use_localisation()
{
    $user = factory(User::class)->create();

    $user->notify(
        (new WelcomeMailNotificationStub)
            ->locale('fr')
    );

    $this->assertContains('Bonjour Le Monde',
        app('swift.transport')->messages()[0]->getBody()
    );
}

class WelcomeMailStub extends \Illuminate\Mail\Mailable
{
    public function build()
    {
        return $this->view('welcome-notification');
    }
}

class WelcomeMailNotificationStub extends \Illuminate\Notifications\Notification
{
    public function via($notifiable)
    {
        return ['mail'];
    }

    public function toMail($notifiable)
    {
        return (new WelcomeMailStub);
    }
}

我想为通过 Twilio 发送的 SMS 通知编写一个类似的测试,但到目前为止,在我的尝试中,发送通知时似乎忽略了区域设置,而是使用了默认区域设置。这是我想出的:

public function test_sms_notifications_use_localisation()
{
    Notification::fake();

    $user = factory(User::class)->create();

    $user->notify(
        (new WelcomeSmsNotificationStub)
            ->locale('fr')
    );

    Notification::assertSentTo(
        $user,
        WelcomeSmsNotificationStub::class,
        function ($notification, $channels) use ($user)
        {
           return $notification->toTwilio($user)->content === 'Bonjour Le Monde'; // test fails here
        }
    );
}

class WelcomeSmsNotificationStub extends \Illuminate\Notifications\Notification
{
    public function via($notifiable)
    {
        return [TwilioChannel::class];
    }

    public function toTwilio($notifiable)
    {
        return (new TwilioSmsMessage())
            ->content(__('welcome_notification.opening_text'));
    }
}

如果我在assertSentTo 的回调中dd() 像这样:

Notification::assertSentTo(
    $user,
    WelcomeSmsNotificationStub::class,
    function ($notification, $channels) use ($user)
    {
       dd(
           $notification,
           $notification->toTwilio($user)
       );

       return $notification->toTwilio($user)->content === 'Bonjour Le Monde'; // test fails here
    }
);

我得到以下信息:

Tests\Unit\Notifications\WelcomeSmsNotificationStub {#116
  +id: "ae164ce6-fa47-4730-b401-e6cc15b27e16"
  +locale: "fr"
}
NotificationChannels\Twilio\TwilioSmsMessage {#2414
  +alphaNumSender: null
  +applicationSid: null
  +maxPrice: null
  +provideFeedback: null
  +validityPeriod: null
  +content: "Default welcome" <== using the default welcome instead of 'fr'
  +from: null
  +statusCallback: null
  +statusCallbackMethod: null
}

任何关于如何使这项工作的建议将不胜感激,谢谢!

【问题讨论】:

    标签: php laravel twilio


    【解决方案1】:

    用户exohjosh 已解决该问题。我需要在 SMS 通知的 toTwilio 方法中明确设置翻译字符串的语言环境,如下所示:

    __('welcome_notification.opening_text', [], $this->locale)
    

    而当使用 Laravel 的 Mailable 时,这是自动完成的。

    编辑

    如果您使用视图,事件更好的解决方案是使用Illuminate\Support\Traits\Localizable,那么您可以这样做:

    public function toTwilio($notifiable)
    {
        return $this->withLocale($this->locale, function() {
            $content = View::make('welcome-notification')->render();
    
            return (new TwilioSmsMessage())->content($content);
        });
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-18
      • 1970-01-01
      • 2018-06-07
      • 1970-01-01
      • 1970-01-01
      • 2018-03-29
      相关资源
      最近更新 更多