8000 bug #17596 [Translation] Add resources from fallback locale to parent… · lukaszmakuch/symfony@27de563 · GitHub
[go: up one dir, main page]

Skip to content

Commit 27de563

Browse files
committed
bug symfony#17596 [Translation] Add resources from fallback locale to parent catalogue (c960657)
This PR was merged into the 2.3 branch. Discussion ---------- [Translation] Add resources from fallback locale to parent catalogue | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - The resources representing a locale includes those of the fallback locale. However, `getCatalogue()->getResources()` only returns the resources belonging specifically to the selected locale. Example: The locale `en_GB` falls back to `en`. I use the locale `en_GB`. During development, when I modify the `en_GB` translation file, the changes appear instantly when reloading the page. If I modify the `en` translation file, I need to manually clear the cache in order for the new translation to appear. I believe this is a regression that was introduced in symfony#15527. This patch is for the 2.3 branch. For 2.6 and later, the test can be updated to use the getCatalogue() method instead of using ReflectionProperty. Commits ------- f7f82fa [Translation] Add resources from fallback locale
2 parents 9cdb207 + f7f82fa commit 27de563

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,30 @@ public function testWhenAResourceHasNoRegisteredLoader()
239239
$translator->trans('foo');
240240
}
241241

242+
public function testFallbackCatalogueResources()
243+
{
244+
$translator = new Translator('en_GB', new MessageSelector());
245+
$translator->addLoader('yml', new \Symfony\Component\Translation\Loader\YamlFileLoader());
246+
$translator->addResource('yml', __DIR__.'/fixtures/empty.yml', 'en_GB');
247+
$translator->addResource('yml', __DIR__.'/fixtures/resources.yml', 'en');
248+
249+
// force catalogue loading
250+
$this->assertEquals('bar', $translator->trans('foo', array()));
251+
252+
$cataloguesProperty = new \ReflectionProperty($translator, 'catalogues');
253+
$cataloguesProperty->setAccessible(true);
254+
$catalogues = $cataloguesProperty->getValue($translator);
255+
256+
$resources = $catalogues['en']->getResources();
257+
$this->assertCount(1, $resources);
258+
$this->assertContains( __DIR__.'/fixtures/resources.yml', $resources);
259+
260+
$resources = $catalogues['en_GB']->getResources();
261+
$this->assertCount(2, $resources);
262+
$this->assertContains( __DIR__.'/fixtures/empty.yml', $resources);
263+
$this->assertContains( __DIR__.'/fixtures/resources.yml', $resources);
264+
}
265+
242266
/**
243267
* @dataProvider getTransTests
244268
*/

src/Symfony/Component/Translation/Translator.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,9 @@ private function loadFallbackCatalogues($locale)
255255
}
256256

257257
$fallbackCatalogue = new MessageCatalogue($fallback, $this->catalogues[$fallback]->all());
258+
foreach ($this->catalogues[$fallback]->getResources() as $resource) {
259+
$fallbackCatalogue->addResource($resource);
260+
}
258261
$current->addFallbackCatalogue($fallbackCatalogue);
259262
$current = $fallbackCatalogue;
260263
}

0 commit comments

Comments
 (0)
0