From 43df0d433c556cd542c4658569d988e8b1253fce Mon Sep 17 00:00:00 2001 From: Bozhidar Hristov Date: Fri, 20 Dec 2024 19:02:52 +0200 Subject: [PATCH] Fix bug where deleted translations remains alive. --- src/Translator.php | 10 ++++++- tests/Integration/TranslatorTest.php | 40 ++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/src/Translator.php b/src/Translator.php index d9fe54d..9d90d6a 100644 --- a/src/Translator.php +++ b/src/Translator.php @@ -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; @@ -45,6 +50,7 @@ protected function loadCatalogue(string $locale): void return; } + $this->originalCatalogues[$locale] = clone $this->catalogues[$locale]; $this->fetchTranslations($locale); } @@ -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( diff --git a/tests/Integration/TranslatorTest.php b/tests/Integration/TranslatorTest.php index 4c79751..0bcb0dc 100644 --- a/tests/Integration/TranslatorTest.php +++ b/tests/Integration/TranslatorTest.php @@ -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. */