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

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[FrameworkBundle][Translation] Invalidate cached catalogues when the …
…scanned directories change
  • Loading branch information
fancyweb committed Oct 26, 2019
commit 6cbee0944c6bbf5be0ed46d8546ef3b7d80850ac
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
foo: bar
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
]);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions 4 src/Symfony/Bundle/FrameworkBundle/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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"
Expand Down
16 changes: 16 additions & 0 deletions 16 src/Symfony/Component/Translation/Tests/TranslatorCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
9 changes: 6 additions & 3 deletions 9 src/Symfony/Component/Translation/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ class Translator implements LegacyTranslatorInterface, TranslatorInterface, Tran
*/
private $debug;

private $cacheVary;

/**
* @var ConfigCacheFactoryInterface|null
*/
Expand All @@ -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 = [])
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was looking at this again an I'm not sure if we can add a new constructor arg on 4.3?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As long as it's added with a default value at the end, I think yes.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although it would look like a new feature.

{
$this->setLocale($locale);

Expand All @@ -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;
}

Expand Down Expand Up @@ -176,7 +179,7 @@ public function setFallbackLocales(array $locales)
$this->assertValidLocale($locale);
}

$this->fallbackLocales = $locales;
$this->fallbackLocales = $this->cacheVary['fallback_locales'] = $locales;
}

/**
Expand Down Expand Up @@ -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';
}

/**
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.