merged branch hason/translationrequest (PR #4650)

Commits
-------

8ae0fa2 [FrameworkBundle] Fixed locale detection from request

Discussion
----------

[FrameworkBundle] Fixed locale detection from request

---------------------------------------------------------------------------

by travisbot at 2012-06-25T10:09:24Z

This pull request [passes](http://travis-ci.org/symfony/symfony/builds/1699743) (merged 8ae0fa21 into 03c8d4d2).
This commit is contained in:
Fabien Potencier 2012-06-28 16:03:12 +02:00
commit 84e619c016
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->isScopeActive('request')) {
if (null === $this->locale && $this->container->isScopeActive('request') && $this->container->has('request')) {
$this->locale = $this->container->get('request')->getLocale();
}