8000 bug #14268 [Translator] Cache does not take fallback locales into con… · symfony/symfony@986e3d1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 986e3d1

Browse files
committed
bug #14268 [Translator] Cache does not take fallback locales into consideration (sf2.3) (mpdude)
This PR was merged into the 2.3 branch. Discussion ---------- [Translator] Cache does not take fallback locales into consideration (sf2.3) | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #14268 | License | MIT | Doc PR | n/a This is the Symfony 2.3 variant of #14267. Commits ------- 0f84f3a [Translator] Cache does not take fallback locales into consideration
2 parents e140c7f + 0f84f3a commit 986e3d1

File tree

2 files changed

+48
-7
lines changed

2 files chang 10000 ed

+48
-7
lines changed

src/Symfony/Bundle/FrameworkBundle/Tests/Translation/TranslatorTest.php

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Bundle\FrameworkBundle\Tests\Translation;
1313

1414
use Symfony\Bundle\FrameworkBundle\Translation\Translator;
15+
use Symfony\Component\Translation\Loader\ArrayLoader;
1516
use Symfony\Component\Translation\MessageCatalogue;
1617
use Symfony\Component\Filesystem\Filesystem;
1718
use Symfony\Component\Translation\MessageSelector;
@@ -183,6 +184,34 @@ public function testGetLocaleWithInvalidLocale()
183184
$this->assertSame('en-US', $translator->getLocale());
184185
}
185186

187+
public function testDifferentCacheFilesAreUsedForDifferentSetsOfFallbackLocales()
188+
{
189+
/*
190+
* Because the cache file contains a catalogue including all of its fallback
191+
* catalogues, we must take the active set of fallback locales into
192+
* consideration when loading a catalogue from the cache.
193+
*/
194+
$translator = $this->createTranslator(new ArrayLoader(), array('cache_dir' => $this->tmpDir));
195+
$translator->setLocale('a');
196+
$translator->setFallbackLocales(array('b'));
197+
$translator->addResource('loader', array('foo' => 'foo (a)'), 'a');
198+
$translator->addResource('loader', array('bar' => 'bar (b)'), 'b');
199+
200+
$this->assertEquals('bar (b)', $translator->trans('bar'));
201+
202+
// Remove fallback locale
203+
$translator->setFallbackLocales(array());
204+
$this->assertEquals('bar', $translator->trans('bar'));
205+
206+
// Use a fresh translator with no fallback locales, result should be the same
207+
$translator = $this->createTranslator(new ArrayLoader(), array('cache_dir' => $this->tmpDir));
208+
$translator->setLocale('a');
209+
$translator->addResource('loader', array('foo' => 'foo (a)'), 'a');
210+
$translator->addResource('loader', array('bar' => 'bar (b)'), 'b');
211+
212+
$this->assertEquals('bar', $translator->trans('bar'));
213+
}
214+
186215
protected function getCatalogue($locale, $messages)
187216
{
188217
$catalogue = new MessageCatalogue($locale);
@@ -265,12 +294,7 @@ protected function getContainer($loader)
265294

266295
public function getTranslator($loader, $options = array(), $translatorClass = '\Symfony\Bundle\FrameworkBundle\Translation\Translator')
267296
{
268-
$translator = new $translatorClass(
269-
$this->getContainer($loader),
270-
new MessageSelector(),
271-
array('loader' => array('loader')),
272-
$options
273-
);
297+
$translator = $this->createTranslator($loader, $options, $translatorClass);
274298

275299
$translator->addResource('loader', 'foo', 'fr');
276300
$translator->addResource('loader', 'foo', 'en');
@@ -282,6 +306,18 @@ public function getTranslator($loader, $options = array(), $translatorClass = '\
282306

283307
return $translator;
284308
}
309+
310+
private function createTranslator($loader, $options, $translatorClass = '\Symfony\Bundle\FrameworkBundle\Translation\Translator')
311+
{
312+
$translator = new $translatorClass(
313+
$this->getContainer($loader),
314+
new MessageSelector(),
315+
array('loader' => array('loader')),
316+
$options
317+
);
318+
319+
return $translator;
320+
}
285321
}
286322

287323
class TranslatorWithInvalidLocale extends Translator

src/Symfony/Bundle/FrameworkBundle/Translation/Translator.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ protected function loadCatalogue($locale)
9595

9696
$this->assertValidLocale($locale);
9797

98-
$cache = new ConfigCache($this->options['cache_dir'].'/catalogue.'.$locale.'.php', $this->options['debug']);
98+
$cache = new ConfigCache($this->getCatalogueCachePath($locale), $this->options['debug']);
9999
if (!$cache->isFresh()) {
100100
$this->initialize();
101101

@@ -157,4 +157,9 @@ protected function initialize()
157157
}
158158
}
159159
}
160+
161+
private function getCatalogueCachePath($locale)
162+
{
163+
return $this->options['cache_dir'].'/catalogue.'.$locale.'.'.sha1(serialize($this->getFallbackLocales())).'.php';
164+
}
160165
}

0 commit comments

Comments
 (0)
0