Language selection on config screen

This commit is contained in:
Nicolas Lœuillet 2015-10-01 16:28:38 +02:00 committed by Jeremy Benoist
parent 3d3ed955f1
commit c89d35e851
11 changed files with 136 additions and 12 deletions

View file

@ -25,6 +25,11 @@ framework:
fragments: ~ fragments: ~
http_method_override: true http_method_override: true
wallabag_core:
languages:
en: 'English'
fr: 'Français'
# Twig Configuration # Twig Configuration
twig: twig:
debug: "%kernel.debug%" debug: "%kernel.debug%"

View file

@ -18,3 +18,15 @@ services:
public: false public: false
tags: tags:
- { name: twig.extension } - { 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 }

View file

@ -42,7 +42,7 @@ class ConfigController extends Controller
$this->get('session')->getFlashBag()->add( $this->get('session')->getFlashBag()->add(
'notice', 'notice',
'Config saved' 'Config saved. Some parameters will be considered after disconnection.'
); );
return $this->redirect($this->generateUrl('config')); return $this->redirect($this->generateUrl('config'));

View file

@ -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;
}
}

View file

@ -11,6 +11,10 @@ class WallabagCoreExtension extends Extension
{ {
public function load(array $configs, ContainerBuilder $container) 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 = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml'); $loader->load('services.yml');
} }

View file

@ -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)),
);
}
}

View file

@ -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());
}
}
}

View file

@ -9,16 +9,20 @@ use Symfony\Component\OptionsResolver\OptionsResolver;
class ConfigType extends AbstractType class ConfigType extends AbstractType
{ {
private $themes = array(); 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( $this->themes = array_combine(
$themes, $themes,
array_map(function ($s) { return ucwords(strtolower(str_replace('-', ' ', $s))); }, $themes) array_map(function ($s) { return ucwords(strtolower(str_replace('-', ' ', $s))); }, $themes)
); );
$this->languages = $languages;
} }
public function buildForm(FormBuilderInterface $builder, array $options) public function buildForm(FormBuilderInterface $builder, array $options)
@ -29,7 +33,9 @@ class ConfigType extends AbstractType
'choices_as_values' => true, 'choices_as_values' => true,
)) ))
->add('items_per_page') ->add('items_per_page')
->add('language') ->add('language', 'choice', array(
'choices' => $this->languages,
))
->add('save', 'submit') ->add('save', 'submit')
; ;
} }

View file

@ -10,6 +10,7 @@ services:
class: Wallabag\CoreBundle\Form\Type\ConfigType class: Wallabag\CoreBundle\Form\Type\ConfigType
arguments: arguments:
- %liip_theme.themes% - %liip_theme.themes%
- %wallabag_core.languages%
tags: tags:
- { name: form.type, alias: config } - { name: form.type, alias: config }

View file

@ -108,7 +108,7 @@ download the application: "téléchargez l'application"
# Flash messages # Flash messages
Information updated: "Vos informations personnelles ont bien été mises à jour" 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" RSS information updated: "La configuration des flux RSS a bien été mise à jour"
Password updated: "Votre mot de passe a bien été mis à jour" Password updated: "Votre mot de passe a bien été mis à jour"
Entry starred: "Article ajouté dans les favoris" Entry starred: "Article ajouté dans les favoris"

View file

@ -46,7 +46,7 @@ class ConfigControllerTest extends WallabagCoreTestCase
$data = array( $data = array(
'config[theme]' => 0, 'config[theme]' => 0,
'config[items_per_page]' => '30', 'config[items_per_page]' => '30',
'config[language]' => 'fr_FR', 'config[language]' => 'en',
); );
$client->submit($form, $data); $client->submit($form, $data);
@ -65,12 +65,7 @@ class ConfigControllerTest extends WallabagCoreTestCase
array(array( array(array(
'config[theme]' => 0, 'config[theme]' => 0,
'config[items_per_page]' => '', 'config[items_per_page]' => '',
'config[language]' => 'fr_FR', 'config[language]' => 'en',
)),
array(array(
'config[theme]' => 0,
'config[items_per_page]' => '12',
'config[language]' => '',
)), )),
); );
} }