diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Resources/translations2/ccc.fr.yml b/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Resources/translations2/ccc.fr.yml new file mode 100644 index 0000000000000..20e9ff3feaa8e --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Resources/translations2/ccc.fr.yml @@ -0,0 +1 @@ +foo: bar diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Translation/TranslatorTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Translation/TranslatorTest.php index 26753e8a8a548..cd3f3d9fba858 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Translation/TranslatorTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Translation/TranslatorTest.php @@ -18,6 +18,7 @@ use Symfony\Component\Config\Resource\FileExistenceResource; use Symfony\Component\Filesystem\Filesystem; use Symfony\Component\Translation\Formatter\MessageFormatter; +use Symfony\Component\Translation\Loader\YamlFileLoader; use Symfony\Component\Translation\MessageCatalogue; class TranslatorTest extends TestCase @@ -244,6 +245,41 @@ public function testCatalogResourcesAreAddedForScannedDirectories() $this->assertEquals(new FileExistenceResource('/tmp/I/sure/hope/this/does/not/exist'), $resources[2]); } + public function testCachedCatalogueIsReDumpedWhenScannedDirectoriesChange() + { + /** @var Translator $translator */ + $translator = $this->getTranslator(new YamlFileLoader(), [ + 'cache_dir' => $this->tmpDir, + 'resource_files' => [ + 'fr' => [ + __DIR__.'/../Fixtures/Resources/translations/messages.fr.yml', + ], + ], + 'scanned_directories' => [ + __DIR__.'/../Fixtures/Resources/translations/', + ], + ], 'yml'); + + // Cached catalogue is dumped + $this->assertSame('répertoire', $translator->trans('folder', [], 'messages', 'fr')); + + $translator = $this->getTranslator(new YamlFileLoader(), [ + 'cache_dir' => $this->tmpDir, + 'resource_files' => [ + 'fr' => [ + __DIR__.'/../Fixtures/Resources/translations/messages.fr.yml', + __DIR__.'/../Fixtures/Resources/translations2/ccc.fr.yml', + ], + ], + 'scanned_directories' => [ + __DIR__.'/../Fixtures/Resources/translations/', + __DIR__.'/../Fixtures/Resources/translations2/', + ], + ], 'yml'); + + $this->assertSame('bar', $translator->trans('foo', [], 'ccc', 'fr')); + } + protected function getCatalogue($locale, $messages, $resources = []) { $catalogue = new MessageCatalogue($locale); diff --git a/src/Symfony/Bundle/FrameworkBundle/Translation/Translator.php b/src/Symfony/Bundle/FrameworkBundle/Translation/Translator.php index 511da9fb22c1c..f718c8d0157bc 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Translation/Translator.php +++ b/src/Symfony/Bundle/FrameworkBundle/Translation/Translator.php @@ -82,7 +82,9 @@ public function __construct(ContainerInterface $container, MessageFormatterInter $this->resourceFiles = $this->options['resource_files']; $this->scannedDirectories = $this->options['scanned_directories']; - parent::__construct($defaultLocale, $formatter, $this->options['cache_dir'], $this->options['debug']); + parent::__construct($defaultLocale, $formatter, $this->options['cache_dir'], $this->options['debug'], [ + 'scanned_directories' => $this->scannedDirectories, + ]); } /** diff --git a/src/Symfony/Bundle/FrameworkBundle/composer.json b/src/Symfony/Bundle/FrameworkBundle/composer.json index 57c344f68f22e..55b9e0cbf041c 100644 --- a/src/Symfony/Bundle/FrameworkBundle/composer.json +++ b/src/Symfony/Bundle/FrameworkBundle/composer.json @@ -49,7 +49,7 @@ "symfony/security-http": "~3.4|~4.0", "symfony/serializer": "^4.3", "symfony/stopwatch": "~3.4|~4.0", - "symfony/translation": "~4.3", + "symfony/translation": "^4.3.6", "symfony/templating": "~3.4|~4.0", "symfony/twig-bundle": "~2.8|~3.2|~4.0", "symfony/validator": "^4.1", @@ -77,7 +77,7 @@ "symfony/property-info": "<3.4", "symfony/serializer": "<4.2", "symfony/stopwatch": "<3.4", - "symfony/translation": "<4.3", + "symfony/translation": "<4.3.6", "symfony/twig-bridge": "<4.1.1", "symfony/validator": "<4.1", "symfony/workflow": "<4.3.6" diff --git a/src/Symfony/Component/Translation/Tests/TranslatorCacheTest.php b/src/Symfony/Component/Translation/Tests/TranslatorCacheTest.php index 08add3404b5c7..ae5c75ba82c22 100644 --- a/src/Symfony/Component/Translation/Tests/TranslatorCacheTest.php +++ b/src/Symfony/Component/Translation/Tests/TranslatorCacheTest.php @@ -269,6 +269,22 @@ public function testRefreshCacheWhenResourcesAreNoLongerFresh() $translator->trans('foo'); } + public function testCachedCatalogueIsReDumpedWhenCacheVaryChange() + { + $translator = new Translator('a', null, $this->tmpDir, false, []); + $translator->addLoader('array', new ArrayLoader()); + $translator->addResource('array', ['foo' => 'bar'], 'a', 'messages'); + + // Cached catalogue is dumped + $this->assertSame('bar', $translator->trans('foo', [], 'messages', 'a')); + + $translator = new Translator('a', null, $this->tmpDir, false, ['vary']); + $translator->addLoader('array', new ArrayLoader()); + $translator->addResource('array', ['foo' => 'ccc'], 'a', 'messages'); + + $this->assertSame('ccc', $translator->trans('foo', [], 'messages', 'a')); + } + protected function getCatalogue($locale, $messages, $resources = []) { $catalogue = new MessageCatalogue($locale); diff --git a/src/Symfony/Component/Translation/Translator.php b/src/Symfony/Component/Translation/Translator.php index 9846c8338bb17..d9241d5a82625 100644 --- a/src/Symfony/Component/Translation/Translator.php +++ b/src/Symfony/Component/Translation/Translator.php @@ -71,6 +71,8 @@ class Translator implements LegacyTranslatorInterface, TranslatorInterface, Tran */ private $debug; + private $cacheVary; + /** * @var ConfigCacheFactoryInterface|null */ @@ -86,7 +88,7 @@ class Translator implements LegacyTranslatorInterface, TranslatorInterface, Tran /** * @throws InvalidArgumentException If a locale contains invalid characters */ - public function __construct(?string $locale, MessageFormatterInterface $formatter = null, string $cacheDir = null, bool $debug = false) + public function __construct(?string $locale, MessageFormatterInterface $formatter = null, string $cacheDir = null, bool $debug = false, array $cacheVary = []) { $this->setLocale($locale); @@ -97,6 +99,7 @@ public function __construct(?string $locale, MessageFormatterInterface $formatte $this->formatter = $formatter; $this->cacheDir = $cacheDir; $this->debug = $debug; + $this->cacheVary = $cacheVary; $this->hasIntlFormatter = $formatter instanceof IntlFormatterInterface; } @@ -176,7 +179,7 @@ public function setFallbackLocales(array $locales) $this->assertValidLocale($locale); } - $this->fallbackLocales = $locales; + $this->fallbackLocales = $this->cacheVary['fallback_locales'] = $locales; } /** @@ -392,7 +395,7 @@ private function getFallbackContent(MessageCatalogue $catalogue): string private function getCatalogueCachePath($locale) { - return $this->cacheDir.'/catalogue.'.$locale.'.'.strtr(substr(base64_encode(hash('sha256', serialize($this->fallbackLocales), true)), 0, 7), '/', '_').'.php'; + return $this->cacheDir.'/catalogue.'.$locale.'.'.strtr(substr(base64_encode(hash('sha256', serialize($this->cacheVary), true)), 0, 7), '/', '_').'.php'; } /**