Skip to content

[Translator] Cache does not take fallback locales into consideration #14267

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

Closed
wants to merge 1 commit into from
Closed
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
32 changes: 32 additions & 0 deletions src/Symfony/Component/Translation/Tests/TranslatorCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -129,6 +130,37 @@ public function testLoadCatalogueWithCachingWithInvalidLocale()
}
}

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);
Expand Down
7 changes: 6 additions & 1 deletion src/Symfony/Component/Translation/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ private function initializeCacheCatalogue($locale)
}

$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);

Expand Down Expand Up @@ -404,6 +404,11 @@ private function initializeCacheCatalogue($locale)
$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);
Expand Down