[Translator] Cache does not take fallback locales into consideration
This commit is contained in:
parent
94eb384163
commit
f666657342
@ -11,6 +11,7 @@
|
|||||||
|
|
||||||
namespace Symfony\Component\Translation\Tests;
|
namespace Symfony\Component\Translation\Tests;
|
||||||
|
|
||||||
|
use Symfony\Component\Translation\Loader\ArrayLoader;
|
||||||
use Symfony\Component\Translation\Translator;
|
use Symfony\Component\Translation\Translator;
|
||||||
use Symfony\Component\Translation\MessageCatalogue;
|
use Symfony\Component\Translation\MessageCatalogue;
|
||||||
use Symfony\Component\Translation\MessageSelector;
|
use Symfony\Component\Translation\MessageSelector;
|
||||||
@ -164,6 +165,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)
|
protected function getCatalogue($locale, $messages)
|
||||||
{
|
{
|
||||||
$catalogue = new MessageCatalogue($locale);
|
$catalogue = new MessageCatalogue($locale);
|
||||||
|
@ -372,7 +372,7 @@ class Translator implements TranslatorInterface, TranslatorBagInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
$this->assertValidLocale($locale);
|
$this->assertValidLocale($locale);
|
||||||
$cacheFile = $this->cacheDir.'/catalogue.'.$locale.'.php';
|
$cacheFile = $this->getCatalogueCachePath($locale);
|
||||||
$self = $this; // required for PHP 5.3 where "$this" cannot be use()d in anonymous functions. Change in Symfony 3.0.
|
$self = $this; // required for PHP 5.3 where "$this" cannot be use()d in anonymous functions. Change in Symfony 3.0.
|
||||||
$cache = $this->getConfigCacheFactory()->cache($cacheFile,
|
$cache = $this->getConfigCacheFactory()->cache($cacheFile,
|
||||||
function (ConfigCacheInterface $cache) use ($self, $locale) {
|
function (ConfigCacheInterface $cache) use ($self, $locale) {
|
||||||
@ -503,6 +503,11 @@ EOF
|
|||||||
return sha1(serialize($this->resources[$locale]));
|
return sha1(serialize($this->resources[$locale]));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function getCatalogueCachePath($locale)
|
||||||
|
{
|
||||||
|
return $this->cacheDir.'/catalogue.'.$locale.'.'.sha1(serialize($this->fallbackLocales)).'.php';
|
||||||
|
}
|
||||||
|
|
||||||
private function doLoadCatalogue($locale)
|
private function doLoadCatalogue($locale)
|
||||||
{
|
{
|
||||||
$this->catalogues[$locale] = new MessageCatalogue($locale);
|
$this->catalogues[$locale] = new MessageCatalogue($locale);
|
||||||
|
Reference in New Issue
Block a user