Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cbffabf

Browse files
committed
bug #14315 [Translation] Revert inlining fallback catalogues as it might cause inconsistent results when a cache is used (mpdude)
This PR was merged into the 2.7 branch. Discussion ---------- [Translation] Revert inlining fallback catalogues as it might cause inconsistent results when a cache is used | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #14315 | License | MIT | Doc PR | n/a The results from `getCatalogue()` are inconsistent when we're in production *and* a cache comes into play. This is due to the changes in 6eb5e73, where the fallback catalogues will be *merged* into the primary catalogue when it is written to the cache. Strictly speaking, this is a BC break because it behaved differently before. I am not sure what the relevance of this might be in practice. However, it may cause headaches because * The result changes only in the second+ try (when the cache is warm); a priori you cannot tell whether you're going to see this * The catalogue is clearly not what the loader provided * You have no chance of telling whether the message was originally available in the catalogue or not * Generally, for every message you retrieve from the catalogue, you have no way of telling which locale it actually comes from (it need not be from the catalogue's locale, and the catalogue does not provide fallback catalogues either) Regarding the last point, you usually don't care when using the `Translator`. Its purpose is to get the "best" translation available. However, when you bother to explicitly retrieve the catalogue, chances are your intentions are different. Commits ------- 12a183a Revert inlining the fallback messages in production that was added in 6eb5e73
2 parents e00f037 + 12a183a commit cbffabf

File tree

2 files changed

+39
-15
lines changed

2 files changed

+39
-15
lines changed

src/Symfony/Component/Translation/Tests/TranslatorCacheTest.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,45 @@ public function testDifferentCacheFilesAreUsedForDifferentSetsOfFallbackLocales(
196196
$this->assertEquals('bar', $translator->trans('bar'));
197197
}
198198

199+
public function testGetCatalogueBehavesConsistently()
200+
{
201+
/*
202+
* Create a translator that loads two catalogues for two different locales.
203+
* The catalogues contain distinct sets of messages.
204+
*/
205+
$translator = new Translator('a', null, $this->tmpDir);
206+
$translator->setFallbackLocales(array('b'));
207+
208+
$translator->addLoader('array', new ArrayLoader());
209+
$translator->addResource('array', array('foo' => 'foo (a)'), 'a');
210+
$translator->addResource('array', array('foo' => 'foo (b)'), 'b');
211+
$translator->addResource('array', array('bar' => 'bar (b)'), 'b');
212+
213+
$catalogue = $translator->getCatalogue('a');
214+
$this->assertFalse($catalogue->defines('bar')); // Sure, the "a" catalogue does not contain that message.
215+
216+
$fallback = $catalogue->getFallbackCatalogue();
217+
$this->assertTrue($fallback->defines('foo')); // "foo" is present in "a" and "b"
218+
219+
/*
220+
* Now, repeat the same test.
221+
* Behind the scenes, the cache is used. But that should not matter, right?
222+
*/
223+
$translator = new Translator('a', null, $this->tmpDir);
224+
$translator->setFallbackLocales(array('b'));
225+
226+
$translator->addLoader('array', new ArrayLoader());
227+
$translator->addResource('array', array('foo' => 'foo (a)'), 'a');
228+
$translator->addResource('array', array('foo' => 'foo (b)'), 'b');
229+
$translator->addResource('array', array('bar' => 'bar (b)'), 'b');
230+
231+
$catalogue = $translator->getCatalogue('a');
232+
$this->assertFalse($catalogue->defines('bar'));
233+
234+
$fallback = $catalogue->getFallbackCatalogue();
235+
$this->assertTrue($fallback->defines('foo'));
236+
}
237+
199238
protected function getCatalogue($locale, $messages)
200239
{
201240
$catalogue = new MessageCatalogue($locale);

src/Symfony/Component/Translation/Translator.php

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -420,21 +420,6 @@ public function dumpCatalogue($locale, ConfigCacheInterface $cache)
420420

421421
private function getFallbackContent(MessageCatalogue $catalogue)
422422
{
423-
if (!$this->debug) {
424-
// merge all fallback catalogues messages into $catalogue
425-
$fallbackCatalogue = $catalogue->getFallbackCatalogue();
426-
$messages = $catalogue->all();
427-
while ($fallbackCatalogue) {
428-
$messages = array_replace_recursive($fallbackCatalogue->all(), $messages);
429-
$fallbackCatalogue = $fallbackCatalogue->getFallbackCatalogue();
430-
}
431-
foreach ($messages as $domain => $domainMessages) {
432-
$catalogue->add($domainMessages, $domain);
433-
}
434-
435-
return '';
436-
}
437-
438423
$fallbackContent = '';
439424
$current = '';
440425
$replacementPattern = '/[^a-z0-9_]/i';

0 commit comments

Comments
 (0)