Created
June 17, 2014 09:34
-
-
Save edefiez/50ef8900946a04f6d444 to your computer and use it in GitHub Desktop.
Symfony2 : i18n Routing ( Src: Garfield-FR)
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\I18nBundle\DependencyInjection; | |
use Symfony\Component\DependencyInjection\ContainerBuilder; | |
use Symfony\Component\Config\FileLocator; | |
use Symfony\Component\HttpKernel\DependencyInjection\Extension; | |
use Symfony\Component\DependencyInjection\Loader; | |
class AcmeI18nExtension extends Extension | |
{ | |
/** | |
* {@inheritDoc} | |
*/ | |
public function load(array $configs, ContainerBuilder $container) | |
{ | |
$configuration = new Configuration(); | |
$config = $this->processConfiguration($configuration, $configs); | |
$loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); | |
$loader->load('services.xml'); | |
$container->setParameter('acme_i18n.available_locales', $config['available_locales']); | |
} | |
} |
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\I18nBundle\DependencyInjection; | |
use Symfony\Component\Config\Definition\Builder\TreeBuilder; | |
use Symfony\Component\Config\Definition\ConfigurationInterface; | |
class Configuration implements ConfigurationInterface | |
{ | |
/** | |
* {@inheritDoc} | |
*/ | |
public function getConfigTreeBuilder() | |
{ | |
$treeBuilder = new TreeBuilder(); | |
$rootNode = $treeBuilder->root('acme_i18n'); | |
$rootNode | |
->children() | |
->arrayNode('available_locales') | |
->isRequired() | |
->prototype('scalar')->end() | |
->end() | |
->end() | |
; | |
return $treeBuilder; | |
} | |
} |
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\I18nBundle\Controller; | |
use Symfony\Bundle\FrameworkBundle\Controller\Controller; | |
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; | |
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; | |
class DefaultController extends Controller | |
{ | |
... | |
/** | |
* @Route("/i18n/{_locale}", name="change_language") | |
*/ | |
public function changeLanguageAction($_locale) | |
{ | |
$router = $this->get('acme_i18n.router_translate_referer'); | |
return $this->redirect($router->getTranslatedUrl($_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
acme_i18n: | |
available_locales: ['de', 'en', 'fr'] |
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\I18nBundle\Router; | |
use Symfony\Component\HttpFoundation\Request; | |
use Symfony\Component\Routing\Router; | |
use Symfony\Component\Translation\Translator; | |
class RouterTranslateReferer | |
{ | |
/** | |
* @var Request | |
*/ | |
private $request; | |
/** | |
* @var Router | |
*/ | |
private $router; | |
/** | |
* @var $translator | |
*/ | |
private $translator; | |
/** | |
* @var $locales | |
*/ | |
private $locales; | |
/** | |
* @var $environment | |
*/ | |
private $environment; | |
/** | |
* Constructor | |
* | |
* @param Request $request | |
* @param Router $router | |
* @param Translator $translator | |
* @param Array $locales | |
* @param string $environment | |
*/ | |
public function __construct( | |
Request $request, | |
Router $router, | |
Translator $translator, | |
array $locales, | |
$environment | |
) | |
{ | |
$this->request = $request; | |
$this->router = $router; | |
$this->translator = $translator; | |
$this->locales = $locales; | |
$this->environment = $environment; | |
} | |
/** | |
* Get Translated Url | |
* | |
* @param string $locale | |
*/ | |
public function getTranslatedUrl($locale) | |
{ | |
$referer = $this->request->headers->get('referer'); | |
$port = $this->request->server->get('SERVER_PORT'); | |
$host = $this->request->server->get('HTTP_HOST'); | |
$script = ($this->environment === 'prod') ? | |
'' : | |
$this->request->server->get('SCRIPT_NAME'); | |
$removeBaseUrl = sprintf( | |
'%s://%s%s', | |
($port == '80') ? 'http' : 'https', | |
$host, | |
$script | |
); | |
$path = str_replace($removeBaseUrl, '', $referer); | |
$params = $this->router->match($path); | |
$route = $params['_route']; | |
$params['_locale'] = $this->AvailableLocale($locale); | |
unset($params['_controller'], $params['_route']); | |
return $this->router->generate($route, $params); | |
} | |
/** | |
* Available Locale | |
* | |
* @param string $locale | |
* | |
* @return string locale | |
*/ | |
private function AvailableLocale($locale) | |
{ | |
if (!in_array($locale, $this->locales)) { | |
$locale = $this->translator->getFallbackLocales()[0]; | |
} | |
return $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
<?xml version="1.0" ?> | |
<container xmlns="http://symfony.com/schema/dic/services" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> | |
<parameters> | |
<parameter key="acme_i18n.router_translate_referer.class">Acme\I18nBundle\Router\RouterTranslateReferer</parameter> | |
</parameters> | |
<services> | |
<service id="acme_i18n.router_translate_referer" class="%acme_i18n.router_translate_referer.class%" scope="request"> | |
<argument type="service" id="request" /> | |
<argument type="service" id="router" /> | |
<argument type="service" id="translator" /> | |
<argument>%acme_i18n.available_locales%</argument> | |
<argument>%kernel.environment%</argument> | |
</service> | |
</services> | |
</container> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment