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

Skip to content

Commit f222f10

Browse files
committed
Merge pull request lunetics-org#142 from coudenysj/feature/domain-locale-guesser
Added a DomainLocaleGuesser
2 parents 191e2ce + 5120918 commit f222f10

9 files changed

Lines changed: 299 additions & 0 deletions

File tree

DependencyInjection/Configuration.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,14 @@ public function getConfigTreeBuilder()
111111
->end()
112112
->end()
113113
->end()
114+
->arrayNode('domain_guesser')
115+
->addDefaultsIfNotSet()
116+
->children()
117+
->scalarNode('class')
118+
->defaultValue('Lunetics\LocaleBundle\LocaleGuesser\DomainLocaleGuesser')
119+
->end()
120+
->end()
121+
->end()
114122
->arrayNode('cookie')
115123
->addDefaultsIfNotSet()
116124
->children()
@@ -151,6 +159,16 @@ public function getConfigTreeBuilder()
151159
->end()
152160
->end()
153161
->end()
162+
->arrayNode('domain')
163+
->addDefaultsIfNotSet()
164+
->children()
165+
->arrayNode('locale_map')
166+
->normalizeKeys(false)
167+
->useAttributeAsKey('name')
168+
->prototype('scalar')->end()
169+
->end()
170+
->end()
171+
->end()
154172
->arrayNode('form')
155173
->addDefaultsIfNotSet()
156174
->children()
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
/**
3+
* This file is part of the LuneticsLocaleBundle package.
4+
*
5+
* <https://github.com/lunetics/LocaleBundle/>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that is distributed with this source code.
9+
*/
10+
namespace Lunetics\LocaleBundle\LocaleGuesser;
11+
12+
use Lunetics\LocaleBundle\LocaleInformation\DomainLocaleMap;
13+
use Symfony\Component\HttpFoundation\Request;
14+
use Lunetics\LocaleBundle\Validator\MetaValidator;
15+
16+
/**
17+
* Locale Guesser for detecting the locale from the domain
18+
*
19+
* @author Jachim Coudenys <[email protected]>
20+
*/
21+
class DomainLocaleGuesser extends AbstractLocaleGuesser
22+
{
23+
/**
24+
* @var MetaValidator
25+
*/
26+
private $metaValidator;
27+
28+
/**
29+
* @var DomainLocaleMap
30+
*/
31+
private $domainLocaleMap;
32+
33+
/**
34+
* @param MetaValidator $metaValidator
35+
* @param DomainLocaleMap $domainLocaleMap
36+
*/
37+
public function __construct(MetaValidator $metaValidator, DomainLocaleMap $domainLocaleMap)
38+
{
39+
$this->metaValidator = $metaValidator;
40+
$this->domainLocaleMap = $domainLocaleMap;
41+
}
42+
43+
/**
44+
* Guess the locale based on the domain
45+
*
46+
* @param Request $request
47+
*
48+
* @return bool
49+
*/
50+
public function guessLocale(Request $request)
51+
{
52+
$domainParts = array_reverse(explode('.', $request->getHost()));
53+
54+
$domain = null;
55+
foreach ($domainParts as $domainPart) {
56+
if (null === $domain) {
57+
$domain = $domainPart;
58+
} else {
59+
$domain = $domainPart . '.' . $domain;
60+
}
61+
62+
if (($locale = $this->domainLocaleMap->getLocale($domain))
63+
&& $this->metaValidator->isAllowed($locale)
64+
) {
65+
$this->identifiedLocale = $locale;
66+
return true;
67+
}
68+
}
69+
70+
return false;
71+
}
72+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
/**
3+
* This file is part of the LuneticsLocaleBundle package.
4+
*
5+
* <https://github.com/lunetics/LocaleBundle/>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that is distributed with this source code.
9+
*/
10+
namespace Lunetics\LocaleBundle\LocaleInformation;
11+
12+
/**
13+
* @author Jachim Coudenys <[email protected]>
14+
*/
15+
class DomainLocaleMap
16+
{
17+
/**
18+
* @var array
19+
*/
20+
private $map = array();
21+
22+
/**
23+
* @param array $map domain locale map, [version.tld => locale, sub.version2.tld => locale]
24+
*/
25+
function __construct(array $map = array())
26+
{
27+
$this->map = $map;
28+
}
29+
30+
/**
31+
* Get the locale for a given domain.
32+
*
33+
* @param string $domain
34+
*
35+
* @return string|bool
36+
*/
37+
public function getLocale($domain)
38+
{
39+
if (isset($this->map[$domain]) && $this->map[$domain]) {
40+
return $this->map[$domain];
41+
}
42+
return false;
43+
}
44+
45+
}

Resources/config/guessers.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,11 @@
4545
<tag name="lunetics_locale.guesser" alias="topleveldomain"/>
4646
</service>
4747

48+
<service id="lunetics_locale.domain_guesser" class="%lunetics_locale.domain_guesser.class%">
49+
<argument type="service" id="lunetics_locale.validator.meta" />
50+
<argument type="service" id="lunetics_locale.domain_locale_map" />
51+
<tag name="lunetics_locale.guesser" alias="domain"/>
52+
</service>
53+
4854
</services>
4955
</container>

Resources/config/services.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<parameter key="lunetics_locale.best_locale_matcher.class">Lunetics\LocaleBundle\Matcher\DefaultBestLocaleMatcher</parameter>
1414
<parameter key="lunetics_locale.allowed_locales_provider.class">Lunetics\LocaleBundle\LocaleInformation\AllowedLocalesProvider</parameter>
1515
<parameter key="lunetics_locale.topleveldomain_locale_map.class">Lunetics\LocaleBundle\LocaleInformation\TopleveldomainLocaleMap</parameter>
16+
<parameter key="lunetics_locale.domain_locale_map.class">Lunetics\LocaleBundle\LocaleInformation\DomainLocaleMap</parameter>
1617
</parameters>
1718

1819
<services>
@@ -82,5 +83,9 @@
8283
<argument>%lunetics_locale.topleveldomain.locale_map%</argument>
8384
</service>
8485

86+
<service id="lunetics_locale.domain_locale_map" class="%lunetics_locale.domain_locale_map.class%">
87+
<argument>%lunetics_locale.domain.locale_map%</argument>
88+
</service>
89+
8590
</services>
8691
</container>

Resources/doc/index.markdown

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,18 @@ topleveldomain:
168168
- be: fr_BE
169169
```
170170

171+
### Domain
172+
173+
The domain guesser will map a domain to a locale.
174+
175+
``` yaml
176+
domain:
177+
locale_map:
178+
- dutchversion.be: nl_BE
179+
- frenchversion.be: fr_BE
180+
- dutchversion.nl: nl_NL
181+
```
182+
171183
### FilterLocaleSwitchEvent / LocaleUpdateListener
172184
The `LocaleGuesserManager` dispatches a `LocaleBundleEvents::onLocalChange` if you use either the `session` or `cookie` guesser. The LocaleUpdateListeners checks if the locale has changed and updates the session or cookie.
173185

Tests/DependencyInjection/LuneticsLocaleExtensionTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ public function testLoad($configs, $strictMatch)
3434

3535
$this->assertArrayHasKey('be', $container->getParameter('lunetics_locale.topleveldomain.locale_map'));
3636

37+
$this->assertTrue($container->hasParameter('lunetics_locale.domain.locale_map'));
38+
$this->assertArrayHasKey('sub.dutchversion.be', $container->getParameter('lunetics_locale.domain.locale_map'));
39+
$this->assertArrayHasKey('dutchversion.be', $container->getParameter('lunetics_locale.domain.locale_map'));
40+
$this->assertArrayHasKey('dutch-version.be', $container->getParameter('lunetics_locale.domain.locale_map'));
41+
3742
if (extension_loaded('intl')) {
3843
$this->assertEquals(array(), $container->getParameter('lunetics_locale.intl_extension_fallback.iso3166'));
3944
$this->assertEquals(array(), $container->getParameter('lunetics_locale.intl_extension_fallback.iso639'));
@@ -108,6 +113,12 @@ public function getFullConfig()
108113
locale_map:
109114
com: en_US
110115
be: nl_BE
116+
domain:
117+
locale_map:
118+
sub.dutchversion.be: en_BE
119+
frechversion.be: fr_BE
120+
dutchversion.be: nl_BE
121+
dutch-version.be: nl_BE
111122
EOF;
112123
$data[]=array($parser->parse($yaml), false);
113124

@@ -128,6 +139,12 @@ public function getFullConfig()
128139
locale_map:
129140
com: en_US
130141
be: nl_BE
142+
domain:
143+
locale_map:
144+
sub.dutchversion.be: en_BE
145+
frechversion.be: fr_BE
146+
dutchversion.be: nl_BE
147+
dutch-version.be: nl_BE
131148
EOF;
132149
$data[]=array($parser->parse($yaml), true);
133150

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
/**
3+
* This file is part of the LuneticsLocaleBundle package.
4+
*
5+
* <https://github.com/lunetics/LocaleBundle/>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that is distributed with this source code.
9+
*/
10+
namespace Lunetics\LocaleBundle\Tests\LocaleGuesser;
11+
12+
use Lunetics\LocaleBundle\LocaleGuesser\DomainLocaleGuesser;
13+
14+
/**
15+
* @author Jachim Coudenys <[email protected]>
16+
*/
17+
class DomainLocaleGuesserTest extends \PHPUnit_Framework_TestCase
18+
{
19+
/**
20+
* @dataProvider dataDomains
21+
*
22+
* @param bool $expected
23+
* @param string $host
24+
* @param bool $allowed
25+
* @param string $mappedLocale
26+
*/
27+
public function testGuessLocale($expected, $expectedLocale, $host, $allowed, $mappedLocale)
28+
{
29+
//$this->markTestSkipped();
30+
$metaValidator = $this->getMockMetaValidator();
31+
$localeMap = $this->getMockDomainLocaleMap();
32+
$localeMap->expects($this->any())
33+
->method('getLocale')
34+
->will($this->returnValue($mappedLocale));
35+
36+
if ($allowed) {
37+
$metaValidator->expects($this->once())
38+
->method('isAllowed')
39+
->will($this->returnValue($allowed));
40+
}
41+
42+
$request = $this->getMockRequest();
43+
$request->expects($this->once())
44+
->method('getHost')
45+
->will($this->returnValue($host));
46+
47+
$guesser = new DomainLocaleGuesser($metaValidator, $localeMap);
48+
49+
$this->assertEquals($expected, $guesser->guessLocale($request));
50+
$this->assertEquals($expectedLocale, $guesser->getIdentifiedLocale());
51+
}
52+
53+
/**
54+
* @return array
55+
*/
56+
public function dataDomains()
57+
{
58+
return array(
59+
array(false, false, 'localhost', false, false),
60+
array(true, 'nl_BE', 'dutchversion.be', true, 'nl_BE'),
61+
array(true, 'en_BE', 'sub.dutchversion.be', true, 'en_BE'),
62+
array(true, 'fr_BE', 'frenchversion.be', true, 'fr_BE'),
63+
array(true, 'fr_BE', 'test.frenchversion.be', true, 'fr_BE'),
64+
//array(true, 'de_CH', 'domain.ch', true, 'de_CH'),
65+
);
66+
}
67+
68+
private function getMockDomainLocaleMap()
69+
{
70+
return $this
71+
->getMockBuilder('\Lunetics\LocaleBundle\LocaleInformation\DomainLocaleMap')
72+
->disableOriginalConstructor()
73+
->getMock();
74+
}
75+
76+
private function getMockMetaValidator()
77+
{
78+
return $this
79+
->getMockBuilder('\Lunetics\LocaleBundle\Validator\MetaValidator')
80+
->disableOriginalConstructor()
81+
->getMock();
82+
}
83+
84+
private function getMockRequest()
85+
{
86+
return $this->getMock('Symfony\Component\HttpFoundation\Request');
87+
}
88+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
/**
3+
* This file is part of the LuneticsLocaleBundle package.
4+
*
5+
* <https://github.com/lunetics/LocaleBundle/>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that is distributed with this source code.
9+
*/
10+
namespace Lunetics\LocaleBundle\Tests\LocaleInformation;
11+
12+
use Lunetics\LocaleBundle\LocaleInformation\DomainLocaleMap;
13+
14+
/**
15+
* @author Ivo Bathke <[email protected]>
16+
*/
17+
class DomainLocaleMapTest extends \PHPUnit_Framework_TestCase
18+
{
19+
public function testGetLocale()
20+
{
21+
$domainLocaleMap = new DomainLocaleMap(
22+
array(
23+
'sub.dutchversion.be' => 'en_BE',
24+
'dutchversion.be' => 'nl_BE',
25+
'spanishversion.be' => null,
26+
'frenchversion.be' => 'fr_BE'
27+
)
28+
);
29+
30+
$this->assertEquals('en_BE', $domainLocaleMap->getLocale('sub.dutchversion.be'));
31+
$this->assertEquals('nl_BE', $domainLocaleMap->getLocale('dutchversion.be'));
32+
$this->assertEquals(false, $domainLocaleMap->getLocale('spanishversion.be'));
33+
$this->assertEquals(false, $domainLocaleMap->getLocale('unknown.be'));
34+
$this->assertEquals('fr_BE', $domainLocaleMap->getLocale('frenchversion.be'));
35+
}
36+
}

0 commit comments

Comments
 (0)