Skip to content

Commit

Permalink
Merge pull request #1852 from kylekatarnls/feature/1851-fix-carbon-im…
Browse files Browse the repository at this point in the history
…mutable-locale-in-laravel

Fix #1851 Update all Carbon classes on Laravel locale change
  • Loading branch information
kylekatarnls authored Aug 17, 2019
2 parents 97a0883 + 238607b commit 767617a
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 767617a

Please sign in to comment.