Skip to content
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

[Translation] Add resources from fallback locale to parent catalogue #17596

Merged
merged 1 commit into from Jan 31, 2016
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/Symfony/Component/Translation/Tests/TranslatorTest.php
Expand Up @@ -239,6 +239,30 @@ public function testWhenAResourceHasNoRegisteredLoader()
$translator->trans('foo');
}

public function testFallbackCatalogueResources()
{
$translator = new Translator('en_GB', new MessageSelector());
$translator->addLoader('yml', new \Symfony\Component\Translation\Loader\YamlFileLoader());
$translator->addResource('yml', __DIR__.'/fixtures/empty.yml', 'en_GB');
$translator->addResource('yml', __DIR__.'/fixtures/resources.yml', 'en');

// force catalogue loading
$this->assertEquals('bar', $translator->trans('foo', array()));

$cataloguesProperty = new \ReflectionProperty($translator, 'catalogues');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can get catalogue from the translator without using reflection, $translator->getCatalogue('en')

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not in branch 2.3.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep 👍

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

simplified in ccc6d31 for 2.7.

$cataloguesProperty->setAccessible(true);
$catalogues = $cataloguesProperty->getValue($translator);

$resources = $catalogues['en']->getResources();
$this->assertCount(1, $resources);
$this->assertContains( __DIR__.'/fixtures/resources.yml', $resources);

$resources = $catalogues['en_GB']->getResources();
$this->assertCount(2, $resources);
$this->assertContains( __DIR__.'/fixtures/empty.yml', $resources);
$this->assertContains( __DIR__.'/fixtures/resources.yml', $resources);
}

/**
* @dataProvider getTransTests
*/
Expand Down
3 changes: 3 additions & 0 deletions src/Symfony/Component/Translation/Translator.php
Expand Up @@ -255,6 +255,9 @@ private function loadFallbackCatalogues($locale)
}

$fallbackCatalogue = new MessageCatalogue($fallback, $this->catalogues[$fallback]->all());
foreach ($this->catalogues[$fallback]->getResources() as $resource) {
$fallbackCatalogue->addResource($resource);
}
$current->addFallbackCatalogue($fallbackCatalogue);
$current = $fallbackCatalogue;
}
Expand Down