Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b63ad17

Browse files
committed
Fix the TargetInformationBuilder
1 parent 0451aab commit b63ad17

3 files changed

Lines changed: 133 additions & 7 deletions

File tree

Switcher/TargetInformationBuilder.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,18 @@ public function __construct($route = null)
4747
* @param array $parameters
4848
* @return array Informations for the switcher template
4949
*/
50-
public function getTargetInformations(Request $request, RouterInterface $router, $allowedLocales, $parameters = array())
50+
public function getTargetInformations(Request $request, RouterInterface $router, $allowedLocales, $showCurrentLocale = false, $parameters = array())
5151
{
5252
$infos = array();
5353
$route = null !== $this->route ? $this->route : $request->attributes->get('_route');
5454
$infos['current_locale'] = $request->getLocale();
5555
$infos['current_route'] = $route;
5656
$targetLocales = $allowedLocales;
57-
$parameters = array_merge($request->attributes->get('_route_params'), $request->query->all(), $parameters);
57+
$parameters = array_merge((array) $request->attributes->get('_route_params'), $request->query->all(), (array) $parameters);
5858

5959
foreach ($targetLocales as $locale) {
60-
// No need to build route and locale names for current locale
61-
if (0 !== strpos($request->getLocale(), $locale)) {
60+
$strpos = 0 === strpos($request->getLocale(), $locale);
61+
if ($showCurrentLocale && $strpos || !$strpos) {
6262
$targetLocaleTargetLang = Locale::getDisplayLanguage($locale, $locale);
6363
$targetLocaleCurrentLang = Locale::getDisplayLanguage($locale, $request->getLocale());
6464
$parameters['_locale'] = $locale;
@@ -74,7 +74,7 @@ public function getTargetInformations(Request $request, RouterInterface $router,
7474
'locale_target_language' => $targetLocaleTargetLang,
7575
'link' => $targetRoute,
7676
'locale' => $locale,
77-
);
77+
);
7878
}
7979
}
8080

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
<?php
2+
3+
namespace Lunetics\LocaleBundle\Tests\Validator;
4+
5+
use Lunetics\LocaleBundle\Switcher\TargetInformationBuilder;
6+
use Symfony\Component\HttpFoundation\Request;
7+
use Symfony\Bundle\FrameworkBundle\Routing\Router;
8+
use Symfony\Component\Routing\RouterInterface;
9+
10+
class TargetInformationBuilderTest extends \PHPUnit_Framework_TestCase
11+
{
12+
13+
public function locales()
14+
{
15+
return array(
16+
'set 1' => array('/hello-world/', 'de', array('de', 'en', 'fr')),
17+
'set 2' => array('/', 'de_DE', array('de', 'en', 'fr', 'nl')),
18+
'set 3' => array('/test/', 'de', array('de', 'fr_FR', 'es_ES', 'nl')),
19+
'set 4' => array('/foo', 'de', array('de', 'en')),
20+
'set 5' => array('/foo', 'de', array('de')),
21+
'set 6' => array('/', 'de_DE', array('de_DE', 'en', 'fr', 'nl'))
22+
);
23+
}
24+
25+
/**
26+
* @dataProvider locales
27+
*/
28+
public function testProvideRouteInInformationBuilder($route, $locale, $allowedLocales)
29+
{
30+
$request = $this->getRequestWithBrowserPreferences($route);
31+
$request->setLocale($locale);
32+
$request->attributes->set('_route', $route);
33+
$router = $this->getRoute();
34+
35+
$targetInformationBuilder = new TargetInformationBuilder($route . 'hello');
36+
$targetInformation = $targetInformationBuilder->getTargetInformations(
37+
$request,
38+
$router,
39+
$allowedLocales
40+
);
41+
$this->assertEquals($route . 'hello', $targetInformation['current_route']);
42+
}
43+
44+
/**
45+
* @dataProvider locales
46+
*/
47+
public function testNotProvideRouteInInformationBuilder($route, $locale, $allowedLocales)
48+
{
49+
$request = $this->getRequestWithBrowserPreferences($route);
50+
$request->setLocale($locale);
51+
$request->attributes->set('_route', $route . 'hello');
52+
$router = $this->getRoute();
53+
54+
$targetInformationBuilder = new TargetInformationBuilder();
55+
$targetInformation = $targetInformationBuilder->getTargetInformations(
56+
$request,
57+
$router,
58+
$allowedLocales
59+
);
60+
$this->assertEquals($route . 'hello', $targetInformation['current_route']);
61+
}
62+
63+
/**
64+
* @dataProvider locales
65+
*/
66+
public function testInformationBuilder($route, $locale, $allowedLocales)
67+
{
68+
$request = $this->getRequestWithBrowserPreferences();
69+
$request->setLocale($locale);
70+
$router = $this->getRoute();
71+
72+
$targetInformationBuilder = new TargetInformationBuilder($route);
73+
$targetInformation = $targetInformationBuilder->getTargetInformations(
74+
$request,
75+
$router,
76+
$allowedLocales
77+
);
78+
$this->assertEquals($locale, $targetInformation['current_locale']);
79+
if (count($allowedLocales) > 1) {
80+
$this->assertCount(count($allowedLocales) - 1, $targetInformation['locales']);
81+
$this->assertArrayNotHasKey($locale, $targetInformation['locales']);
82+
} else {
83+
$this->assertArrayNotHasKey('locales', $targetInformation);
84+
}
85+
}
86+
87+
/**
88+
* @dataProvider locales
89+
*/
90+
public function testShowCurrentLocale($route, $locale, $allowedLocales)
91+
{
92+
$request = $this->getRequestWithBrowserPreferences();
93+
$request->setLocale($locale);
94+
$router = $this->getRoute();
95+
96+
$targetInformationBuilder = new TargetInformationBuilder($route);
97+
$targetInformation = $targetInformationBuilder->getTargetInformations(
98+
$request,
99+
$router,
100+
$allowedLocales,
101+
true
102+
);
103+
$this->assertCount(count($allowedLocales), $targetInformation['locales']);
104+
foreach ($allowedLocales as $allowed) {
105+
$this->assertArrayHasKey($allowed, $targetInformation['locales']);
106+
}
107+
}
108+
109+
private function getRequestWithBrowserPreferences($route = "/")
110+
{
111+
$request = Request::create($route);
112+
$request->headers->set('Accept-language', 'fr-FR,fr;q=0.1,en-US;q=0.6,en;q=0.4');
113+
114+
return $request;
115+
}
116+
117+
private function getRoute()
118+
{
119+
return $this->getMockBuilder('Symfony\Component\Routing\RouterInterface')->disableOriginalConstructor()->getMock();
120+
}
121+
}

Twig/Extension/LocaleSwitcherExtension.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,14 @@
1313
use Lunetics\LocaleBundle\Switcher\TargetInformationBuilder;
1414

1515
/**
16-
* @author Christophe Willemsen <[email protected]/>
16+
* @author Christophe Willemsen <[email protected]>
17+
* @author Matthias Breddin <[email protected]>
1718
*/
1819
class LocaleSwitcherExtension extends \Twig_Extension
1920
{
21+
/**
22+
* @var ContainerInterface
23+
*/
2024
protected $container;
2125

2226
/**
@@ -59,8 +63,9 @@ public function renderSwitcher($route = null, $parameters = array())
5963
$request = $this->container->get('request');
6064
$router = $this->container->get('router');
6165
$allowedLocales = $this->container->getParameter('lunetics_locale.allowed_locales');
66+
$showCurrentLocale = $this->container->getParameter('lunetics_locale.switcher.show_current_locale');
6267

63-
$infos = $infosBuilder->getTargetInformations($request, $router, $allowedLocales, $parameters);
68+
$infos = $infosBuilder->getTargetInformations($request, $router, $allowedLocales, $showCurrentLocale, $parameters);
6469

6570
return $this->container->get('lunetics_locale.switcher_helper')->renderSwitch($infos, 'switcher_links.html.twig');
6671
}

0 commit comments

Comments
 (0)