[Translator] Cache does not take fallback locales into consideration
This commit is contained in:
parent
b66c9347cb
commit
0d55b2f92d
@ -11,6 +11,7 @@
|
||||
|
||||
namespace Symfony\Component\Translation\Tests;
|
||||
|
||||
use Symfony\Component\Translation\Loader\ArrayLoader;
|
||||
use Symfony\Component\Translation\Translator;
|
||||
use Symfony\Component\Translation\MessageCatalogue;
|
||||
use Symfony\Component\Translation\MessageSelector;
|
||||
@ -129,6 +130,37 @@ class TranslatorCacheTest extends \PHPUnit_Framework_TestCase
|
||||
}
|
||||
}
|
||||
|
||||
public function testDifferentCacheFilesAreUsedForDifferentSetsOfFallbackLocales()
|
||||
{
|
||||
/*
|
||||
* Because the cache file contains a catalogue including all of its fallback
|
||||
* catalogues (either "inlined" in Symfony 2.7 production or "standalone"),
|
||||
* we must take the active set of fallback locales into consideration when
|
||||
* loading a catalogue from the cache.
|
||||
*/
|
||||
$translator = new Translator('a', null, $this->tmpDir);
|
||||
$translator->setFallbackLocales(array('b'));
|
||||
|
||||
$translator->addLoader('array', new ArrayLoader());
|
||||
$translator->addResource('array', array('foo' => 'foo (a)'), 'a');
|
||||
$translator->addResource('array', array('bar' => 'bar (b)'), 'b');
|
||||
|
||||
$this->assertEquals('bar (b)', $translator->trans('bar'));
|
||||
|
||||
// Remove fallback locale
|
||||
$translator->setFallbackLocales(array());
|
||||
$this->assertEquals('bar', $translator->trans('bar'));
|
||||
|
||||
// Use a fresh translator with no fallback locales, result should be the same
|
||||
$translator = new Translator('a', null, $this->tmpDir);
|
||||
|
||||
$translator->addLoader('array', new ArrayLoader());
|
||||
$translator->addResource('array', array('foo' => 'foo (a)'), 'a');
|
||||
$translator->addResource('array', array('bar' => 'bar (b)'), 'b');
|
||||
|
||||
$this->assertEquals('bar', $translator->trans('bar'));
|
||||
}
|
||||
|
||||
protected function getCatalogue($locale, $messages)
|
||||
{
|
||||
$catalogue = new MessageCatalogue($locale);
|
||||
|
@ -352,7 +352,7 @@ class Translator implements TranslatorInterface, TranslatorBagInterface
|
||||
}
|
||||
|
||||
$this->assertValidLocale($locale);
|
||||
$cache = new ConfigCache($this->cacheDir.'/catalogue.'.$locale.'.php', $this->debug);
|
||||
$cache = new ConfigCache($this->getCatalogueCachePath($locale), $this->debug);
|
||||
if (!$cache->isFresh()) {
|
||||
$this->initializeCatalogue($locale);
|
||||
|
||||
@ -404,6 +404,11 @@ EOF
|
||||
$this->catalogues[$locale] = include $cache;
|
||||
}
|
||||
|
||||
private function getCatalogueCachePath($locale)
|
||||
{
|
||||
return $this->cacheDir.'/catalogue.'.$locale.'.'.sha1(serialize($this->fallbackLocales)).'.php';
|
||||
}
|
||||
|
||||
private function doLoadCatalogue($locale)
|
||||
{
|
||||
$this->catalogues[$locale] = new MessageCatalogue($locale);
|
||||
|
Reference in New Issue
Block a user