diff options
author | Nicolas LÅ“uillet <nicolas@loeuillet.org> | 2015-10-18 15:35:42 +0200 |
---|---|---|
committer | Nicolas LÅ“uillet <nicolas@loeuillet.org> | 2015-10-18 15:35:42 +0200 |
commit | fcc6949d4a70f8b1d2c2b4e2c1e2fabdf6a93a7c (patch) | |
tree | cc09f37927d4a7ffbf576f636db0d36015190b08 /src/Wallabag/CoreBundle/EventListener/LocaleListener.php | |
parent | 3d3ed955f11006a408c6596eb9151a0afb28e721 (diff) | |
parent | 2aac2f278f1ca63f2097f80ddbdb924dea9ec59e (diff) | |
download | wallabag-fcc6949d4a70f8b1d2c2b4e2c1e2fabdf6a93a7c.tar.gz wallabag-fcc6949d4a70f8b1d2c2b4e2c1e2fabdf6a93a7c.tar.zst wallabag-fcc6949d4a70f8b1d2c2b4e2c1e2fabdf6a93a7c.zip |
Merge pull request #1446 from wallabag/v2-language-config
[WIP] language selection on config screen
Diffstat (limited to 'src/Wallabag/CoreBundle/EventListener/LocaleListener.php')
-rw-r--r-- | src/Wallabag/CoreBundle/EventListener/LocaleListener.php | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/src/Wallabag/CoreBundle/EventListener/LocaleListener.php b/src/Wallabag/CoreBundle/EventListener/LocaleListener.php new file mode 100644 index 00000000..80f59504 --- /dev/null +++ b/src/Wallabag/CoreBundle/EventListener/LocaleListener.php | |||
@@ -0,0 +1,44 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Wallabag\CoreBundle\EventListener; | ||
4 | |||
5 | use Symfony\Component\HttpKernel\Event\GetResponseEvent; | ||
6 | use Symfony\Component\HttpKernel\KernelEvents; | ||
7 | use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
8 | |||
9 | /** | ||
10 | * @see http://symfony.com/doc/current/cookbook/session/locale_sticky_session.html | ||
11 | */ | ||
12 | class 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 array( | ||
40 | // must be registered before the default Locale listener | ||
41 | KernelEvents::REQUEST => array(array('onKernelRequest', 17)), | ||
42 | ); | ||
43 | } | ||
44 | } | ||