aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/CoreBundle/Event/Listener/LocaleListener.php
diff options
context:
space:
mode:
authorJeremy Benoist <j0k3r@users.noreply.github.com>2017-01-27 09:34:32 +0100
committerGitHub <noreply@github.com>2017-01-27 09:34:32 +0100
commit6fb06904ecde15b1b07d0a2af945338b416cf0e2 (patch)
treee76f3e8142399316ec5660fab8c646b2c34b8336 /src/Wallabag/CoreBundle/Event/Listener/LocaleListener.php
parent05fa529bcfde01be5d320cb532900d72cf4b0830 (diff)
parent78295b99dd1721c613f1ce52e2debbe6f6db7753 (diff)
downloadwallabag-6fb06904ecde15b1b07d0a2af945338b416cf0e2.tar.gz
wallabag-6fb06904ecde15b1b07d0a2af945338b416cf0e2.tar.zst
wallabag-6fb06904ecde15b1b07d0a2af945338b416cf0e2.zip
Merge pull request #2416 from wallabag/2.2
wallabag 2.2.0
Diffstat (limited to 'src/Wallabag/CoreBundle/Event/Listener/LocaleListener.php')
-rw-r--r--src/Wallabag/CoreBundle/Event/Listener/LocaleListener.php44
1 files changed, 44 insertions, 0 deletions
diff --git a/src/Wallabag/CoreBundle/Event/Listener/LocaleListener.php b/src/Wallabag/CoreBundle/Event/Listener/LocaleListener.php
new file mode 100644
index 00000000..b435d99e
--- /dev/null
+++ b/src/Wallabag/CoreBundle/Event/Listener/LocaleListener.php
@@ -0,0 +1,44 @@
1<?php
2
3namespace Wallabag\CoreBundle\Event\Listener;
4
5use Symfony\Component\EventDispatcher\EventSubscriberInterface;
6use Symfony\Component\HttpKernel\Event\GetResponseEvent;
7use Symfony\Component\HttpKernel\KernelEvents;
8
9/**
10 * @see http://symfony.com/doc/current/cookbook/session/locale_sticky_session.html
11 */
12class LocaleListener implements EventSubscriberInterface
13{
14 private $defaultLocale;
15
16 public function __construct($defaultLocale = 'en')
17 {
18 $this->defaultLocale = $defaultLocale;
19 }
20
21 public function onKernelRequest(GetResponseEvent $event)
22 {
23 $request = $event->getRequest();
24 if (!$request->hasPreviousSession()) {
25 return;
26 }
27
28 // try to see if the locale has been set as a _locale routing parameter
29 if ($locale = $request->attributes->get('_locale')) {
30 $request->getSession()->set('_locale', $locale);
31 } else {
32 // if no explicit locale has been set on this request, use one from the session
33 $request->setLocale($request->getSession()->get('_locale', $this->defaultLocale));
34 }
35 }
36
37 public static function getSubscribedEvents()
38 {
39 return [
40 // must be registered before the default Locale listener
41 KernelEvents::REQUEST => [['onKernelRequest', 17]],
42 ];
43 }
44}