Skip to content

n/a #11830

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

Merged
merged 2 commits into from
Sep 3, 2014
Merged

n/a #11830

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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')),
Expand All @@ -231,3 +284,14 @@ public function getTranslator($loader, $options = array())
return $translator;
}
}

class TranslatorWithInvalidLocale extends Translator
{
/**
* {@inheritdoc}
*/
public function setLocale($locale)
{
$this->locale = $locale;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand Down
10 changes: 10 additions & 0 deletions src/Symfony/Component/HttpFoundation/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -1358,6 +1358,16 @@ public function setDefaultLocale($locale)
}
}

/**
* Get the default locale.
*
* @return string
*/
public function getDefaultLocale()
{
return $this->defaultLocale;
}

/**
* Sets the locale.
*
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Translation/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down