diff options
13 files changed, 283 insertions, 12 deletions
diff --git a/app/config/config.yml b/app/config/config.yml index 956fdd07..a4d88b94 100644 --- a/app/config/config.yml +++ b/app/config/config.yml | |||
@@ -25,6 +25,11 @@ framework: | |||
25 | fragments: ~ | 25 | fragments: ~ |
26 | http_method_override: true | 26 | http_method_override: true |
27 | 27 | ||
28 | wallabag_core: | ||
29 | languages: | ||
30 | en: 'English' | ||
31 | fr: 'Français' | ||
32 | |||
28 | # Twig Configuration | 33 | # Twig Configuration |
29 | twig: | 34 | twig: |
30 | debug: "%kernel.debug%" | 35 | debug: "%kernel.debug%" |
diff --git a/app/config/services.yml b/app/config/services.yml index ff6a582b..80d6c1a1 100644 --- a/app/config/services.yml +++ b/app/config/services.yml | |||
@@ -18,3 +18,15 @@ services: | |||
18 | public: false | 18 | public: false |
19 | tags: | 19 | tags: |
20 | - { name: twig.extension } | 20 | - { name: twig.extension } |
21 | |||
22 | wallabag.locale_listener: | ||
23 | class: Wallabag\CoreBundle\EventListener\LocaleListener | ||
24 | arguments: ["%kernel.default_locale%"] | ||
25 | tags: | ||
26 | - { name: kernel.event_subscriber } | ||
27 | |||
28 | wallabag.user_locale_listener: | ||
29 | class: Wallabag\CoreBundle\EventListener\UserLocaleListener | ||
30 | arguments: ["@session"] | ||
31 | tags: | ||
32 | - { name: kernel.event_listener, event: security.interactive_login, method: onInteractiveLogin } | ||
diff --git a/src/Wallabag/CoreBundle/Controller/ConfigController.php b/src/Wallabag/CoreBundle/Controller/ConfigController.php index ecfecc66..ca4acc6a 100644 --- a/src/Wallabag/CoreBundle/Controller/ConfigController.php +++ b/src/Wallabag/CoreBundle/Controller/ConfigController.php | |||
@@ -42,7 +42,7 @@ class ConfigController extends Controller | |||
42 | 42 | ||
43 | $this->get('session')->getFlashBag()->add( | 43 | $this->get('session')->getFlashBag()->add( |
44 | 'notice', | 44 | 'notice', |
45 | 'Config saved' | 45 | 'Config saved. Some parameters will be considered after disconnection.' |
46 | ); | 46 | ); |
47 | 47 | ||
48 | return $this->redirect($this->generateUrl('config')); | 48 | 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 index 00000000..32acd1f1 --- /dev/null +++ b/src/Wallabag/CoreBundle/DependencyInjection/Configuration.php | |||
@@ -0,0 +1,25 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Wallabag\CoreBundle\DependencyInjection; | ||
4 | |||
5 | use Symfony\Component\Config\Definition\Builder\TreeBuilder; | ||
6 | use Symfony\Component\Config\Definition\ConfigurationInterface; | ||
7 | |||
8 | class Configuration implements ConfigurationInterface | ||
9 | { | ||
10 | public function getConfigTreeBuilder() | ||
11 | { | ||
12 | $treeBuilder = new TreeBuilder(); | ||
13 | $rootNode = $treeBuilder->root('wallabag_core'); | ||
14 | |||
15 | $rootNode | ||
16 | ->children() | ||
17 | ->arrayNode('languages') | ||
18 | ->prototype('scalar')->end() | ||
19 | ->end() | ||
20 | ->end() | ||
21 | ; | ||
22 | |||
23 | return $treeBuilder; | ||
24 | } | ||
25 | } | ||
diff --git a/src/Wallabag/CoreBundle/DependencyInjection/WallabagCoreExtension.php b/src/Wallabag/CoreBundle/DependencyInjection/WallabagCoreExtension.php index 7493351b..330cc957 100644 --- a/src/Wallabag/CoreBundle/DependencyInjection/WallabagCoreExtension.php +++ b/src/Wallabag/CoreBundle/DependencyInjection/WallabagCoreExtension.php | |||
@@ -11,6 +11,10 @@ class WallabagCoreExtension extends Extension | |||
11 | { | 11 | { |
12 | public function load(array $configs, ContainerBuilder $container) | 12 | public function load(array $configs, ContainerBuilder $container) |
13 | { | 13 | { |
14 | $configuration = new Configuration(); | ||
15 | $config = $this->processConfiguration($configuration, $configs); | ||
16 | $container->setParameter('wallabag_core.languages', $config['languages']); | ||
17 | |||
14 | $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); | 18 | $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
15 | $loader->load('services.yml'); | 19 | $loader->load('services.yml'); |
16 | } | 20 | } |
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 | } | ||
diff --git a/src/Wallabag/CoreBundle/EventListener/UserLocaleListener.php b/src/Wallabag/CoreBundle/EventListener/UserLocaleListener.php new file mode 100644 index 00000000..82d1a63a --- /dev/null +++ b/src/Wallabag/CoreBundle/EventListener/UserLocaleListener.php | |||
@@ -0,0 +1,37 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Wallabag\CoreBundle\EventListener; | ||
4 | |||
5 | use Symfony\Component\HttpFoundation\Session\Session; | ||
6 | use Symfony\Component\Security\Http\Event\InteractiveLoginEvent; | ||
7 | |||
8 | /** | ||
9 | * Stores the locale of the user in the session after the | ||
10 | * login. This can be used by the LocaleListener afterwards. | ||
11 | * | ||
12 | * @see http://symfony.com/doc/master/cookbook/session/locale_sticky_session.html | ||
13 | */ | ||
14 | class UserLocaleListener | ||
15 | { | ||
16 | /** | ||
17 | * @var Session | ||
18 | */ | ||
19 | private $session; | ||
20 | |||
21 | public function __construct(Session $session) | ||
22 | { | ||
23 | $this->session = $session; | ||
24 | } | ||
25 | |||
26 | /** | ||
27 | * @param InteractiveLoginEvent $event | ||
28 | */ | ||
29 | public function onInteractiveLogin(InteractiveLoginEvent $event) | ||
30 | { | ||
31 | $user = $event->getAuthenticationToken()->getUser(); | ||
32 | |||
33 | if (null !== $user->getConfig()->getLanguage()) { | ||
34 | $this->session->set('_locale', $user->getConfig()->getLanguage()); | ||
35 | } | ||
36 | } | ||
37 | } | ||
diff --git a/src/Wallabag/CoreBundle/Form/Type/ConfigType.php b/src/Wallabag/CoreBundle/Form/Type/ConfigType.php index 49b05b80..1f0ad89d 100644 --- a/src/Wallabag/CoreBundle/Form/Type/ConfigType.php +++ b/src/Wallabag/CoreBundle/Form/Type/ConfigType.php | |||
@@ -9,16 +9,20 @@ use Symfony\Component\OptionsResolver\OptionsResolver; | |||
9 | class ConfigType extends AbstractType | 9 | class ConfigType extends AbstractType |
10 | { | 10 | { |
11 | private $themes = array(); | 11 | private $themes = array(); |
12 | private $languages = array(); | ||
12 | 13 | ||
13 | /** | 14 | /** |
14 | * @param array $themes Themes come from the LiipThemeBundle (liip_theme.themes) | 15 | * @param array $themes Themes come from the LiipThemeBundle (liip_theme.themes) |
16 | * @param array $languages Languages come from configuration, array just code language as key and label as value | ||
15 | */ | 17 | */ |
16 | public function __construct($themes) | 18 | public function __construct($themes, $languages) |
17 | { | 19 | { |
18 | $this->themes = array_combine( | 20 | $this->themes = array_combine( |
19 | $themes, | 21 | $themes, |
20 | array_map(function ($s) { return ucwords(strtolower(str_replace('-', ' ', $s))); }, $themes) | 22 | array_map(function ($s) { return ucwords(strtolower(str_replace('-', ' ', $s))); }, $themes) |
21 | ); | 23 | ); |
24 | |||
25 | $this->languages = $languages; | ||
22 | } | 26 | } |
23 | 27 | ||
24 | public function buildForm(FormBuilderInterface $builder, array $options) | 28 | public function buildForm(FormBuilderInterface $builder, array $options) |
@@ -29,7 +33,9 @@ class ConfigType extends AbstractType | |||
29 | 'choices_as_values' => true, | 33 | 'choices_as_values' => true, |
30 | )) | 34 | )) |
31 | ->add('items_per_page') | 35 | ->add('items_per_page') |
32 | ->add('language') | 36 | ->add('language', 'choice', array( |
37 | 'choices' => $this->languages, | ||
38 | )) | ||
33 | ->add('save', 'submit') | 39 | ->add('save', 'submit') |
34 | ; | 40 | ; |
35 | } | 41 | } |
diff --git a/src/Wallabag/CoreBundle/Resources/config/services.yml b/src/Wallabag/CoreBundle/Resources/config/services.yml index c38787de..e29fcd1f 100644 --- a/src/Wallabag/CoreBundle/Resources/config/services.yml +++ b/src/Wallabag/CoreBundle/Resources/config/services.yml | |||
@@ -10,6 +10,7 @@ services: | |||
10 | class: Wallabag\CoreBundle\Form\Type\ConfigType | 10 | class: Wallabag\CoreBundle\Form\Type\ConfigType |
11 | arguments: | 11 | arguments: |
12 | - %liip_theme.themes% | 12 | - %liip_theme.themes% |
13 | - %wallabag_core.languages% | ||
13 | tags: | 14 | tags: |
14 | - { name: form.type, alias: config } | 15 | - { name: form.type, alias: config } |
15 | 16 | ||
diff --git a/src/Wallabag/CoreBundle/Resources/translations/messages.fr.yml b/src/Wallabag/CoreBundle/Resources/translations/messages.fr.yml index aced4d83..7b10dea1 100644 --- a/src/Wallabag/CoreBundle/Resources/translations/messages.fr.yml +++ b/src/Wallabag/CoreBundle/Resources/translations/messages.fr.yml | |||
@@ -108,7 +108,7 @@ download the application: "téléchargez l'application" | |||
108 | 108 | ||
109 | # Flash messages | 109 | # Flash messages |
110 | Information updated: "Vos informations personnelles ont bien été mises à jour" | 110 | Information updated: "Vos informations personnelles ont bien été mises à jour" |
111 | Config saved: "Les paramètres de wallabag ont bien été mis à jour" | 111 | "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." |
112 | RSS information updated: "La configuration des flux RSS a bien été mise à jour" | 112 | RSS information updated: "La configuration des flux RSS a bien été mise à jour" |
113 | Password updated: "Votre mot de passe a bien été mis à jour" | 113 | Password updated: "Votre mot de passe a bien été mis à jour" |
114 | Entry starred: "Article ajouté dans les favoris" | 114 | Entry starred: "Article ajouté dans les favoris" |
diff --git a/src/Wallabag/CoreBundle/Tests/Controller/ConfigControllerTest.php b/src/Wallabag/CoreBundle/Tests/Controller/ConfigControllerTest.php index 3da5e8b7..7085151a 100644 --- a/src/Wallabag/CoreBundle/Tests/Controller/ConfigControllerTest.php +++ b/src/Wallabag/CoreBundle/Tests/Controller/ConfigControllerTest.php | |||
@@ -46,7 +46,7 @@ class ConfigControllerTest extends WallabagCoreTestCase | |||
46 | $data = array( | 46 | $data = array( |
47 | 'config[theme]' => 0, | 47 | 'config[theme]' => 0, |
48 | 'config[items_per_page]' => '30', | 48 | 'config[items_per_page]' => '30', |
49 | 'config[language]' => 'fr_FR', | 49 | 'config[language]' => 'en', |
50 | ); | 50 | ); |
51 | 51 | ||
52 | $client->submit($form, $data); | 52 | $client->submit($form, $data); |
@@ -65,12 +65,7 @@ class ConfigControllerTest extends WallabagCoreTestCase | |||
65 | array(array( | 65 | array(array( |
66 | 'config[theme]' => 0, | 66 | 'config[theme]' => 0, |
67 | 'config[items_per_page]' => '', | 67 | 'config[items_per_page]' => '', |
68 | 'config[language]' => 'fr_FR', | 68 | 'config[language]' => 'en', |
69 | )), | ||
70 | array(array( | ||
71 | 'config[theme]' => 0, | ||
72 | 'config[items_per_page]' => '12', | ||
73 | 'config[language]' => '', | ||
74 | )), | 69 | )), |
75 | ); | 70 | ); |
76 | } | 71 | } |
diff --git a/src/Wallabag/CoreBundle/Tests/EventListener/LocaleListenerTest.php b/src/Wallabag/CoreBundle/Tests/EventListener/LocaleListenerTest.php new file mode 100644 index 00000000..356a411e --- /dev/null +++ b/src/Wallabag/CoreBundle/Tests/EventListener/LocaleListenerTest.php | |||
@@ -0,0 +1,83 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Wallabag\CoreBundle\Tests\EventListener; | ||
4 | |||
5 | use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; | ||
6 | use Symfony\Component\EventDispatcher\EventDispatcher; | ||
7 | use Symfony\Component\HttpFoundation\Request; | ||
8 | use Symfony\Component\HttpKernel\KernelEvents; | ||
9 | use Symfony\Component\HttpKernel\Event\GetResponseEvent; | ||
10 | use Symfony\Component\HttpKernel\HttpKernelInterface; | ||
11 | use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage; | ||
12 | use Symfony\Component\HttpFoundation\Session\Session; | ||
13 | use Wallabag\CoreBundle\EventListener\LocaleListener; | ||
14 | |||
15 | class LocaleListenerTest extends KernelTestCase | ||
16 | { | ||
17 | private function getEvent(Request $request) | ||
18 | { | ||
19 | return new GetResponseEvent($this->getMock('Symfony\Component\HttpKernel\HttpKernelInterface'), $request, HttpKernelInterface::MASTER_REQUEST); | ||
20 | } | ||
21 | |||
22 | public function testWithoutSession() | ||
23 | { | ||
24 | $request = Request::create('/'); | ||
25 | |||
26 | $listener = new LocaleListener('fr'); | ||
27 | $event = $this->getEvent($request); | ||
28 | |||
29 | $listener->onKernelRequest($event); | ||
30 | $this->assertEquals('en', $request->getLocale()); | ||
31 | } | ||
32 | |||
33 | public function testWithPreviousSession() | ||
34 | { | ||
35 | $request = Request::create('/'); | ||
36 | // generate a previous session | ||
37 | $request->cookies->set('MOCKSESSID', 'foo'); | ||
38 | $request->setSession(new Session(new MockArraySessionStorage())); | ||
39 | |||
40 | $listener = new LocaleListener('fr'); | ||
41 | $event = $this->getEvent($request); | ||
42 | |||
43 | $listener->onKernelRequest($event); | ||
44 | $this->assertEquals('fr', $request->getLocale()); | ||
45 | } | ||
46 | |||
47 | public function testLocaleFromRequestAttribute() | ||
48 | { | ||
49 | $request = Request::create('/'); | ||
50 | // generate a previous session | ||
51 | $request->cookies->set('MOCKSESSID', 'foo'); | ||
52 | $request->setSession(new Session(new MockArraySessionStorage())); | ||
53 | $request->attributes->set('_locale', 'es'); | ||
54 | |||
55 | $listener = new LocaleListener('fr'); | ||
56 | $event = $this->getEvent($request); | ||
57 | |||
58 | $listener->onKernelRequest($event); | ||
59 | $this->assertEquals('en', $request->getLocale()); | ||
60 | $this->assertEquals('es', $request->getSession()->get('_locale')); | ||
61 | } | ||
62 | |||
63 | public function testSubscribedEvents() | ||
64 | { | ||
65 | $request = Request::create('/'); | ||
66 | // generate a previous session | ||
67 | $request->cookies->set('MOCKSESSID', 'foo'); | ||
68 | $request->setSession(new Session(new MockArraySessionStorage())); | ||
69 | |||
70 | $listener = new LocaleListener('fr'); | ||
71 | $event = $this->getEvent($request); | ||
72 | |||
73 | $dispatcher = new EventDispatcher(); | ||
74 | $dispatcher->addSubscriber($listener); | ||
75 | |||
76 | $dispatcher->dispatch( | ||
77 | KernelEvents::REQUEST, | ||
78 | $event | ||
79 | ); | ||
80 | |||
81 | $this->assertEquals('fr', $request->getLocale()); | ||
82 | } | ||
83 | } | ||
diff --git a/src/Wallabag/CoreBundle/Tests/EventListener/UserLocaleListenerTest.php b/src/Wallabag/CoreBundle/Tests/EventListener/UserLocaleListenerTest.php new file mode 100644 index 00000000..e8a65fbf --- /dev/null +++ b/src/Wallabag/CoreBundle/Tests/EventListener/UserLocaleListenerTest.php | |||
@@ -0,0 +1,59 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Wallabag\CoreBundle\Tests\EventListener; | ||
4 | |||
5 | use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; | ||
6 | use Symfony\Component\HttpFoundation\Request; | ||
7 | use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage; | ||
8 | use Symfony\Component\HttpFoundation\Session\Session; | ||
9 | use Symfony\Component\Security\Http\Event\InteractiveLoginEvent; | ||
10 | use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; | ||
11 | use Wallabag\CoreBundle\EventListener\UserLocaleListener; | ||
12 | use Wallabag\CoreBundle\Entity\Config; | ||
13 | use Wallabag\UserBundle\Entity\User; | ||
14 | |||
15 | class UserLocaleListenerTest extends KernelTestCase | ||
16 | { | ||
17 | public function testWithLanguage() | ||
18 | { | ||
19 | $session = new Session(new MockArraySessionStorage()); | ||
20 | $listener = new UserLocaleListener($session); | ||
21 | |||
22 | $user = new User(); | ||
23 | $user->setEnabled(true); | ||
24 | |||
25 | $config = new Config($user); | ||
26 | $config->setLanguage('fr'); | ||
27 | |||
28 | $user->setConfig($config); | ||
29 | |||
30 | $userToken = new UsernamePasswordToken($user, '', 'test'); | ||
31 | $request = Request::create('/'); | ||
32 | $event = new InteractiveLoginEvent($request, $userToken); | ||
33 | |||
34 | $listener->onInteractiveLogin($event); | ||
35 | |||
36 | $this->assertEquals('fr', $session->get('_locale')); | ||
37 | } | ||
38 | |||
39 | public function testWithoutLanguage() | ||
40 | { | ||
41 | $session = new Session(new MockArraySessionStorage()); | ||
42 | $listener = new UserLocaleListener($session); | ||
43 | |||
44 | $user = new User(); | ||
45 | $user->setEnabled(true); | ||
46 | |||
47 | $config = new Config($user); | ||
48 | |||
49 | $user->setConfig($config); | ||
50 | |||
51 | $userToken = new UsernamePasswordToken($user, '', 'test'); | ||
52 | $request = Request::create('/'); | ||
53 | $event = new InteractiveLoginEvent($request, $userToken); | ||
54 | |||
55 | $listener->onInteractiveLogin($event); | ||
56 | |||
57 | $this->assertEquals('', $session->get('_locale')); | ||
58 | } | ||
59 | } | ||