Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Laravel] Defer Translator + Simplify Provider #1777

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions src/Carbon/Laravel/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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()
Expand Down
23 changes: 23 additions & 0 deletions src/Carbon/Traits/Localization.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@
*/
trait Localization
{
/**
* Default locale.
*
* @var string
*/
protected static $defaultLocale;

/**
* Default translator.
*
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down
13 changes: 12 additions & 1 deletion tests/Laravel/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@

class App implements ArrayAccess
{
/**
* @var string
*/
protected $locale;

/**
* @var string
*/
Expand All @@ -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)
Expand All @@ -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});
Expand Down