]> git.immae.eu Git - github/wallabag/wallabag.git/commitdiff
Language selection on config screen
authorNicolas Lœuillet <nicolas@loeuillet.org>
Thu, 1 Oct 2015 14:28:38 +0000 (16:28 +0200)
committerJeremy Benoist <jeremy.benoist@gmail.com>
Thu, 15 Oct 2015 19:42:29 +0000 (21:42 +0200)
app/config/config.yml
app/config/services.yml
src/Wallabag/CoreBundle/Controller/ConfigController.php
src/Wallabag/CoreBundle/DependencyInjection/Configuration.php [new file with mode: 0644]
src/Wallabag/CoreBundle/DependencyInjection/WallabagCoreExtension.php
src/Wallabag/CoreBundle/EventListener/LocaleListener.php [new file with mode: 0644]
src/Wallabag/CoreBundle/EventListener/UserLocaleListener.php [new file with mode: 0644]
src/Wallabag/CoreBundle/Form/Type/ConfigType.php
src/Wallabag/CoreBundle/Resources/config/services.yml
src/Wallabag/CoreBundle/Resources/translations/messages.fr.yml
src/Wallabag/CoreBundle/Tests/Controller/ConfigControllerTest.php

index 956fdd07c7d5174983bd566863c9b3f535af3b93..a4d88b94fe3406252d6833db0829eeff0b6bd8d1 100644 (file)
@@ -25,6 +25,11 @@ framework:
     fragments:       ~
     http_method_override: true
 
+wallabag_core:
+    languages:
+        en: 'English'
+        fr: 'Français'
+
 # Twig Configuration
 twig:
     debug:            "%kernel.debug%"
index ff6a582bb36bfee7b006168eaa5889badde0d69d..80d6c1a1ade63fc88fe028a3ee5cd1137737d5cc 100644 (file)
@@ -18,3 +18,15 @@ services:
         public: false
         tags:
             - { name: twig.extension }
+
+    wallabag.locale_listener:
+        class: Wallabag\CoreBundle\EventListener\LocaleListener
+        arguments: ["%kernel.default_locale%"]
+        tags:
+            - { name: kernel.event_subscriber }
+
+    wallabag.user_locale_listener:
+        class: Wallabag\CoreBundle\EventListener\UserLocaleListener
+        arguments: ["@session"]
+        tags:
+            - { name: kernel.event_listener, event: security.interactive_login, method: onInteractiveLogin }
index ecfecc66daa1084fc71838c7684c6eb7e30f7b0e..ca4acc6ae450a3acd00af827ef3090581eb543d5 100644 (file)
@@ -42,7 +42,7 @@ class ConfigController extends Controller
 
             $this->get('session')->getFlashBag()->add(
                 'notice',
-                'Config saved'
+                'Config saved. Some parameters will be considered after disconnection.'
             );
 
             return $this->redirect($this->generateUrl('config'));
diff --git a/src/Wallabag/CoreBundle/DependencyInjection/Configuration.php b/src/Wallabag/CoreBundle/DependencyInjection/Configuration.php
new file mode 100644 (file)
index 0000000..32acd1f
--- /dev/null
@@ -0,0 +1,25 @@
+<?php
+
+namespace Wallabag\CoreBundle\DependencyInjection;
+
+use Symfony\Component\Config\Definition\Builder\TreeBuilder;
+use Symfony\Component\Config\Definition\ConfigurationInterface;
+
+class Configuration implements ConfigurationInterface
+{
+    public function getConfigTreeBuilder()
+    {
+        $treeBuilder = new TreeBuilder();
+        $rootNode = $treeBuilder->root('wallabag_core');
+
+        $rootNode
+            ->children()
+                ->arrayNode('languages')
+                    ->prototype('scalar')->end()
+                ->end()
+            ->end()
+        ;
+
+        return $treeBuilder;
+    }
+}
index 7493351bd7b4bab1986843046f5f00077cbbed04..330cc957698660ad95eab41e095419fedca206da 100644 (file)
@@ -11,6 +11,10 @@ class WallabagCoreExtension extends Extension
 {
     public function load(array $configs, ContainerBuilder $container)
     {
+        $configuration = new Configuration();
+        $config = $this->processConfiguration($configuration, $configs);
+        $container->setParameter('wallabag_core.languages', $config['languages']);
+
         $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
         $loader->load('services.yml');
     }
diff --git a/src/Wallabag/CoreBundle/EventListener/LocaleListener.php b/src/Wallabag/CoreBundle/EventListener/LocaleListener.php
new file mode 100644 (file)
index 0000000..7179531
--- /dev/null
@@ -0,0 +1,41 @@
+<?php
+
+namespace Wallabag\CoreBundle\EventListener;
+
+use Symfony\Component\HttpKernel\Event\GetResponseEvent;
+use Symfony\Component\HttpKernel\KernelEvents;
+use Symfony\Component\EventDispatcher\EventSubscriberInterface;
+
+class LocaleListener implements EventSubscriberInterface
+{
+    private $defaultLocale;
+
+    public function __construct($defaultLocale = 'en')
+    {
+        $this->defaultLocale = $defaultLocale;
+    }
+
+    public function onKernelRequest(GetResponseEvent $event)
+    {
+        $request = $event->getRequest();
+        if (!$request->hasPreviousSession()) {
+            return;
+        }
+
+        // try to see if the locale has been set as a _locale routing parameter
+        if ($locale = $request->attributes->get('_locale')) {
+            $request->getSession()->set('_locale', $locale);
+        } else {
+            // if no explicit locale has been set on this request, use one from the session
+            $request->setLocale($request->getSession()->get('_locale', $this->defaultLocale));
+        }
+    }
+
+    public static function getSubscribedEvents()
+    {
+        return array(
+            // must be registered before the default Locale listener
+            KernelEvents::REQUEST => array(array('onKernelRequest', 17)),
+        );
+    }
+}
diff --git a/src/Wallabag/CoreBundle/EventListener/UserLocaleListener.php b/src/Wallabag/CoreBundle/EventListener/UserLocaleListener.php
new file mode 100644 (file)
index 0000000..97bfabc
--- /dev/null
@@ -0,0 +1,35 @@
+<?php
+
+namespace Wallabag\CoreBundle\EventListener;
+
+use Symfony\Component\HttpFoundation\Session\Session;
+use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
+
+/**
+ * Stores the locale of the user in the session after the
+ * login. This can be used by the LocaleListener afterwards.
+ */
+class UserLocaleListener
+{
+    /**
+     * @var Session
+     */
+    private $session;
+
+    public function __construct(Session $session)
+    {
+        $this->session = $session;
+    }
+
+    /**
+     * @param InteractiveLoginEvent $event
+     */
+    public function onInteractiveLogin(InteractiveLoginEvent $event)
+    {
+        $user = $event->getAuthenticationToken()->getUser();
+
+        if (null !== $user->getConfig()->getLanguage()) {
+            $this->session->set('_locale', $user->getConfig()->getLanguage());
+        }
+    }
+}
index 49b05b807185f5af25525bddd81476b13bf42e38..1f0ad89dfad578ffeac5c5067a0748da61dfe3fd 100644 (file)
@@ -9,16 +9,20 @@ use Symfony\Component\OptionsResolver\OptionsResolver;
 class ConfigType extends AbstractType
 {
     private $themes = array();
+    private $languages = array();
 
     /**
-     * @param array $themes Themes come from the LiipThemeBundle (liip_theme.themes)
+     * @param array $themes    Themes come from the LiipThemeBundle (liip_theme.themes)
+     * @param array $languages Languages come from configuration, array just code language as key and label as value
      */
-    public function __construct($themes)
+    public function __construct($themes, $languages)
     {
         $this->themes = array_combine(
             $themes,
             array_map(function ($s) { return ucwords(strtolower(str_replace('-', ' ', $s))); }, $themes)
         );
+
+        $this->languages = $languages;
     }
 
     public function buildForm(FormBuilderInterface $builder, array $options)
@@ -29,7 +33,9 @@ class ConfigType extends AbstractType
                 'choices_as_values' => true,
             ))
             ->add('items_per_page')
-            ->add('language')
+            ->add('language', 'choice', array(
+                'choices' => $this->languages,
+            ))
             ->add('save', 'submit')
         ;
     }
index c38787ded9040dac07844a2f5c6b5224209814d9..e29fcd1f1b9c0c46fddd38cc01a7b342d1e6240c 100644 (file)
@@ -10,6 +10,7 @@ services:
         class: Wallabag\CoreBundle\Form\Type\ConfigType
         arguments:
             - %liip_theme.themes%
+            - %wallabag_core.languages%
         tags:
             - { name: form.type, alias: config }
 
index aced4d83fd59c3d06e25acab449ff0802df8dd11..7b10dea11ef2d414c9efbafa5ef3aa0b44a026af 100644 (file)
@@ -108,7 +108,7 @@ download the application: "téléchargez l'application"
 
 # Flash messages
 Information updated: "Vos informations personnelles ont bien été mises à jour"
-Config saved: "Les paramètres de wallabag ont bien été mis à jour"
+"Config saved. Some parameters will be considered after disconnection.": "Les paramètres ont bien été mis à jour. Certains seront pris en compte après déconnexion."
 RSS information updated: "La configuration des flux RSS a bien été mise à jour"
 Password updated: "Votre mot de passe a bien été mis à jour"
 Entry starred: "Article ajouté dans les favoris"
index 3da5e8b73de1b9458d10482ca63042ca4b008187..7085151ae405f567a440df36606213af22dd4062 100644 (file)
@@ -46,7 +46,7 @@ class ConfigControllerTest extends WallabagCoreTestCase
         $data = array(
             'config[theme]' => 0,
             'config[items_per_page]' => '30',
-            'config[language]' => 'fr_FR',
+            'config[language]' => 'en',
         );
 
         $client->submit($form, $data);
@@ -65,12 +65,7 @@ class ConfigControllerTest extends WallabagCoreTestCase
             array(array(
                 'config[theme]' => 0,
                 'config[items_per_page]' => '',
-                'config[language]' => 'fr_FR',
-            )),
-            array(array(
-                'config[theme]' => 0,
-                'config[items_per_page]' => '12',
-                'config[language]' => '',
+                'config[language]' => 'en',
             )),
         );
     }