Skip to content

Commit

Permalink
Copy locale only if previous entity has a specific one
Browse files Browse the repository at this point in the history
  • Loading branch information
kylekatarnls committed Jan 10, 2020
1 parent f279df6 commit ae41561
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
7 changes: 4 additions & 3 deletions src/Carbon/CarbonPeriod.php
Original file line number Diff line number Diff line change
Expand Up @@ -1446,10 +1446,11 @@ public function rewind()
$this->key = 0;
$this->current = call_user_func([$this->dateClass, 'make'], $this->startDate);
$settings = $this->getSettings();
$locale = $this->getTranslatorLocale();
if ($locale) {
$settings['locale'] = $locale;

if ($this->hasLocalTranslator()) {
$settings['locale'] = $this->getTranslatorLocale();
}

$this->current->settings($settings);
$this->timezone = static::intervalHasTime($this->dateInterval) ? $this->current->getTimezone() : null;

Expand Down
8 changes: 7 additions & 1 deletion src/Carbon/Traits/Creator.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,13 @@ public static function instance($date)
$instance = new static($date->format('Y-m-d H:i:s.u'), $date->getTimezone());

if ($date instanceof CarbonInterface || $date instanceof Options) {
$instance->settings($date->getSettings());
$settings = $date->getSettings();

if (!$date->hasLocalTranslator()) {
unset($settings['locale']);
}

$instance->settings($settings);
}

return $instance;
Expand Down
10 changes: 10 additions & 0 deletions src/Carbon/Traits/Localization.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,16 @@ public static function setTranslator(TranslatorInterface $translator)
static::$translator = $translator;
}

/**
* Return true if the current instance has its own translator.
*
* @return bool
*/
public function hasLocalTranslator()
{
return isset($this->localTranslator);
}

/**
* Get the translator of the current instance or the default if none set.
*
Expand Down

0 comments on commit ae41561

Please sign in to comment.