Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 9e68874

Browse filesBrowse files
committed
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.
1 parent d482f0a commit 9e68874
Copy full SHA for 9e68874

File tree

1 file changed

+36
-0
lines changed
Filter options

1 file changed

+36
-0
lines changed

‎src/Symfony/Component/Translation/Translator.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Translation/Translator.php
+36Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
use Symfony\Component\Config\ConfigCacheFactory;
1515
use Symfony\Component\Config\ConfigCacheFactoryInterface;
1616
use Symfony\Component\Config\ConfigCacheInterface;
17+
use Symfony\Component\Config\Resource\DirectoryResource;
18+
use Symfony\Component\Config\Resource\FileResource;
1719
use Symfony\Component\Translation\Exception\InvalidArgumentException;
1820
use Symfony\Component\Translation\Exception\LogicException;
1921
use Symfony\Component\Translation\Exception\NotFoundResourceException;
@@ -333,6 +335,10 @@ private function dumpCatalogue($locale, ConfigCacheInterface $cache): void
333335
$fallbackContent
334336
);
335337

338+
foreach ($this->getDirectoryResources($locale) as $dir) {
339+
$this->catalogues[$locale]->addResource($dir);
340+
}
341+
336342
$cache->write($content, $this->catalogues[$locale]->getResources());
337343
}
338344

@@ -463,4 +469,34 @@ private function getConfigCacheFactory(): ConfigCacheFactoryInterface
463469

464470
return $this->configCacheFactory;
465471
}
472+
473+
/**
474+
* Fetches a unique list of directories where files were found
475+
* for a catalogue.
476+
*
477+
* @param string $locale
478+
*
479+
* @return DirectoryResource[]|array
480+
*/
481+
private function getDirectoryResources($locale): array
482+
{
483+
$directories = array();
484+
foreach ($this->catalogues[$locale]->getResources() as $resource) {
485+
if (!$resource instanceof FileResource) {
486+
continue;
487+
}
488+
489+
$directory = \dirname($resource->getResource());
490+
if (!\in_array($directory, $directories, true)) {
491+
$directories[] = $directory;
492+
}
493+
}
494+
495+
return \array_map(
496+
function ($directory) {
497+
return new DirectoryResource($directory);
498+
},
499+
$directories
500+
);
501+
}
466502
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.