diff --git a/src/Carbon/Laravel/ServiceProvider.php b/src/Carbon/Laravel/ServiceProvider.php index 52d1ad6a04..3fa0d5cd25 100644 --- a/src/Carbon/Laravel/ServiceProvider.php +++ b/src/Carbon/Laravel/ServiceProvider.php @@ -13,7 +13,9 @@ class ServiceProvider extends \Illuminate\Support\ServiceProvider { public function boot() { - if (!$this->app->bound('events') || !$this->app->bound('translator')) { + $this->updateLocale(); + + if (!$this->app->bound('events')) { return; } @@ -24,17 +26,12 @@ public function boot() $events->listen(class_exists('Illuminate\Foundation\Events\LocaleUpdated') ? 'Illuminate\Foundation\Events\LocaleUpdated' : 'locale.changed', function () use ($service) { $service->updateLocale(); }); - $service->updateLocale(); } } public function updateLocale() { - $translator = $this->app['translator']; - - if ($translator instanceof Translator || $translator instanceof IlluminateTranslator) { - Carbon::setLocale($translator->getLocale()); - } + Carbon::setDefaultLocale($this->app->getLocale()); } public function register() diff --git a/src/Carbon/Traits/Localization.php b/src/Carbon/Traits/Localization.php index 6a9ca3c852..909fa9ccb9 100644 --- a/src/Carbon/Traits/Localization.php +++ b/src/Carbon/Traits/Localization.php @@ -25,6 +25,13 @@ */ trait Localization { + /** + * Default locale. + * + * @var string + */ + protected static $defaultLocale; + /** * Default translator. * @@ -101,6 +108,9 @@ protected static function translator() { if (static::$translator === null) { static::$translator = Translator::get(); + if (static::$defaultLocale) { + static::$translator->setLocale(static::$defaultLocale); + } } return static::$translator; @@ -395,6 +405,19 @@ public static function getLocale() return static::translator()->getLocale(); } + /** + * Set the default translator locale. + * Pass 'auto' as locale to use closest language from the current LC_TIME locale. + * + * @param string $locale locale ex. en + * + * @return void + */ + public static function setDefaultLocale($locale) + { + static::$defaultLocale = $locale; + } + /** * Set the current translator locale and indicate if the source locale file exists. * Pass 'auto' as locale to use closest language from the current LC_TIME locale. diff --git a/tests/Laravel/App.php b/tests/Laravel/App.php index 5ce9114e3f..b4509474d8 100644 --- a/tests/Laravel/App.php +++ b/tests/Laravel/App.php @@ -8,6 +8,11 @@ class App implements ArrayAccess { + /** + * @var string + */ + protected $locale; + /** * @var string */ @@ -26,7 +31,7 @@ class App implements ArrayAccess public function register() { include_once __DIR__.'/EventDispatcher.php'; - $this->translator = new Translator('de'); + $this->translator = new Translator($this->locale = 'de'); } public function setEventDispatcher($dispatcher) @@ -50,10 +55,16 @@ public static function getLocaleChangeEventName() public function setLocale($locale) { + $this->locale = $locale; $this->translator->setLocale($locale); $this->events->dispatch(static::getLocaleChangeEventName()); } + public function getLocale() + { + return $this->locale; + } + public function bound($service) { return isset($this->{$service});