Permalink
Browse files

added a cookie setting listener to remember a users language when no …

…host map is used
  • Loading branch information...
1 parent e476bbd commit 62b83843daa047f6b86a7d315182cc42a36e817a @schmittjoh schmittjoh committed Apr 11, 2012
@@ -67,6 +67,12 @@ public function load(array $configs, ContainerBuilder $container)
->getDefinition('jms_i18n_routing.locale_resolver.default')
->addArgument(array_flip($config['hosts']))
;
+ } else {
+ $container
+ ->getDefinition('jms_i18n_routing.cookie_setting_listener')
+ ->setPublic(true)
+ ->addTag('kernel.event_listener', array('event' => 'kernel.response', 'priority' => 256))
+ ;
}
// remove route extractor if JMSTranslationBundle is not enabled to avoid any problems
@@ -0,0 +1,32 @@
+<?php
+
+namespace JMS\I18nRoutingBundle\EventListener;
+
+use Symfony\Component\HttpFoundation\Cookie;
+use Symfony\Component\HttpKernel\HttpKernelInterface;
+use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
+
+/**
+ * Sets the user's language as a cookie.
+ *
+ * This is necessary if you are not using a host map, and still would like to
+ * use Varnish in front of your Symfony2 application.
+ *
+ * @author Johannes M. Schmitt <schmittjoh@gmail.com>
+ */
+class CookieSettingListener
+{
+ public function onKernelResponse(FilterResponseEvent $event)
+ {
+ if (HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType()) {
+ return;
+ }
+
+ $request = $event->getRequest();
+
+ if (!$request->cookies->has('hl')
+ || $request->cookies->get('hl') !== $request->getLocale()) {
+ $event->getResponse()->headers->setCookie(new Cookie('hl', $request->getLocale(), time() + 86400 * 365));
+ }
+ }
+}
@@ -68,6 +68,7 @@ public function onKernelException(GetResponseForExceptionEvent $event)
}
$locale = $this->localeResolver->resolveLocale($request, $this->locales) ?: $this->defaultLocale;
+ $request->setLocale($locale);
$event->setResponse(new RedirectResponse($request->getBaseUrl().'/'.$locale.'/'));
}
}
@@ -13,6 +13,7 @@
<parameter key="jms_i18n_routing.pattern_generation_strategy.class">JMS\I18nRoutingBundle\Router\DefaultPatternGenerationStrategy</parameter>
<parameter key="jms_i18n_routing.locale_choosing_listener.class">JMS\I18nRoutingBundle\EventListener\LocaleChoosingListener</parameter>
+ <parameter key="jms_i18n_routing.cookie_setting_listener.class">JMS\I18nRoutingBundle\EventListener\CookieSettingListener</parameter>
<parameter key="jms_i18n_routing.route_translation_extractor.class">JMS\I18nRoutingBundle\Translation\RouteTranslationExtractor</parameter>
</parameters>
@@ -42,6 +43,8 @@
<argument type="service" id="jms_i18n_routing.locale_resolver" />
</service>
+ <service id="jms_i18n_routing.cookie_setting_listener" class="%jms_i18n_routing.cookie_setting_listener.class%" public="false" />
+
<service id="jms_i18n_routing.route_exclusion_strategy" class="%jms_i18n_routing.route_exclusion_strategy.class%" public="false" />
<service id="jms_i18n_routing.pattern_generation_strategy.default" class="%jms_i18n_routing.pattern_generation_strategy.class%" public="false">
@@ -42,4 +42,19 @@ public function getLocaleChoosingTests()
array('fr;q=0.5', 'en'),
);
}
+
+ public function testLanguageCookieIsSet()
+ {
+ $client = $this->createClient(array('config' => 'strategy_prefix.yml'));
+ $client->insulate();
+
+ $client->request('GET', '/?hl=de');
+
+ $response = $client->getResponse();
+ $this->assertTrue($response->isRedirect('/de/'), (string) $response);
+
+ $cookies = $response->headers->getCookies();
+ $this->assertSame(2, count($cookies));
+ $this->assertSame('de', $cookies[0]->getValue());
+ }
}

0 comments on commit 62b8384

Please sign in to comment.