diff --git a/Resources/config/routing.xml b/Resources/config/routing.xml index 9cad97d98..ea12c693d 100644 --- a/Resources/config/routing.xml +++ b/Resources/config/routing.xml @@ -71,6 +71,7 @@ + %kernel.default_locale% diff --git a/Routing/Router.php b/Routing/Router.php index 6bea82e1e..93e6c3ad8 100644 --- a/Routing/Router.php +++ b/Routing/Router.php @@ -43,7 +43,7 @@ class Router extends BaseRouter implements WarmableInterface, ServiceSubscriberI * @param ContainerInterface|null $parameters A ContainerInterface instance allowing to fetch parameters * @param LoggerInterface|null $logger */ - public function __construct(ContainerInterface $container, $resource, array $options = [], RequestContext $context = null, ContainerInterface $parameters = null, LoggerInterface $logger = null) + public function __construct(ContainerInterface $container, $resource, array $options = [], RequestContext $context = null, ContainerInterface $parameters = null, LoggerInterface $logger = null, string $defaultLocale = null) { $this->container = $container; $this->resource = $resource; @@ -58,6 +58,8 @@ public function __construct(ContainerInterface $container, $resource, array $opt } else { throw new \LogicException(sprintf('You should either pass a "%s" instance or provide the $parameters argument of the "%s" method.', SymfonyContainerInterface::class, __METHOD__)); } + + $this->defaultLocale = $defaultLocale; } /** diff --git a/Tests/Routing/RouterTest.php b/Tests/Routing/RouterTest.php index 329f1e7cb..3f04e46dd 100644 --- a/Tests/Routing/RouterTest.php +++ b/Tests/Routing/RouterTest.php @@ -82,6 +82,32 @@ public function testGenerateWithServiceParamWithSfContainer() $this->assertSame('"bar" == "bar"', $router->getRouteCollection()->get('foo')->getCondition()); } + public function testGenerateWithDefaultLocale() + { + $routes = new RouteCollection(); + + $route = new Route(''); + + $name = 'testFoo'; + + foreach (['hr' => '/test-hr', 'en' => '/test-en'] as $locale => $path) { + $localizedRoute = clone $route; + $localizedRoute->setDefault('_locale', $locale); + $localizedRoute->setDefault('_canonical_route', $name); + $localizedRoute->setPath($path); + $routes->add($name.'.'.$locale, $localizedRoute); + } + + $sc = $this->getServiceContainer($routes); + + $router = new Router($sc, '', [], null, null, null, 'hr'); + + $this->assertSame('/test-hr', $router->generate($name)); + + $this->assertSame('/test-en', $router->generate($name, ['_locale' => 'en'])); + $this->assertSame('/test-hr', $router->generate($name, ['_locale' => 'hr'])); + } + public function testDefaultsPlaceholders() { $routes = new RouteCollection(); diff --git a/composer.json b/composer.json index 44bb6b039..9921bf6a7 100644 --- a/composer.json +++ b/composer.json @@ -28,7 +28,7 @@ "symfony/polyfill-mbstring": "~1.0", "symfony/filesystem": "~3.4|~4.0", "symfony/finder": "~3.4|~4.0", - "symfony/routing": "^4.1" + "symfony/routing": "^4.2.6" }, "require-dev": { "doctrine/cache": "~1.0",