Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6cbee09

Browse files
committed
[FrameworkBundle][Translation] Invalidate cached catalogues when the scanned directories change
1 parent 6c08ac5 commit 6cbee09

File tree

6 files changed

+64
-6
lines changed

6 files changed

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

src/Symfony/Bundle/FrameworkBundle/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);

src/Symfony/Bundle/FrameworkBundle/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
/**

src/Symfony/Bundle/FrameworkBundle/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"

src/Symfony/Component/Translation/Tests/TranslatorCacheTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,22 @@ public function testRefreshCacheWhenResourcesAreNoLongerFresh()
269269
$translator->trans('foo');
270270
}
271271

272+
public function testCachedCatalogueIsReDumpedWhenCacheVaryChange()
273+
{
274+
$translator = new Translator('a', null, $this->tmpDir, false, []);
275+
$translator->addLoader('array', new ArrayLoader());
276+
$translator->addResource('array', ['foo' => 'bar'], 'a', 'messages');
277+
278+
// Cached catalogue is dumped
279+
$this->assertSame('bar', $translator->trans('foo', [], 'messages', 'a'));
280+
281+
$translator = new Translator('a', null, $this->tmpDir, false, ['vary']);
282+
$translator->addLoader('array', new ArrayLoader());
283+
$translator->addResource('array', ['foo' => 'ccc'], 'a', 'messages');
284+
285+
$this->assertSame('ccc', $translator->trans('foo', [], 'messages', 'a'));
286+
}
287+
272288
protected function getCatalogue($locale, $messages, $resources = [])
273289
{
274290
$catalogue = new MessageCatalogue($locale);

src/Symfony/Component/Translation/Translator.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ class Translator implements LegacyTranslatorInterface, TranslatorInterface, Tran
7171
*/
7272
private $debug;
7373

74+
private $cacheVary;
75+
7476
/**
7577
* @var ConfigCacheFactoryInterface|null
7678
*/
@@ -86,7 +88,7 @@ class Translator implements LegacyTranslatorInterface, TranslatorInterface, Tran
8688
/**
8789
* @throws InvalidArgumentException If a locale contains invalid characters
8890
*/
89-
public function __construct(?string $locale, MessageFormatterInterface $formatter = null, string $cacheDir = null, bool $debug = false)
91+
public function __construct(?string $locale, MessageFormatterInterface $formatter = null, string $cacheDir = null, bool $debug = false, array $cacheVary = [])
9092
{
9193
$this->setLocale($locale);
9294

@@ -97,6 +99,7 @@ public function __construct(?string $locale, MessageFormatterInterface $formatte
9799
$this->formatter = $formatter;
98100
$this->cacheDir = $cacheDir;
99101
$this->debug = $debug;
102+
$this->cacheVary = $cacheVary;
100103
$this->hasIntlFormatter = $formatter instanceof IntlFormatterInterface;
101104
}
102105

@@ -176,7 +179,7 @@ public function setFallbackLocales(array $locales)
176179
$this->assertValidLocale($locale);
177180
}
178181

179-
$this->fallbackLocales = $locales;
182+
$this->fallbackLocales = $this->cacheVary['fallback_locales'] = $locales;
180183
}
181184

182185
/**
@@ -392,7 +395,7 @@ private function getFallbackContent(MessageCatalogue $catalogue): string
392395

393396
private function getCatalogueCachePath($locale)
394397
{
395-
return $this->cacheDir.'/catalogue.'.$locale.'.'.strtr(substr(base64_encode(hash('sha256', serialize($this->fallbackLocales), true)), 0, 7), '/', '_').'.php';
398+
return $this->cacheDir.'/catalogue.'.$locale.'.'.strtr(substr(base64_encode(hash('sha256', serialize($this->cacheVary), true)), 0, 7), '/', '_').'.php';
396399
}
397400

398401
/**

0 commit comments

Comments
 (0)