From 4dbcfef9c6c707a4271b0ee54c279e6064aba0d8 Mon Sep 17 00:00:00 2001 From: Lughino Date: Fri, 6 Dec 2013 01:52:06 +0100 Subject: [PATCH 1/5] Insert new strategy --- DependencyInjection/Configuration.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index cc4a9da..1f576b0 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -80,8 +80,8 @@ public function getConfigTreeBuilder() ->scalarNode('strategy') ->defaultValue('custom') ->validate() - ->ifNotInArray(array('prefix', 'prefix_except_default', 'custom')) - ->thenInvalid('Must be one of the following: prefix, prefix_except_default, or custom (default)') + ->ifNotInArray(array('prefix', 'prefix_except_default', 'prefix_except_default_listener', 'custom')) + ->thenInvalid('Must be one of the following: prefix, prefix_except_default, prefix_except_default_listener, or custom (default)') ->end() ->end() ->booleanNode('prefix_with_locale')->defaultFalse()->end() From e327a1f2198b898c112015f0e7d5d312e886bb97 Mon Sep 17 00:00:00 2001 From: Lughino Date: Fri, 6 Dec 2013 01:53:56 +0100 Subject: [PATCH 2/5] Create new listener for new strategy --- .../LocaleChoosingExceptListener.php | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 EventListener/LocaleChoosingExceptListener.php diff --git a/EventListener/LocaleChoosingExceptListener.php b/EventListener/LocaleChoosingExceptListener.php new file mode 100644 index 0000000..3aeef59 --- /dev/null +++ b/EventListener/LocaleChoosingExceptListener.php @@ -0,0 +1,80 @@ + + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +namespace JMS\I18nRoutingBundle\EventListener; + +use JMS\I18nRoutingBundle\Router\LocaleResolverInterface; +use Symfony\Component\HttpFoundation\RedirectResponse; +use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent; +use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; +use Symfony\Component\HttpKernel\HttpKernelInterface; +use Symfony\Component\Routing\Exception\ResourceNotFoundException; + +/** + * Chooses the default locale. + * + * This listener chooses the default locale to use on the first request of a + * user to the application. + * + * This listener is only active if the strategy is "prefix_except_default_listener". + * + * @author Johannes M. Schmitt + */ +class LocaleChoosingExceptListener +{ + private $defaultLocale; + private $locales; + private $localeResolver; + + public function __construct($defaultLocale, array $locales, LocaleResolverInterface $localeResolver) + { + $this->defaultLocale = $defaultLocale; + $this->locales = $locales; + $this->localeResolver = $localeResolver; + } + + public function onKernelException(GetResponseForExceptionEvent $event) + { + if (HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType()) { + return; + } + + $request = $event->getRequest(); + if ('' !== rtrim($request->getPathInfo(), '/')) { + return; + } + + $ex = $event->getException(); + if (!$ex instanceof NotFoundHttpException || !$ex->getPrevious() instanceof ResourceNotFoundException) { + return; + } + + $locale = $this->localeResolver->resolveLocale($request, $this->locales) ?: $this->defaultLocale; + $request->setLocale($locale); + + $params = $request->query->all(); + unset($params['hl']); + + if($locale === $this->defaultLocale) + { + $event->setResponse(new RedirectResponse($request->getBaseUrl().'/'.($params ? '?'.http_build_query($params) : ''))); + } else { + $event->setResponse(new RedirectResponse($request->getBaseUrl().'/'.$locale.'/'.($params ? '?'.http_build_query($params) : ''))); + } + } +} From 4853c28f419f6067fd72717d38e977d0dad17b14 Mon Sep 17 00:00:00 2001 From: Lughino Date: Fri, 6 Dec 2013 01:55:08 +0100 Subject: [PATCH 3/5] Create service for new listener --- Resources/config/services.xml | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Resources/config/services.xml b/Resources/config/services.xml index a5c3f66..7a84a87 100644 --- a/Resources/config/services.xml +++ b/Resources/config/services.xml @@ -13,6 +13,7 @@ JMS\I18nRoutingBundle\Router\DefaultPatternGenerationStrategy JMS\I18nRoutingBundle\EventListener\LocaleChoosingListener + JMS\I18nRoutingBundle\EventListener\LocaleChoosingExceptListener JMS\I18nRoutingBundle\EventListener\CookieSettingListener JMS\I18nRoutingBundle\Translation\RouteTranslationExtractor @@ -44,6 +45,12 @@ %jms_i18n_routing.locales% + + + %jms_i18n_routing.default_locale% + %jms_i18n_routing.locales% + + From 21daec7151307487df87f733a5c0cdbdda829fcf Mon Sep 17 00:00:00 2001 From: Lughino Date: Fri, 6 Dec 2013 01:57:17 +0100 Subject: [PATCH 4/5] Added call to the listener for the new strategy --- DependencyInjection/JMSI18nRoutingExtension.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/DependencyInjection/JMSI18nRoutingExtension.php b/DependencyInjection/JMSI18nRoutingExtension.php index 9bc1549..811b9b1 100644 --- a/DependencyInjection/JMSI18nRoutingExtension.php +++ b/DependencyInjection/JMSI18nRoutingExtension.php @@ -57,6 +57,14 @@ public function load(array $configs, ContainerBuilder $container) ->addTag('kernel.event_listener', array('event' => 'kernel.exception', 'priority' => 128)) ; } + + if ('prefix_except_default_listener' === $config['strategy']) { + $container + ->getDefinition('jms_i18n_routing.locale_choosing_except_listener') + ->setPublic(true) + ->addTag('kernel.event_listener', array('event' => 'kernel.exception', 'priority' => 128)) + ; + } if ($config['hosts']) { $container->setParameter('jms_i18n_routing.hostmap', $config['hosts']); From cf36fe58213bae6175e7af361921ddd7e34863ec Mon Sep 17 00:00:00 2001 From: Lughino Date: Fri, 6 Dec 2013 01:59:31 +0100 Subject: [PATCH 5/5] Add new constant STRATEGY_PREFIX_EXCEPT_DEFAULT_LISTENER --- Router/DefaultPatternGenerationStrategy.php | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/Router/DefaultPatternGenerationStrategy.php b/Router/DefaultPatternGenerationStrategy.php index 05255a7..d5436df 100755 --- a/Router/DefaultPatternGenerationStrategy.php +++ b/Router/DefaultPatternGenerationStrategy.php @@ -2,9 +2,9 @@ namespace JMS\I18nRoutingBundle\Router; -use Symfony\Component\Routing\RouteCollection; -use Symfony\Component\Translation\TranslatorInterface; -use Symfony\Component\Routing\Route; +use Symfony\Component\Routing\RouteCollection; +use Symfony\Component\Translation\TranslatorInterface; +use Symfony\Component\Routing\Route; /** * The default strategy supports 3 different scenarios, and makes use of the @@ -16,6 +16,7 @@ class DefaultPatternGenerationStrategy implements PatternGenerationStrategyInter { const STRATEGY_PREFIX = 'prefix'; const STRATEGY_PREFIX_EXCEPT_DEFAULT = 'prefix_except_default'; + const STRATEGY_PREFIX_EXCEPT_DEFAULT_LISTENER = 'prefix_except_default_listener'; const STRATEGY_CUSTOM = 'custom'; private $strategy; @@ -49,7 +50,7 @@ public function generateI18nPatterns($routeName, Route $route) // prefix with locale if requested if (self::STRATEGY_PREFIX === $this->strategy - || (self::STRATEGY_PREFIX_EXCEPT_DEFAULT === $this->strategy && $this->defaultLocale !== $locale)) { + || (self::STRATEGY_PREFIX_EXCEPT_DEFAULT === $this->strategy && $this->defaultLocale !== $locale) || (self::STRATEGY_PREFIX_EXCEPT_DEFAULT_LISTENER === $this->strategy && $this->defaultLocale !== $locale)) { $i18nPattern = '/'.$locale.$i18nPattern; } @@ -72,4 +73,4 @@ public function addResources(RouteCollection $i18nCollection) } } } -} \ No newline at end of file +}