[FrameworkBundle] Fixed locale detection from request

This commit is contained in:
Martin Hasoň 2012-06-25 11:49:17 +02:00
parent 03c8d4d2b0
commit 8ae0fa2178
2 changed files with 41 additions and 1 deletions

View File

@ -80,6 +80,46 @@ class TranslatorTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('no translation', $translator->trans('no translation'));
}
public function testGetLocale()
{
$request = $this->getMock('Symfony\Component\HttpFoundation\Request');
$request
->expects($this->once())
->method('getLocale')
->will($this->returnValue('en'))
;
$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
$container
->expects($this->exactly(2))
->method('isScopeActive')
->with('request')
->will($this->onConsecutiveCalls(false, true))
;
$container
->expects($this->once())
->method('has')
->with('request')
->will($this->returnValue(true))
;
$container
->expects($this->once())
->method('get')
->with('request')
->will($this->returnValue($request))
;
$translator = new Translator($container, new MessageSelector());
$this->assertNull($translator->getLocale());
$this->assertSame('en', $translator->getLocale());
$this->assertSame('en', $translator->getLocale());
}
protected function getCatalogue($locale, $messages)
{
$catalogue = new MessageCatalogue($locale);

View File

@ -65,7 +65,7 @@ class Translator extends BaseTranslator
*/
public function getLocale()
{
if (null === $this->locale && $this->container->has('request')) {
if (null === $this->locale && $this->container->isScopeActive('request') && $this->container->has('request')) {
$this->locale = $this->container->get('request')->getLocale();
}