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

Commit 1113999

Browse filesBrowse files
committed
Refresh catalogues when resources change
1 parent 3d174a4 commit 1113999
Copy full SHA for 1113999

File tree

Expand file treeCollapse file tree

2 files changed

+65
-4
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+65
-4
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Translation/Tests/TranslatorCacheTest.php
+35Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,41 @@ public function testTransWithCaching()
101101
$this->assertEquals('foobarbax (sr@latin)', $translator->trans('foobarbax'));
102102
}
103103

104+
public function testRefreshCacheWhenResourcesChange()
105+
{
106+
// prime the cache
107+
$loader = $this->getMock('Symfony\Component\Translation\Loader\LoaderInterface');
108+
$loader
109+
->method('load')
110+
->will($this->returnValue($this->getCatalogue('fr', array(
111+
'foo' => 'foo A',
112+
))))
113+
;
114+
115+
$translator = new Translator('fr', new MessageSelector(), $this->tmpDir, true);
116+
$translator->setLocale('fr');
117+
$translator->addLoader('loader', $loader);
118+
$translator->addResource('loader', 'foo', 'fr');
119+
120+
$this->assertEquals('foo A', $translator->trans('foo'));
121+
122+
// add a new resource to refresh the cache
123+
$loader = $this->getMock('Symfony\Component\Translation\Loader\LoaderInterface');
124+
$loader
125+
->method('load')
126+
->will($this->returnValue($this->getCatalogue('fr', array(
127+
'foo' => 'foo B',
128+
))))
129+
;
130+
131+
$translator = new Translator('fr', new MessageSelector(), $this->tmpDir, true);
132+
$translator->setLocale('fr');
133+
$translator->addLoader('loader', $loader);
134+
$translator->addResource('loader', 'bar', 'fr');
135+
136+
$this->assertEquals('foo B', $translator->trans('foo'));
137+
}
138+
104139
public function testTransWithCachingWithInvalidLocale()
105140
{
106141
$loader = $this->getMock('Symfony\Component\Translation\Loader\LoaderInterface');

‎src/Symfony/Component/Translation/Translator.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Translation/Translator.php
+30-4Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -346,8 +346,9 @@ protected function initializeCatalogue($locale)
346346

347347
/**
348348
* @param string $locale
349+
* @param bool $forceRefresh
349350
*/
350-
private function initializeCacheCatalogue($locale)
351+
private function initializeCacheCatalogue($locale, $forceRefresh = false)
351352
{
352353
if (isset($this->catalogues[$locale])) {
353354
return;
@@ -361,7 +362,7 @@ private function initializeCacheCatalogue($locale)
361362

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

367368
$fallbackContent = '';
@@ -392,13 +393,15 @@ private function initializeCacheCatalogue($locale)
392393
393394
use Symfony\Component\Translation\MessageCatalogue;
394395
396+
\$resourcesHash = '%s';
395397
\$catalogue = new MessageCatalogue('%s', %s);
396398
397399
%s
398-
return \$catalogue;
400+
return array(\$catalogue, \$resourcesHash);
399401
400402
EOF
401403
,
404+
$this->getResourcesHash($locale),
402405
$locale,
403406
var_export($this->catalogues[$locale]->all(), true),
404407
$fallbackContent
@@ -409,7 +412,30 @@ private function initializeCacheCatalogue($locale)
409412
return;
410413
}
411414

412-
$this->catalogues[$locale] = include $cache;
415+
$catalogue = include $cache;
416+
417+
/**
418+
* Old cache returns only the catalogue, without resourcesHash
419+
*/
420+
$resourcesHash = null;
421+
if (is_array($catalogue)) {
422+
list($catalogue, $resourcesHash) = $catalogue;
423+
}
424+
425+
if ($this->debug && $resourcesHash !== $this->getResourcesHash($locale)) {
426+
return $this->initializeCacheCatalogue($locale, true);
427+
}
428+
429+
$this->catalogues[$locale] = $catalogue;
430+
}
431+
432+
private function getResourcesHash($locale)
433+
{
434+
if (!isset($this->resources[$locale])) {
435+
return '';
436+
}
437+
438+
return sha1(serialize($this->resources[$locale]));
413439
}
414440

415441
private function doLoadCatalogue($locale)

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.