Skip to content

Commit

Permalink
Fix #1851 Update all Carbon classes on Laravel locale change
Browse files Browse the repository at this point in the history
  • Loading branch information
kylekatarnls committed Aug 17, 2019
1 parent 97a0883 commit 238607b
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/Carbon/Laravel/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
namespace Carbon\Laravel;

use Carbon\Carbon;
use Carbon\CarbonImmutable;
use Illuminate\Contracts\Events\Dispatcher as DispatcherContract;
use Illuminate\Events\Dispatcher;
use Illuminate\Events\EventDispatcher;
use Illuminate\Support\Carbon as IlluminateCarbon;
use Illuminate\Support\Facades\Date;
use Throwable;

class ServiceProvider extends \Illuminate\Support\ServiceProvider
{
Expand All @@ -30,7 +34,24 @@ public function boot()
public function updateLocale()
{
$app = $this->app && method_exists($this->app, 'getLocale') ? $this->app : app('translator');
Carbon::setLocale($app->getLocale());
$locale = $app->getLocale();
Carbon::setLocale($locale);
CarbonImmutable::setLocale($locale);

// @codeCoverageIgnoreStart
if (class_exists(IlluminateCarbon::class)) {
IlluminateCarbon::setLocale($locale);
}

if (class_exists(Date::class)) {
try {
$root = Date::getFacadeRoot();
$root->setLocale($locale);
} catch (Throwable $e) {
// Non Carbon class in use in Date facade
}
}
// @codeCoverageIgnoreEnd
}

public function register()
Expand Down

0 comments on commit 238607b

Please sign in to comment.