diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Translation/TranslatorTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Translation/TranslatorTest.php index ad2462abfb..f5b0ab7181 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Translation/TranslatorTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Translation/TranslatorTest.php @@ -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); diff --git a/src/Symfony/Bundle/FrameworkBundle/Translation/Translator.php b/src/Symfony/Bundle/FrameworkBundle/Translation/Translator.php index 44771218d0..4d16bcff95 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Translation/Translator.php +++ b/src/Symfony/Bundle/FrameworkBundle/Translation/Translator.php @@ -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(); }