laravel-notification-channels / twilio

Twilio notifications channel for Laravel

Home Page:https://laravel-notification-channels.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Support for Localisation?

haakym opened this issue · comments

In Laravel, locale can be set on Notifications:

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

https://laravel.com/docs/5.7/notifications#localizing-notifications

With the exception of using the mail channel handling the locale implementation is up to the developer as is the case with this Twilio package. So currently to make use of the locale when using this package I have done the following in my Notifications using Twilio:

  1. Using the __() helper and passing it the notification's locale:
public function toTwilio($notifiable)
{
    $content = __('welcome.message', [], $this->locale);

    return (new TwilioSmsMessage())->content($content);
}
  1. Using views and Illuminate\Support\Traits\Localizable:
public function toTwilio($notifiable)
{
    return $this->withLocale($this->locale, function() {
        $content = View::make('welcome')->render();

        return (new TwilioSmsMessage())->content($content);
    });
}

I understand that it's not a major inconvenience to either pass the __() helper the notification's locale or make use of the Localizable trait, but I was wondering if there is any scope for localisation to be baked into this package when the SMS is being sent? This package is for use with Laravel Notifications and Notifications in Laravel can have their locale set, so for me it makes complete sense and would make the code within my app a lot cleaner too without having to do my own implementation.

I am thinking it could simply work like the Mail class does with the Localizable trait, for example (a bit contrived):

public function sendMessage(TwilioMessage $message, $to, $useAlphanumericSender = false)
{
  return $this->withLocale($this->locale, function() {
    if ($message instanceof TwilioSmsMessage) {
      if ($useAlphanumericSender && $sender = $this->getAlphanumericSender()) {
        $message->from($sender);
      }

      return $this->sendSmsMessage($message, $to);
    }

    if ($message instanceof TwilioCallMessage) {
      return $this->makeCall($message, $to);
    }

    throw CouldNotSendNotification::invalidMessageObject($message);
  });
}

For the record this is what the Localizable trait does:

<?php

namespace Illuminate\Support\Traits;

use Illuminate\Container\Container;

trait Localizable
{
    /**
     * Run the callback with the given locale.
     *
     * @param  string   $locale
     * @param  \Closure $callback
     * @return mixed
     */
    public function withLocale($locale, $callback)
    {
        if (! $locale) {
            return $callback();
        }

        $app = Container::getInstance();

        $original = $app->getLocale();

        try {
            $app->setLocale($locale);

            return $callback();
        } finally {
            $app->setLocale($original);
        }
    }
}

Any thoughts? Happy to make a PR for this!

Feel free ! :)

Hi @haakym,
For what I can see, the NotificationSender already uses the Localizable trait when sending the notification (https://github.com/laravel/framework/blob/5.8/src/Illuminate/Notifications/NotificationSender.php#L99), so there shouldn't be need to pass again the notification locale to the translating methods.
Am I missing something?

Any news on this? Can I close this issue?

Hey @gregoriohc sorry for the late reply. I think the Localizable trait was used in v5.7 so I guess unless you see value in it being added specifically for use in Laravel versions <= 5.6 perhaps there's not much point?

Since only 5.5 LTS is in active support i'm not sure this is worth working on, but i'll leave it open, but if you would like to PR :)

Closing due to inactivity.