Created
June 10, 2012 22:05
-
-
Save qrizly/2907485 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
parameters: | |
locales: [en, nl] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace ACME\ExampleBundle\Listener; | |
use Gedmo\Translatable\TranslatableListener as Listener; | |
use Symfony\Component\HttpKernel\Event\GetResponseEvent; | |
use Symfony\Component\DependencyInjection\ContainerInterface; | |
/** | |
* TranslationListener | |
*/ | |
class TranslationListener | |
{ | |
protected $listener; | |
protected $container; | |
public function __construct(Listener $listener, ContainerInterface $container) | |
{ | |
$this->listener = $listener; | |
$this->container = $container; | |
} | |
/** | |
* Set the translation listener locale from the session. | |
* | |
* This method should be attached to the kernel.request event. | |
* | |
* @param GetResponseEvent $event | |
*/ | |
public function onKernelRequest(GetResponseEvent $event) | |
{ | |
$locale = null; | |
$request = $this->container->get('request'); | |
$session = $event->getRequest()->getSession(); | |
if (null !== $session) | |
{ | |
$locale = $session->getLocale(); | |
} | |
if ( | |
$request->query->has('lang') && | |
in_array($request->query->get('lang'), $this->container->getParameter('locales')) | |
) | |
{ | |
$locale = $request->query->get('lang'); | |
} | |
if ( | |
$request->request->has('lang') && | |
in_array($request->request->get('lang'), $this->container->getParameter('locales')) | |
) | |
{ | |
$locale = $request->request->get('lang'); | |
} | |
if($locale !== NULL) | |
{ | |
$this->listener->setTranslatableLocale($locale); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace ACME\ExampleBundle\Routing; | |
use Symfony\Component\Routing\Router as BaseRouter; | |
use Symfony\Component\Routing\RequestContext; | |
use Symfony\Component\DependencyInjection\ContainerInterface; | |
class Router extends BaseRouter | |
{ | |
protected $container; | |
/** | |
* Constructor. | |
* | |
* @param ContainerInterface $container A ContainerInterface instance | |
* @param mixed $resource The main resource to load | |
* @param array $options An array of options | |
* @param RequestContext $context The context | |
* @param array $defaults The default values | |
*/ | |
public function __construct(ContainerInterface $container, $resource, array $options = array(), RequestContext $context = null, array $defaults = array()) | |
{ | |
$this->container = $container; | |
$this->resource = $resource; | |
$this->context = null === $context ? new RequestContext() : $context; | |
$this->defaults = $defaults; | |
$this->setOptions($options); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function getRouteCollection() | |
{ | |
if (null === $this->collection) { | |
$this->collection = $this->container->get('routing.loader')->load($this->resource, $this->options['resource_type']); | |
} | |
return $this->collection; | |
} | |
/** | |
* Generates a URL from the given parameters. | |
* | |
* @param string $name The name of the route | |
* @param mixed $parameters An array of parameters | |
* @param Boolean $absolute Whether to generate an absolute URL | |
* | |
* @return string The generated URL | |
*/ | |
public function generate($name, $parameters = array(), $absolute = false) | |
{ | |
$request = $this->container->get('request'); | |
$locales = $this->container->getParameter('locales'); | |
if( | |
$request->request->has("lang") && | |
in_array($request->request->get("lang"), $locales) && | |
(! isset($parameters['lang']) || ! in_array($parameters['lang'], $locales)) | |
) | |
{ | |
$parameters['lang'] = $request->request->get("lang"); | |
} | |
if( | |
$request->query->has("lang") && | |
in_array($request->query->get("lang"), $locales) && | |
(! isset($parameters['lang']) || ! in_array($parameters['lang'], $locales)) | |
) | |
{ | |
$parameters['lang'] = $request->query->get("lang"); | |
} | |
return $this->getGenerator()->generate($name, $parameters, $absolute); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#should be in your bundle (ACME\ExampleBundle\Resources\config\services.yml) | |
parameters: | |
router.class: ACME\ExampleBundle\Routing\Router | |
services: | |
ACME.listener.translatable: | |
class: ACME\DosingGuideBundle\Listener\TranslationListener | |
arguments: [ @gedmo.listener.translatable, @service_container ] | |
tags: | |
- { name: kernel.event_listener, event: kernel.request, method: onKernelRequest } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment