[Translator] Cache does not take fallback locales into consideration
As we're dumping entire catalogues including their fallbacks (standalone or inlined in ~2.7), we need to use different cache files for different sets of fallback locales.
This commit is contained in:
parent
e140c7f3bd
commit
0f84f3adfa
@ -12,6 +12,7 @@
|
|||||||
namespace Symfony\Bundle\FrameworkBundle\Tests\Translation;
|
namespace Symfony\Bundle\FrameworkBundle\Tests\Translation;
|
||||||
|
|
||||||
use Symfony\Bundle\FrameworkBundle\Translation\Translator;
|
use Symfony\Bundle\FrameworkBundle\Translation\Translator;
|
||||||
|
use Symfony\Component\Translation\Loader\ArrayLoader;
|
||||||
use Symfony\Component\Translation\MessageCatalogue;
|
use Symfony\Component\Translation\MessageCatalogue;
|
||||||
use Symfony\Component\Filesystem\Filesystem;
|
use Symfony\Component\Filesystem\Filesystem;
|
||||||
use Symfony\Component\Translation\MessageSelector;
|
use Symfony\Component\Translation\MessageSelector;
|
||||||
@ -183,6 +184,34 @@ class TranslatorTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertSame('en-US', $translator->getLocale());
|
$this->assertSame('en-US', $translator->getLocale());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testDifferentCacheFilesAreUsedForDifferentSetsOfFallbackLocales()
|
||||||
|
{
|
||||||
|
/*
|
||||||
|
* Because the cache file contains a catalogue including all of its fallback
|
||||||
|
* catalogues, we must take the active set of fallback locales into
|
||||||
|
* consideration when loading a catalogue from the cache.
|
||||||
|
*/
|
||||||
|
$translator = $this->createTranslator(new ArrayLoader(), array('cache_dir' => $this->tmpDir));
|
||||||
|
$translator->setLocale('a');
|
||||||
|
$translator->setFallbackLocales(array('b'));
|
||||||
|
$translator->addResource('loader', array('foo' => 'foo (a)'), 'a');
|
||||||
|
$translator->addResource('loader', 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 = $this->createTranslator(new ArrayLoader(), array('cache_dir' => $this->tmpDir));
|
||||||
|
$translator->setLocale('a');
|
||||||
|
$translator->addResource('loader', array('foo' => 'foo (a)'), 'a');
|
||||||
|
$translator->addResource('loader', 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);
|
||||||
@ -265,12 +294,7 @@ class TranslatorTest extends \PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
public function getTranslator($loader, $options = array(), $translatorClass = '\Symfony\Bundle\FrameworkBundle\Translation\Translator')
|
public function getTranslator($loader, $options = array(), $translatorClass = '\Symfony\Bundle\FrameworkBundle\Translation\Translator')
|
||||||
{
|
{
|
||||||
$translator = new $translatorClass(
|
$translator = $this->createTranslator($loader, $options, $translatorClass);
|
||||||
$this->getContainer($loader),
|
|
||||||
new MessageSelector(),
|
|
||||||
array('loader' => array('loader')),
|
|
||||||
$options
|
|
||||||
);
|
|
||||||
|
|
||||||
$translator->addResource('loader', 'foo', 'fr');
|
$translator->addResource('loader', 'foo', 'fr');
|
||||||
$translator->addResource('loader', 'foo', 'en');
|
$translator->addResource('loader', 'foo', 'en');
|
||||||
@ -282,6 +306,18 @@ class TranslatorTest extends \PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
return $translator;
|
return $translator;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function createTranslator($loader, $options, $translatorClass = '\Symfony\Bundle\FrameworkBundle\Translation\Translator')
|
||||||
|
{
|
||||||
|
$translator = new $translatorClass(
|
||||||
|
$this->getContainer($loader),
|
||||||
|
new MessageSelector(),
|
||||||
|
array('loader' => array('loader')),
|
||||||
|
$options
|
||||||
|
);
|
||||||
|
|
||||||
|
return $translator;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class TranslatorWithInvalidLocale extends Translator
|
class TranslatorWithInvalidLocale extends Translator
|
||||||
|
@ -95,7 +95,7 @@ class Translator extends BaseTranslator
|
|||||||
|
|
||||||
$this->assertValidLocale($locale);
|
$this->assertValidLocale($locale);
|
||||||
|
|
||||||
$cache = new ConfigCache($this->options['cache_dir'].'/catalogue.'.$locale.'.php', $this->options['debug']);
|
$cache = new ConfigCache($this->getCatalogueCachePath($locale), $this->options['debug']);
|
||||||
if (!$cache->isFresh()) {
|
if (!$cache->isFresh()) {
|
||||||
$this->initialize();
|
$this->initialize();
|
||||||
|
|
||||||
@ -157,4 +157,9 @@ EOF
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function getCatalogueCachePath($locale)
|
||||||
|
{
|
||||||
|
return $this->options['cache_dir'].'/catalogue.'.$locale.'.'.sha1(serialize($this->getFallbackLocales())).'.php';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user