diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Translation/TranslatorTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Translation/TranslatorTest.php index 9f2012816a682..a93dd5e845144 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Translation/TranslatorTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Translation/TranslatorTest.php @@ -92,6 +92,16 @@ public function testTransWithCaching() $this->assertEquals('foobarbax (sr@latin)', $translator->trans('foobarbax')); } + public function testTransWithCachingWithInvalidLocale() + { + $loader = $this->getMock('Symfony\Component\Translation\Loader\LoaderInterface'); + $translator = $this->getTranslator($loader, array('cache_dir' => $this->tmpDir), '\Symfony\Bundle\FrameworkBundle\Tests\Translation\TranslatorWithInvalidLocale'); + $translator->setLocale('invalid locale'); + + $this->setExpectedException('\InvalidArgumentException'); + $translator->trans('foo'); + } + public function testGetLocale() { $request = $this->getMock('Symfony\Component\HttpFoundation\Request'); @@ -131,6 +141,49 @@ public function testGetLocale() $this->assertSame('en', $translator->getLocale()); } + public function testGetLocaleWithInvalidLocale() + { + $request = $this->getMock('Symfony\Component\HttpFoundation\Request'); + + $request + ->expects($this->once()) + ->method('getLocale') + ->will($this->returnValue('foo bar')) + ; + $request + ->expects($this->once()) + ->method('getDefaultLocale') + ->will($this->returnValue('en-US')) + ; + + $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface'); + + $container + ->expects($this->once()) + ->method('isScopeActive') + ->with('request') + ->will($this->returnValue(true)) + ; + + $container + ->expects($this->once()) + ->method('has') + ->with('request') + ->will($this->returnValue(true)) + ; + + $container + ->expects($this->any()) + ->method('get') + ->with('request') + ->will($this->returnValue($request)) + ; + + $translator = new Translator($container, new MessageSelector()); + $this->assertSame('en-US', $translator->getLocale()); + } + + protected function getCatalogue($locale, $messages) { $catalogue = new MessageCatalogue($locale); @@ -211,9 +264,9 @@ protected function getContainer($loader) return $container; } - public function getTranslator($loader, $options = array()) + public function getTranslator($loader, $options = array(), $translatorClass = '\Symfony\Bundle\FrameworkBundle\Translation\Translator') { - $translator = new Translator( + $translator = new $translatorClass( $this->getContainer($loader), new MessageSelector(), array('loader' => array('loader')), @@ -231,3 +284,14 @@ public function getTranslator($loader, $options = array()) return $translator; } } + +class TranslatorWithInvalidLocale extends Translator +{ + /** + * {@inheritdoc} + */ + public function setLocale($locale) + { + $this->locale = $locale; + } +} diff --git a/src/Symfony/Bundle/FrameworkBundle/Translation/Translator.php b/src/Symfony/Bundle/FrameworkBundle/Translation/Translator.php index 0f99f6428e84e..92f90d4ff5c2b 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Translation/Translator.php +++ b/src/Symfony/Bundle/FrameworkBundle/Translation/Translator.php @@ -68,7 +68,11 @@ public function __construct(ContainerInterface $container, MessageSelector $sele public function getLocale() { if (null === $this->locale && $this->container->isScopeActive('request') && $this->container->has('request')) { - $this->locale = $this->container->get('request')->getLocale(); + try { + $this->setLocale($this->container->get('request')->getLocale()); + } catch (\InvalidArgumentException $e) { + $this->setLocale($this->container->get('request')->getDefaultLocale()); + } } return $this->locale; @@ -89,6 +93,8 @@ protected function loadCatalogue($locale) return parent::loadCatalogue($locale); } + $this->assertValidLocale($locale); + $cache = new ConfigCache($this->options['cache_dir'].'/catalogue.'.$locale.'.php', $this->options['debug']); if (!$cache->isFresh()) { $this->initialize(); diff --git a/src/Symfony/Component/HttpFoundation/Request.php b/src/Symfony/Component/HttpFoundation/Request.php index f50219deae8cf..f180b3e6197f5 100644 --- a/src/Symfony/Component/HttpFoundation/Request.php +++ b/src/Symfony/Component/HttpFoundation/Request.php @@ -1358,6 +1358,16 @@ public function setDefaultLocale($locale) } } + /** + * Get the default locale. + * + * @return string + */ + public function getDefaultLocale() + { + return $this->defaultLocale; + } + /** * Sets the locale. * diff --git a/src/Symfony/Component/Translation/Translator.php b/src/Symfony/Component/Translation/Translator.php index 0ed30526d2b43..88178f8a83f17 100644 --- a/src/Symfony/Component/Translation/Translator.php +++ b/src/Symfony/Component/Translation/Translator.php @@ -306,7 +306,7 @@ protected function computeFallbackLocales($locale) * * @throws \InvalidArgumentException If the locale contains invalid characters */ - private function assertValidLocale($locale) + protected function assertValidLocale($locale) { if (1 !== preg_match('/^[a-z0-9@_\\.\\-]*$/i', $locale)) { throw new \InvalidArgumentException(sprintf('Invalid "%s" locale.', $locale));