From f666657342a1b6fcd249ca4cdda115338e9b1602 Mon Sep 17 00:00:00 2001 From: Matthias Pigulla Date: Wed, 8 Apr 2015 14:50:34 +0200 Subject: [PATCH] [Translator] Cache does not take fallback locales into consideration --- .../Translation/Tests/TranslatorCacheTest.php | 32 +++++++++++++++++++ .../Component/Translation/Translator.php | 7 +++- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Translation/Tests/TranslatorCacheTest.php b/src/Symfony/Component/Translation/Tests/TranslatorCacheTest.php index 24514b73dc..fdbb4f48e6 100644 --- a/src/Symfony/Component/Translation/Tests/TranslatorCacheTest.php +++ b/src/Symfony/Component/Translation/Tests/TranslatorCacheTest.php @@ -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; @@ -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) { $catalogue = new MessageCatalogue($locale); diff --git a/src/Symfony/Component/Translation/Translator.php b/src/Symfony/Component/Translation/Translator.php index c0d3629b36..ca9b4054f7 100644 --- a/src/Symfony/Component/Translation/Translator.php +++ b/src/Symfony/Component/Translation/Translator.php @@ -372,7 +372,7 @@ class Translator implements TranslatorInterface, TranslatorBagInterface } $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. $cache = $this->getConfigCacheFactory()->cache($cacheFile, function (ConfigCacheInterface $cache) use ($self, $locale) { @@ -503,6 +503,11 @@ EOF return sha1(serialize($this->resources[$locale])); } + private function getCatalogueCachePath($locale) + { + return $this->cacheDir.'/catalogue.'.$locale.'.'.sha1(serialize($this->fallbackLocales)).'.php'; + } + private function doLoadCatalogue($locale) { $this->catalogues[$locale] = new MessageCatalogue($locale);