Skip to content

[Translator] Cache does not take fallback locales into consideration (sf2.3) #14268

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 8, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Bundle\FrameworkBundle\Tests\Translation;

use Symfony\Bundle\FrameworkBundle\Translation\Translator;
use Symfony\Component\Translation\Loader\ArrayLoader;
use Symfony\Component\Translation\MessageCatalogue;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Translation\MessageSelector;
Expand Down Expand Up @@ -183,6 +184,34 @@ public function testGetLocaleWithInvalidLocale()
$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)
{
$catalogue = new MessageCatalogue($locale);
Expand Down Expand Up @@ -265,12 +294,7 @@ protected function getContainer($loader)

public function getTranslator($loader, $options = array(), $translatorClass = '\Symfony\Bundle\FrameworkBundle\Translation\Translator')
{
$translator = new $translatorClass(
$this->getContainer($loader),
new MessageSelector(),
array('loader' => array('loader')),
$options
);
$translator = $this->createTranslator($loader, $options, $translatorClass);

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

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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ protected function loadCatalogue($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()) {
$this->initialize();

Expand Down Expand Up @@ -157,4 +157,9 @@ protected function initialize()
}
}
}

private function getCatalogueCachePath($locale)
{
return $this->options['cache_dir'].'/catalogue.'.$locale.'.'.sha1(serialize($this->getFallbackLocales())).'.php';
}
}