diff --git a/lib/Alchemy/Phrasea/Core/Event/Subscriber/PhraseaLocaleSubscriber.php b/lib/Alchemy/Phrasea/Core/Event/Subscriber/PhraseaLocaleSubscriber.php index 8a01d4a9dd..71c1f2445a 100644 --- a/lib/Alchemy/Phrasea/Core/Event/Subscriber/PhraseaLocaleSubscriber.php +++ b/lib/Alchemy/Phrasea/Core/Event/Subscriber/PhraseaLocaleSubscriber.php @@ -14,7 +14,9 @@ namespace Alchemy\Phrasea\Core\Event\Subscriber; use Silex\Application; use Symfony\Component\HttpKernel\KernelEvents; use Symfony\Component\EventDispatcher\EventSubscriberInterface; +use Symfony\Component\HttpFoundation\Cookie; use Symfony\Component\HttpKernel\Event\GetResponseEvent; +use Symfony\Component\HttpKernel\Event\FilterResponseEvent; class PhraseaLocaleSubscriber implements EventSubscriberInterface { @@ -34,18 +36,13 @@ class PhraseaLocaleSubscriber implements EventSubscriberInterface // symfony locale is set on 16 priority, let's override it array('addLocale', 17), array('addLocale', 15), - array('removePhraseanetLocale', 14), ), + KernelEvents::RESPONSE => array( + array('addLocaleCookie', 8) + ) ); } - public function removePhraseanetLocale(GetResponseEvent $event) - { - if (isset($this->locale)) { - unset($this->locale); - } - } - public function addLocale(GetResponseEvent $event) { if (isset($this->locale)) { @@ -66,7 +63,7 @@ class PhraseaLocaleSubscriber implements EventSubscriberInterface ) ); - $this->app['locale'] = $this->locale = $this->app->share(function(Application $app) use ($event) { + $this->app['locale'] = $this->app->share(function(Application $app) use ($event) { if (isset($app['phraseanet.registry'])) { $event->getRequest()->setDefaultLocale( $app['phraseanet.registry']->get('GV_default_lng', 'en_GB') @@ -102,8 +99,16 @@ class PhraseaLocaleSubscriber implements EventSubscriberInterface return $event->getRequest()->getLocale(); }); - \phrasea::use_i18n($this->app['locale']); + $this->locale = $this->app['locale']; + \phrasea::use_i18n($this->locale); + } - $event->getResponse()->headers->setCookie(new Cookie('locale', $this->app['locale'], 0, '/', null, false, false)); + public function addLocaleCookie(FilterResponseEvent $event) + { + $cookies = $event->getRequest()->cookies; + + if (isset($this->locale) && (false === $cookies->has('locale') || $cookies->get('locale') !== $this->locale)) { + $event->getResponse()->headers->setCookie(new Cookie('locale', $this->locale, 0, '/', null, false, false)); + } } } diff --git a/tests/Alchemy/Tests/Phrasea/Core/Event/Subscriber/PhraseaLocaleSubscriberTest.php b/tests/Alchemy/Tests/Phrasea/Core/Event/Subscriber/PhraseaLocaleSubscriberTest.php index 7377ac6bda..65f587c661 100644 --- a/tests/Alchemy/Tests/Phrasea/Core/Event/Subscriber/PhraseaLocaleSubscriberTest.php +++ b/tests/Alchemy/Tests/Phrasea/Core/Event/Subscriber/PhraseaLocaleSubscriberTest.php @@ -57,6 +57,23 @@ class PhraseaLocaleSubscriberTest extends \PhraseanetPHPUnitAbstract $this->assertEquals('fr_FR', $client->getResponse()->getContent()); } + public function testCookieIsSet() + { + $client = new Client(self::$DI['app']); + $client->request('GET', '/', array(), array(), array('HTTP_ACCEPT_LANGUAGE' => 'fr-FR,fr;q=0.9')); + + $settedCookie = null; + foreach($client->getResponse()->headers->getCookies() as $cookie) { + if($cookie->getName() === 'locale') { + $settedCookie = $cookie; + break; + } + } + + $this->assertNotNull($settedCookie); + $this->assertEquals('fr_FR', $settedCookie->getValue()); + } + /** * @covers Alchemy\Phrasea\Application */ diff --git a/www/scripts/apps/admin/fields/app.js b/www/scripts/apps/admin/fields/app.js index 9fd5dfd018..baf77a0925 100644 --- a/www/scripts/apps/admin/fields/app.js +++ b/www/scripts/apps/admin/fields/app.js @@ -31,7 +31,9 @@ define([ $leftBlock : $(".left-block", this.$bottom), $rightBlock : $(".right-block", this.$bottom), fieldsToDelete : [], - lng : window.p4.lng || "en", + lng : function() { + return typeof p4 === "undefined" ? "en" : (p4.lng || "en"); + }, resizeListBlock: function () { var listBlock = $(".list-block", AdminFieldApp.$leftBlock); listBlock.height(AdminFieldApp.$window.height() - listBlock.offset().top - 10); diff --git a/www/scripts/apps/admin/fields/views/edit.js b/www/scripts/apps/admin/fields/views/edit.js index d4752f3e31..2864f6e4e3 100644 --- a/www/scripts/apps/admin/fields/views/edit.js +++ b/www/scripts/apps/admin/fields/views/edit.js @@ -35,7 +35,7 @@ define([ render: function() { var self = this; var template = _.template($("#edit_template").html(), { - lng: AdminFieldApp.lng, + lng: AdminFieldApp.lng(), field: this.model.toJSON(), vocabularyTypes: AdminFieldApp.vocabularyCollection.toJSON(), modelErrors: AdminFieldApp.errorManager.getModelError(this.model),