From 9e6887470754489b4c9e218d5ebf8d1e4172096a Mon Sep 17 00:00:00 2001 From: Remon van de Kamp Date: Mon, 8 Oct 2018 20:52:36 +0200 Subject: [PATCH] Invalidate Translator cache when new translation files are added Just before writing the Translator cache, find out in which directories the translation files were found, and add those directories as resources for the catalogue so that when a new file appears in one of those directories the catalogue cache will be invalidated. This may cause some false positives in case non-translation files are in those directories, but since the recommendation is to keep translations on their own, it should not happen. --- .../Component/Translation/Translator.php | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/src/Symfony/Component/Translation/Translator.php b/src/Symfony/Component/Translation/Translator.php index d8d27301f04df..a4efdd15438b3 100644 --- a/src/Symfony/Component/Translation/Translator.php +++ b/src/Symfony/Component/Translation/Translator.php @@ -14,6 +14,8 @@ use Symfony\Component\Config\ConfigCacheFactory; use Symfony\Component\Config\ConfigCacheFactoryInterface; use Symfony\Component\Config\ConfigCacheInterface; +use Symfony\Component\Config\Resource\DirectoryResource; +use Symfony\Component\Config\Resource\FileResource; use Symfony\Component\Translation\Exception\InvalidArgumentException; use Symfony\Component\Translation\Exception\LogicException; use Symfony\Component\Translation\Exception\NotFoundResourceException; @@ -333,6 +335,10 @@ private function dumpCatalogue($locale, ConfigCacheInterface $cache): void $fallbackContent ); + foreach ($this->getDirectoryResources($locale) as $dir) { + $this->catalogues[$locale]->addResource($dir); + } + $cache->write($content, $this->catalogues[$locale]->getResources()); } @@ -463,4 +469,34 @@ private function getConfigCacheFactory(): ConfigCacheFactoryInterface return $this->configCacheFactory; } + + /** + * Fetches a unique list of directories where files were found + * for a catalogue. + * + * @param string $locale + * + * @return DirectoryResource[]|array + */ + private function getDirectoryResources($locale): array + { + $directories = array(); + foreach ($this->catalogues[$locale]->getResources() as $resource) { + if (!$resource instanceof FileResource) { + continue; + } + + $directory = \dirname($resource->getResource()); + if (!\in_array($directory, $directories, true)) { + $directories[] = $directory; + } + } + + return \array_map( + function ($directory) { + return new DirectoryResource($directory); + }, + $directories + ); + } }