Skip to content

[Translation][cache fallback] keep only missing messages in fallbackCatalogue. #14318

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 2 commits 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
31 changes: 31 additions & 0 deletions src/Symfony/Component/Translation/Tests/TranslatorCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,37 @@ public function testDifferentCacheFilesAreUsedForDifferentSetsOfFallbackLocales(
$this->assertEquals('bar', $translator->trans('bar'));
}

public function testGetCatalogueBehavesConsistently()
{
/*
* Create a translator that loads two catalogues for two different locales.
* The catalogues contain distinct sets of messages.
*/
$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');

$catalogue = $translator->getCatalogue('a');
$this->assertFalse($catalogue->defines('bar')); // Sure, the "a" catalogue does not contain that message.

/*
* Now, repeat the same test.
* Behind the scenes, the cache is used. But that should not matter, right?
*/
$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');

$catalogue = $translator->getCatalogue('a');
$this->assertFalse($catalogue->defines('bar'));
}

protected function getCatalogue($locale, $messages)
{
$catalogue = new MessageCatalogue($locale);
Expand Down
30 changes: 14 additions & 16 deletions src/Symfony/Component/Translation/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Symfony\Component\Config\ConfigCacheInterface;
use Symfony\Component\Config\ConfigCacheFactoryInterface;
use Symfony\Component\Config\ConfigCacheFactory;
use Symfony\Component\Translation\Catalogue\DiffOperation;

/**
* Translator.
Expand Down Expand Up @@ -420,21 +421,6 @@ public function dumpCatalogue($locale, ConfigCacheInterface $cache)

private function getFallbackContent(MessageCatalogue $catalogue)
{
if (!$this->debug) {
// merge all fallback catalogues messages into $catalogue
$fallbackCatalogue = $catalogue->getFallbackCatalogue();
$messages = $catalogue->all();
while ($fallbackCatalogue) {
$messages = array_replace_recursive($fallbackCatalogue->all(), $messages);
$fallbackCatalogue = $fallbackCatalogue->getFallbackCatalogue();
}
foreach ($messages as $domain => $domainMessages) {
$catalogue->add($domainMessages, $domain);
}

return '';
}

$fallbackContent = '';
$current = '';
$replacementPattern = '/[^a-z0-9_]/i';
Expand All @@ -444,6 +430,18 @@ private function getFallbackContent(MessageCatalogue $catalogue)
$fallbackSuffix = ucfirst(preg_replace($replacementPattern, '_', $fallback));
$currentSuffix = ucfirst(preg_replace($replacementPattern, '_', $current));

if (!$this->debug) {
// keep only missing messages.
$currentCatalogue = new MessageCatalogue($fallbackCatalogue->getLocale(), $catalogue->all());
$operation = new DiffOperation($fallbackCatalogue, $currentCatalogue);
$fallbackMessages = array();
foreach ($operation->getDomains() as $domain) {
$fallbackMessages[$domain] = $operation->getObsoleteMessages($domain);
}
} else {
$fallbackMessages = $fallbackCatalogue->all();
}

$fallbackContent .= sprintf(<<<EOF
\$catalogue%s = new MessageCatalogue('%s', %s);
\$catalogue%s->addFallbackCatalogue(\$catalogue%s);
Expand All @@ -452,7 +450,7 @@ private function getFallbackContent(MessageCatalogue $catalogue)
,
$fallbackSuffix,
$fallback,
var_export($fallbackCatalogue->all(), true),
var_export($fallbackMessages, true),
$currentSuffix,
$fallbackSuffix
);
Expand Down