Created
May 21, 2013 14:23
-
-
Save kayue/5620157 to your computer and use it in GitHub Desktop.
Hacking JMSI18nRoutingBundle, allow localization in path.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Hypebeast\DesktopBundle\Router; | |
use Symfony\Component\Routing\RouteCollection; | |
use Symfony\Component\Translation\TranslatorInterface; | |
use Symfony\Component\Routing\Route; | |
use JMS\I18nRoutingBundle\Router\PatternGenerationStrategyInterface; | |
use JMS\I18nRoutingBundle\Router\DefaultPatternGenerationStrategy; | |
class RoutePatternGenerationStrategy extends DefaultPatternGenerationStrategy implements PatternGenerationStrategyInterface | |
{ | |
private $locales; | |
public function __construct($strategy, TranslatorInterface $translator, array $locales, $cacheDir, $translationDomain = 'routes', $defaultLocale = 'en') | |
{ | |
parent::__construct($strategy, $translator, $locales, $cacheDir, $translationDomain, $defaultLocale); | |
$this->locales = $locales; | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
public function generateI18nPatterns($routeName, Route $route) | |
{ | |
$patterns = array(); | |
foreach ($route->getOption('i18n_locales') ?: $this->locales as $locale) { | |
if ($locale == 'zh_TW'){ | |
$i18nPattern = '/t' . $route->getPattern(); | |
} else { | |
$i18nPattern = $route->getPattern(); | |
} | |
$patterns[$i18nPattern][] = $locale; | |
} | |
return $patterns; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Hypebeast\DesktopBundle\Router; | |
use Symfony\Component\Routing\RouteCollection; | |
use Symfony\Component\Translation\TranslatorInterface; | |
use Symfony\Component\Routing\Route; | |
use JMS\I18nRoutingBundle\Exception\NotAcceptableLanguageException; | |
use JMS\I18nRoutingBundle\Router\I18nRouter; | |
class Router extends I18nRouter | |
{ | |
public function setHostMap(array $hostMap) | |
{ | |
$hostMap['zh_TW'] = $hostMap['zh_CN']; | |
parent::setHostMap($hostMap); | |
} | |
public function match($url) | |
{ | |
try { | |
$result = parent::match($url); | |
} catch (NotAcceptableLanguageException $ex) { | |
if (substr($url, 0, 2) == '/t') { | |
$url = substr($url, 2); | |
$url = $url ? $url : '/'; | |
$result = parent::match($url); | |
$result['_locale'] = 'zh_TW'; | |
} else { | |
throw $ex; | |
} | |
} | |
return $result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment