Skip to content

[Translation] Refresh catalogues when resources change #13074

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
Feb 5, 2015
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
35 changes: 35 additions & 0 deletions src/Symfony/Component/Translation/Tests/TranslatorCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,41 @@ public function testTransWithCaching()
$this->assertEquals('foobarbax (sr@latin)', $translator->trans('foobarbax'));
}

public function testRefreshCacheWhenResourcesChange()
{
// prime the cache
$loader = $this->getMock('Symfony\Component\Translation\Loader\LoaderInterface');
$loader
->method('load')
->will($this->returnValue($this->getCatalogue('fr', array(
'foo' => 'foo A',
))))
;

$translator = new Translator('fr', new MessageSelector(), $this->tmpDir, true);
$translator->setLocale('fr');
$translator->addLoader('loader', $loader);
$translator->addResource('loader', 'foo', 'fr');

$this->assertEquals('foo A', $translator->trans('foo'));

// add a new resource to refresh the cache
$loader = $this->getMock('Symfony\Component\Translation\Loader\LoaderInterface');
$loader
->method('load')
->will($this->returnValue($this->getCatalogue('fr', array(
'foo' => 'foo B',
))))
;

$translator = new Translator('fr', new MessageSelector(), $this->tmpDir, true);
$translator->setLocale('fr');
$translator->addLoader('loader', $loader);
$translator->addResource('loader', 'bar', 'fr');

$this->assertEquals('foo B', $translator->trans('foo'));
}

public function testTransWithCachingWithInvalidLocale()
{
$loader = $this->getMock('Symfony\Component\Translation\Loader\LoaderInterface');
Expand Down
34 changes: 30 additions & 4 deletions src/Symfony/Component/Translation/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -346,8 +346,9 @@ protected function initializeCatalogue($locale)

/**
* @param string $locale
* @param bool $forceRefresh
*/
private function initializeCacheCatalogue($locale)
private function initializeCacheCatalogue($locale, $forceRefresh = false)
{
if (isset($this->catalogues[$locale])) {
return;
Expand All @@ -361,7 +362,7 @@ private function initializeCacheCatalogue($locale)

$this->assertValidLocale($locale);
$cache = new ConfigCache($this->cacheDir.'/catalogue.'.$locale.'.php', $this->debug);
if (!$cache->isFresh()) {
if ($forceRefresh || !$cache->isFresh()) {
$this->initializeCatalogue($locale);

$fallbackContent = '';
Expand Down Expand Up @@ -392,13 +393,15 @@ private function initializeCacheCatalogue($locale)

use Symfony\Component\Translation\MessageCatalogue;

\$resourcesHash = '%s';
\$catalogue = new MessageCatalogue('%s', %s);

%s
return \$catalogue;
return array(\$catalogue, \$resourcesHash);

EOF
,
$this->getResourcesHash($locale),
$locale,
var_export($this->catalogues[$locale]->all(), true),
$fallbackContent
Expand All @@ -409,7 +412,30 @@ private function initializeCacheCatalogue($locale)
return;
}

$this->catalogues[$locale] = include $cache;
$catalogue = include $cache;

/**
* Old cache returns only the catalogue, without resourcesHash
*/
$resourcesHash = null;
if (is_array($catalogue)) {
list($catalogue, $resourcesHash) = $catalogue;
}

if ($this->debug && $resourcesHash !== $this->getResourcesHash($locale)) {
return $this->initializeCacheCatalogue($locale, true);
}

$this->catalogues[$locale] = $catalogue;
}

private function getResourcesHash($locale)
{
if (!isset($this->resources[$locale])) {
return '';
}

return sha1(serialize($this->resources[$locale]));
}

private function doLoadCatalogue($locale)
Expand Down