symfony/symfony — Issue #2759: [Request] Get the locale from Request in a twig_extension
Author: spike31
Created: 2011-12-01 11:05:44+00:00
State: open

I have a twig_extension and I would like to get the locale from the Request, so I get it with:

try{
$request = (null !== $this->container) ? $this->container->get('request') : null;
} catch(InactiveScopeException $e){
return array();
}

$defaultLocale = $this->container->getParameter('locale');

if ($request instanceof Request) {
$defaultLocale = $request->getLocale();
}

It's works, but this code is initialized in the constructor, and this doesn't work when I run the cache:clear command

[Symfony\Component\DependencyInjection\Exception\InactiveScopeException]
You cannot create a service ("request") of an inactive scope ("request").

I understand why, but I would like to know if is the correct way to retrieve the locale ? I add a try catch but... :/
Thanks