Permalink
Browse files

[FrameworkBundle] added the current locale as the default value for _…

…locale when generating routes
  • Loading branch information...
fabpot committed Apr 20, 2011
1 parent c6dcf0f commit 7266b41aded2db6a88f980431752f25688cb204b
Showing with 13 additions and 6 deletions.
  1. +13 −6 src/Symfony/Bundle/FrameworkBundle/RequestListener.php
@@ -77,14 +77,20 @@ protected function initializeRequestAttributes(Request $request, $master)
if ($master) {
// set the context even if the parsing does not need to be done
// to have correct link generation
- $this->router->setContext(new RequestContext(
+ $context = new RequestContext(
$request->getBaseUrl(),
$request->getMethod(),
$request->getHost(),
$request->getScheme(),
$this->httpPort,
$this->httpsPort
- ));
+ );
+
+ if ($session = $request->getSession()) {
+ $context->setParameter('_locale', $session->getLocale());
+ }
+
+ $this->router->setContext($context);
}
if ($request->attributes->has('_controller')) {
@@ -101,10 +107,6 @@ protected function initializeRequestAttributes(Request $request, $master)
}
$request->attributes->add($parameters);
-
- if ($locale = $request->attributes->get('_locale')) {
- $request->getSession()->setLocale($locale);
- }
} catch (NotFoundException $e) {
$message = sprintf('No route found for "%s %s"', $request->getMethod(), $request->getPathInfo());
if (null !== $this->logger) {
@@ -118,6 +120,11 @@ protected function initializeRequestAttributes(Request $request, $master)
}
throw new MethodNotAllowedHttpException($e->getAllowedMethods(), $message, $e);
}
+
+ if ($master && $locale = $request->attributes->get('_locale')) {
+ $request->getSession()->setLocale($locale);
+ $context->setParameter('_locale', $locale);
+ }
}
private function parametersToString(array $parameters)

8 comments on commit 7266b41

Member

Seldaek replied Apr 20, 2011

Yeah! Another small routing patch that removes a big pain point :)

Member

weaverryan replied Apr 20, 2011

Awesome! I've gotten this request steadily for awhile now.

Owner

fabpot replied Apr 20, 2011

This patch is indeed small, but just because of the routing refactoring I did today ;)

Contributor

benjamindulau replied Apr 20, 2011

Great patch ! However, now, all generated links in templates contain "?_locale=en" (generated through the path() twig function), is that on purpose ?

Contributor

benjamindulau replied Apr 20, 2011

To prevent what i described in my previous comment, all routes must now explicitly define a default value for _locale in their definition.

Owner

fabpot replied Apr 21, 2011

@benjamindulau: this is indeed a bug.

Owner

fabpot replied Apr 21, 2011

This is fixed now.

Awesome. Thank you !

Please sign in to comment.