[FrameworkBundle][Translation] Invalidate cached catalogues when the scanned directories change

This commit is contained in:
Thomas Calvet 2019-10-26 12:21:13 +02:00
parent 6c08ac599e
commit 6cbee0944c
6 changed files with 64 additions and 6 deletions

View File

@ -18,6 +18,7 @@ use Symfony\Component\Config\Resource\DirectoryResource;
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 @@ class TranslatorTest extends TestCase
$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);

View File

@ -82,7 +82,9 @@ class Translator extends BaseTranslator implements WarmableInterface
$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,
]);
}
/**

View File

@ -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"

View File

@ -269,6 +269,22 @@ class TranslatorCacheTest extends TestCase
$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);

View File

@ -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 @@ class Translator implements LegacyTranslatorInterface, TranslatorInterface, Tran
$this->formatter = $formatter;
$this->cacheDir = $cacheDir;
$this->debug = $debug;
$this->cacheVary = $cacheVary;
$this->hasIntlFormatter = $formatter instanceof IntlFormatterInterface;
}
@ -176,7 +179,7 @@ class Translator implements LegacyTranslatorInterface, TranslatorInterface, Tran
$this->assertValidLocale($locale);
}
$this->fallbackLocales = $locales;
$this->fallbackLocales = $this->cacheVary['fallback_locales'] = $locales;
}
/**
@ -392,7 +395,7 @@ EOF
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';
}
/**