JMSI18nRoutingBundle
JMSI18nRoutingBundle copied to clipboard
Host redirected to english version
Hello
I've got a problem with the JMSI18RoutingBundle. I've the configuration as followed:
jms_i18n_routing:
default_locale: fr
locales: [fr, en]
strategy: custom
hosts:
en: www.mysite.com
fr: www.mysite.fr
redirect_to_host: true
If I go to www.mysite.com I have the english version as expected. However, if I go to www.mysite.fr then I'm redirected to the .com and still have the english version.
If I set redirect_to_host to false, then I have : No route found for "GET /".
Am I missing something ? Did anybody have the same issue ?
Thanks
After hours searching, I found a trick to "fix" my issue.
In JMS\I18nRoutingBundle\Router\I18nRouter before the
if (!($currentLocale = $this->context->getParameter('_locale'))
&& $this->container->isScopeActive('request')) {
(line 204) I added these lines :
foreach($this->hostMap as $locale => $host) {
if ($host == $this->context->getHost()) {
$this->context->setParameter('_locale', $locale);
}
}
Is it really necessary for me to add these lines? Did I miss something?
Looks similar to this: https://github.com/schmittjoh/JMSI18nRoutingBundle/issues/106
I've made a Listener to resolve this issue
<?php
use JMS\I18nRoutingBundle\Router\LocaleResolverInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Routing\RequestContextAwareInterface;
class JMSI18nRoutingLocaleListener implements EventSubscriberInterface
{
/**
* @var RequestContextAwareInterface
*/
private $router;
/**
* @var LocaleResolverInterface
*/
private $localeResolver;
/**
* Constructor
*
* @param LocaleResolverInterface $localeResolver
* @param RequestContextAwareInterface $router
*/
public function __construct(LocaleResolverInterface $localeResolver, RequestContextAwareInterface $router = null)
{
$this->localeResolver = $localeResolver;
$this->router = $router;
}
public function onKernelRequest(GetResponseEvent $event)
{
$request = $event->getRequest();
$locale = $this->localeResolver->resolveLocale($request, array());
$request->setDefaultLocale($locale);
$request->setLocale($locale);
$this->router->getContext()->setParameter('_locale', $locale);
}
public static function getSubscribedEvents()
{
return array(
KernelEvents::REQUEST => array(array('onKernelRequest', 35)),
);
}
}
And xml:
<service id="jms_i18n_routing.locale_listener" class="%jms_i18n_routing.locale_listener.class%">
<tag name="kernel.event_subscriber" />
<argument type="service" id="jms_i18n_routing.locale_resolver" />
<argument type="service" id="router" />
</service>
Thank you :)
@Dudytz Did you submit a pull request with your fix ?