Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
8 changes: 8 additions & 0 deletions DependencyInjection/JMSI18nRoutingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand Down
80 changes: 80 additions & 0 deletions EventListener/LocaleChoosingExceptListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

/*
* Copyright 2012 Johannes M. Schmitt <schmittjoh@gmail.com>
*
* 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 <schmittjoh@gmail.com>
*/
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) : '')));
}
}
}
7 changes: 7 additions & 0 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<parameter key="jms_i18n_routing.pattern_generation_strategy.class">JMS\I18nRoutingBundle\Router\DefaultPatternGenerationStrategy</parameter>

<parameter key="jms_i18n_routing.locale_choosing_listener.class">JMS\I18nRoutingBundle\EventListener\LocaleChoosingListener</parameter>
<parameter key="jms_i18n_routing.locale_choosing_except_listener.class">JMS\I18nRoutingBundle\EventListener\LocaleChoosingExceptListener</parameter>
<parameter key="jms_i18n_routing.cookie_setting_listener.class">JMS\I18nRoutingBundle\EventListener\CookieSettingListener</parameter>

<parameter key="jms_i18n_routing.route_translation_extractor.class">JMS\I18nRoutingBundle\Translation\RouteTranslationExtractor</parameter>
Expand Down Expand Up @@ -44,6 +45,12 @@
<argument>%jms_i18n_routing.locales%</argument>
<argument type="service" id="jms_i18n_routing.locale_resolver" />
</service>

<service id="jms_i18n_routing.locale_choosing_except_listener" class="%jms_i18n_routing.locale_choosing_except_listener.class%" public="false">
<argument>%jms_i18n_routing.default_locale%</argument>
<argument>%jms_i18n_routing.locales%</argument>
<argument type="service" id="jms_i18n_routing.locale_resolver" />
</service>

<service id="jms_i18n_routing.cookie_setting_listener" class="%jms_i18n_routing.cookie_setting_listener.class%" public="false" />

Expand Down
11 changes: 6 additions & 5 deletions Router/DefaultPatternGenerationStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}

Expand All @@ -72,4 +73,4 @@ public function addResources(RouteCollection $i18nCollection)
}
}
}
}
}