Skip to content

Commit de30765

Browse files
committed
bug #34129 [FrameworkBundle][Translation] Invalidate cached catalogues when the scanned directories change (fancyweb)
This PR was merged into the 4.3 branch. Discussion ---------- [FrameworkBundle][Translation] Invalidate cached catalogues when the scanned directories change | Q | A | ------------- | --- | Branch? | 4.3 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | symfony/symfony#33992 | License | MIT | Doc PR | - The cache file name needs to depend on the scanned directories list. Otherwise, when a new directory is added, even if the container is rebuilt and the `FWB Translator` gets the new scanned directories list, the cached catalogue name is still the same and is resolved accordingly. An alternative would be to make the `Translation Translator` `getCatalogueCachePath()` method and `fallbackLocales` `@internal` and `protected` to just override everything in the `FWB Translator`. The `cacheVary` argument has the benefit to be reusable by all the `Translation` component users. Note that there is a negative minor performance impact that increases when the list of scanned directories grows. Commits ------- 6cbee0944c [FrameworkBundle][Translation] Invalidate cached catalogues when the scanned directories change
2 parents 6336efc + fe73edf commit de30765

File tree

4 files changed

+42
-3
lines changed

4 files changed

+42
-3
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
foo: bar

Tests/Translation/TranslatorTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Symfony\Component\Config\Resource\FileExistenceResource;
1919
use Symfony\Component\Filesystem\Filesystem;
2020
use Symfony\Component\Translation\Formatter\MessageFormatter;
21+
use Symfony\Component\Translation\Loader\YamlFileLoader;
2122
use Symfony\Component\Translation\MessageCatalogue;
2223

2324
class TranslatorTest extends TestCase
@@ -244,6 +245,41 @@ public function testCatalogResourcesAreAddedForScannedDirectories()
244245
$this->assertEquals(new FileExistenceResource('/tmp/I/sure/hope/this/does/not/exist'), $resources[2]);
245246
}
246247

248+
public function testCachedCatalogueIsReDumpedWhenScannedDirectoriesChange()
249+
{
250+
/** @var Translator $translator */
251+
$translator = $this->getTranslator(new YamlFileLoader(), [
252+
'cache_dir' => $this->tmpDir,
253+
'resource_files' => [
254+
'fr' => [
255+
__DIR__.'/../Fixtures/Resources/translations/messages.fr.yml',
256+
],
257+
],
258+
'scanned_directories' => [
259+
__DIR__.'/../Fixtures/Resources/translations/',
260+
],
261+
], 'yml');
262+
263+
// Cached catalogue is dumped
264+
$this->assertSame('répertoire', $translator->trans('folder', [], 'messages', 'fr'));
265+
266+
$translator = $this->getTranslator(new YamlFileLoader(), [
267+
'cache_dir' => $this->tmpDir,
268+
'resource_files' => [
269+
'fr' => [
270+
__DIR__.'/../Fixtures/Resources/translations/messages.fr.yml',
271+
__DIR__.'/../Fixtures/Resources/translations2/ccc.fr.yml',
272+
],
273+
],
274+
'scanned_directories' => [
275+
__DIR__.'/../Fixtures/Resources/translations/',
276+
__DIR__.'/../Fixtures/Resources/translations2/',
277+
],
278+
], 'yml');
279+
280+
$this->assertSame('bar', $translator->trans('foo', [], 'ccc', 'fr'));
281+
}
282+
247283
protected function getCatalogue($locale, $messages, $resources = [])
248284
{
249285
$catalogue = new MessageCatalogue($locale);

Translation/Translator.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,9 @@ public function __construct(ContainerInterface $container, MessageFormatterInter
8282
$this->resourceFiles = $this->options['resource_files'];
8383
$this->scannedDirectories = $this->options['scanned_directories'];
8484

85-
parent::__construct($defaultLocale, $formatter, $this->options['cache_dir'], $this->options['debug']);
85+
parent::__construct($defaultLocale, $formatter, $this->options['cache_dir'], $this->options['debug'], [
86+
'scanned_directories' => $this->scannedDirectories,
87+
]);
8688
}
8789

8890
/**

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
"symfony/security-http": "~3.4|~4.0",
5050
"symfony/serializer": "^4.3",
5151
"symfony/stopwatch": "~3.4|~4.0",
52-
"symfony/translation": "~4.3",
52+
"symfony/translation": "^4.3.6",
5353
"symfony/templating": "~3.4|~4.0",
5454
"symfony/twig-bundle": "~2.8|~3.2|~4.0",
5555
"symfony/validator": "^4.1",
@@ -77,7 +77,7 @@
7777
"symfony/property-info": "<3.4",
7878
"symfony/serializer": "<4.2",
7979
"symfony/stopwatch": "<3.4",
80-
"symfony/translation": "<4.3",
80+
"symfony/translation": "<4.3.6",
8181
"symfony/twig-bridge": "<4.1.1",
8282
"symfony/validator": "<4.1",
8383
"symfony/workflow": "<4.3.6"

0 commit comments

Comments
 (0)