Skip to content

Commit

Permalink
Fix bug where deleted translations remains alive.
Browse files Browse the repository at this point in the history
  • Loading branch information
Warxcell committed Dec 20, 2024
1 parent 02eae8a commit 43df0d4
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
*/
class Translator extends OriginalTranslator implements ResetInterface
{
/**
* @var MessageCatalogueInterface[]
*/
private array $originalCatalogues = [];

private Repository|null $repository = null;

private CacheFlag|null $cacheFlag = null;
Expand Down Expand Up @@ -45,6 +50,7 @@ protected function loadCatalogue(string $locale): void
return;
}

$this->originalCatalogues[$locale] = clone $this->catalogues[$locale];
$this->fetchTranslations($locale);
}

Expand Down Expand Up @@ -84,7 +90,9 @@ public function reset(): void
return;
}

foreach ($this->catalogues as $locale => $catalogue) {
foreach (array_keys($this->catalogues) as $locale) {
$catalogue = $this->catalogues[$locale] = clone $this->originalCatalogues[$locale];

$translations = $this->repository->findByLocale($locale);
foreach ($translations as $translation) {
$catalogue->set(
Expand Down
40 changes: 40 additions & 0 deletions tests/Integration/TranslatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,46 @@ public function testCacheFlag(): void
self::assertSame('Hello, world! Edited', $translator->trans('hello_world', locale: 'en'));
}

public function testDeletedTranslation(): void
{
$kernel = self::bootKernel();
$this->buildDb($kernel);

/** @var EntityManagerInterface $entityManager */
$entityManager = static::getContainer()->get(EntityManagerInterface::class);

$bg = new Language('bg');
$entityManager->persist($bg);

$en = new Language('en');
$entityManager->persist($en);

$helloWorldInBg = new Translation($bg, new Token('hello_world', 'messages'), 'Здравей, свят!');
$helloWorldInEn = new Translation($en, new Token('hello_world', 'messages'), 'Hello, world!');
$entityManager->persist($helloWorldInBg);
$entityManager->persist($helloWorldInEn);
$entityManager->flush();

/** @var TranslatorInterface $translator */
$translator = static::getContainer()->get(TranslatorInterface::class);

self::assertSame('Здравей, свят!', $translator->trans('hello_world', locale: 'bg'));

$entityManager->remove($helloWorldInBg);
$entityManager->flush();

/** @var CacheFlag $cacheFlag */
$cacheFlag = static::getContainer()->get(CacheFlag::class);
$cacheFlag->increment($cacheFlag->getVersion());

if ($translator instanceof ResetInterface) {
$translator->reset();
}

// deleted BG trans should fallback to EN
self::assertSame('Hello, world!', $translator->trans('hello_world', locale: 'bg'));
}

/**
* When Symfony clear cache, tables might not be created, no exception should be thrown in that case.
*/
Expand Down

0 comments on commit 43df0d4

Please sign in to comment.