Skip to content

Commit

Permalink
Fix translator and interval serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
kylekatarnls committed Apr 23, 2024
1 parent 7219739 commit 9b3d3a2
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 6 deletions.
29 changes: 23 additions & 6 deletions src/Carbon/AbstractTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,7 @@ public static function get(?string $locale = null): static

public function __construct($locale, ?MessageFormatterInterface $formatter = null, $cacheDir = null, $debug = false)
{
parent::setLocale($locale);
$this->initializing = true;
$this->directories = [__DIR__.'/Lang'];
$this->addLoader('array', new ArrayLoader());
parent::__construct($locale, new MessageFormatterMapper($formatter), $cacheDir, $debug);
$this->initializing = false;
$this->initialize($locale, $formatter, $cacheDir, $debug);
}

/**
Expand Down Expand Up @@ -371,6 +366,28 @@ public function __debugInfo()
];
}

public function __serialize(): array
{
return [
'locale' => $this->getLocale(),
];
}

public function __unserialize(array $data): void
{
$this->initialize($data['locale'] ?? 'en');
}

private function initialize($locale, ?MessageFormatterInterface $formatter = null, $cacheDir = null, $debug = false): void
{
parent::setLocale($locale);
$this->initializing = true;
$this->directories = [__DIR__.'/Lang'];
$this->addLoader('array', new ArrayLoader());
parent::__construct($locale, new MessageFormatterMapper($formatter), $cacheDir, $debug);
$this->initializing = false;
}

private static function compareChunkLists($referenceChunks, $chunks)
{
$score = 0;
Expand Down
13 changes: 13 additions & 0 deletions tests/CarbonInterval/ConstructTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -429,4 +429,17 @@ public function testEnums()
$this->assertCarbonInterval(CarbonInterval::make(3, Unit::Hour), 0, 0, 0, 3, 0, 0);
$this->assertCarbonInterval(CarbonInterval::make(Unit::Week), 0, 0, 7, 0, 0, 0);
}

public function testFromSerialization()
{
$past = new Carbon('-3 Days');
$today = new Carbon('today');
$interval = $today->diffAsCarbonInterval($past);
$copy = unserialize(serialize($interval));

$this->assertSame(['en'], array_keys($interval->getLocalTranslator()->getMessages()));

Check failure on line 440 in tests/CarbonInterval/ConstructTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1)

Call to an undefined method Symfony\Contracts\Translation\TranslatorInterface::getMessages().

Check failure on line 440 in tests/CarbonInterval/ConstructTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1)

Call to an undefined method Symfony\Contracts\Translation\TranslatorInterface::getMessages().
$this->assertSame(['en'], array_keys($copy->getLocalTranslator()->getMessages()));

$this->assertEquals($interval, $copy);
}
}
21 changes: 21 additions & 0 deletions tests/Language/TranslatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,25 @@ public function testTranslatorImmutable()
);
TranslatorImmutable::get('en')->setTranslations([]);
}

public function testSerializationKeepLocale()
{
$translator = TranslatorImmutable::get('de');

$this->assertEquals('de', unserialize(serialize($translator))->getLocale());

$past = new Carbon('-3 Days');
$today = new Carbon('today');
$interval = $today->diffAsCarbonInterval($past);
$translator = $interval->getLocalTranslator();

$this->assertEquals('en', unserialize(serialize($translator))->getLocale());

$past = new Carbon('-3 Days');
$today = new Carbon('today');
$interval = $today->locale('zh')->diffAsCarbonInterval($past);
$translator = $interval->getLocalTranslator();

$this->assertEquals('zh', unserialize(serialize($translator))->getLocale());
}
}

0 comments on commit 9b3d3a2

Please sign in to comment.