Skip to content

Commit

Permalink
updated for Symfony2.1 locale changes (still compatible with Symfony2.0)
Browse files Browse the repository at this point in the history
  • Loading branch information
schmittjoh committed Oct 27, 2011
1 parent a08f02e commit ad91318
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 7 deletions.
9 changes: 7 additions & 2 deletions EventListener/LocaleChangingListener.php
Expand Up @@ -43,10 +43,15 @@ public function onKernelRequest(GetResponseEvent $event)
return;
}

$host = $event->getRequest()->getHost();
$request = $event->getRequest();
$host = $request->getHost();

if (isset($this->hostMap[$host])) {
$event->getRequest()->getSession()->setLocale($this->hostMap[$host]);
if (method_exists($request, 'setLocale')) {
$request->setLocale($this->hostMap[$host]);
} else {
$request->getSession()->setLocale($this->hostMap[$host]);
}
}
}
}
3 changes: 2 additions & 1 deletion Tests/Functional/CustomStrategyTest.php
Expand Up @@ -29,6 +29,7 @@ public function testDefaultLocaleIsSetCorrectly()

$crawler = $client->request('GET', '/');

$this->assertEquals('de', $crawler->filter('#locale')->text());
$this->assertEquals(1, count($locale = $crawler->filter('#locale')), substr($client->getResponse(), 0, 2000));
$this->assertEquals('de', $locale->text());
}
}
9 changes: 7 additions & 2 deletions Tests/Functional/TestBundle/Controller/DefaultController.php
Expand Up @@ -18,6 +18,8 @@

namespace JMS\I18nRoutingBundle\Tests\Functional\TestBundle\Controller;

use Symfony\Component\HttpFoundation\Request;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
Expand All @@ -28,8 +30,11 @@ class DefaultController
* @Route("/")
* @Template
*/
public function indexAction()
public function indexAction(Request $request)
{
return array();
$locale = method_exists($request, 'getLocale') ? $request->getLocale()
: $request->getSession()->getLocale();

return array('locale' => $locale);
}
}
@@ -1,5 +1,5 @@
{% extends "::base.html.twig" %}

{% block body %}
<div id="locale">{{ app.session.locale }}</div>
<div id="locale">{{ locale }}</div>
{% endblock %}
1 change: 1 addition & 0 deletions Tests/Functional/config/default.yml
@@ -1,4 +1,5 @@
imports:
- { resource: framework.yml }
- { resource: default_locale.php }
- { resource: twig.yml }

11 changes: 11 additions & 0 deletions Tests/Functional/config/default_locale.php
@@ -0,0 +1,11 @@
<?php

use Symfony\Component\HttpKernel\Kernel;

if (version_compare(Kernel::VERSION, '2.1.0-DEV', '<')) {
$config = array('session' => array('default_locale' => 'en'));
} else {
$config = array('default_locale' => 'en');
}

$container->loadFromExtension('framework', $config);
1 change: 0 additions & 1 deletion Tests/Functional/config/framework.yml
Expand Up @@ -2,7 +2,6 @@ framework:
secret: test
test: ~
session:
default_locale: en
storage_id: session.storage.filesystem
form: true
csrf_protection: true
Expand Down
1 change: 1 addition & 0 deletions Tests/Router/I18nRouterTest.php
Expand Up @@ -82,6 +82,7 @@ public function testGenerateDoesUseCorrectHostWhenSchemeChanges()
));

$context = new RequestContext();
$context->setHost('en.test');
$context->setScheme('http');
$context->setParameter('_locale', 'en');
$router->setContext($context);
Expand Down

0 comments on commit ad91318

Please sign in to comment.