|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Victoire\Bundle\I18nBundle\Resolver; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* A class to guess the locale form URL |
|
9
|
|
|
* ref: victoire_i18n.locale_resolver. |
|
10
|
|
|
*/ |
|
11
|
|
|
class LocaleResolver |
|
12
|
|
|
{ |
|
13
|
|
|
const PATTERN_PARAMETER = 'parameter'; // pass the locale the normal way, ie. http://acme.dn/fr |
|
14
|
|
|
const PATTERN_DOMAIN = 'domain'; |
|
15
|
|
|
|
|
16
|
|
|
public $localePattern; |
|
17
|
|
|
protected $localeDomainConfig; |
|
18
|
|
|
protected $availableLocales; |
|
19
|
|
|
public $defaultLocale; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Constructor. |
|
23
|
|
|
* |
|
24
|
|
|
* @param string $localePattern What is the strategy to resolve locale |
|
25
|
|
|
* @param string $localeDomainConfig The locale domain config |
|
26
|
|
|
* @param string $defaultLocale The default local app |
|
27
|
|
|
* @param string $availableLocales The list of available locales |
|
28
|
|
|
*/ |
|
29
|
|
|
public function __construct($localePattern, $localeDomainConfig, $defaultLocale, $availableLocales) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->localePattern = $localePattern; |
|
32
|
|
|
$this->localeDomainConfig = $localeDomainConfig; |
|
33
|
|
|
$this->defaultLocale = $defaultLocale; |
|
34
|
|
|
$this->availableLocales = $availableLocales; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* set the local depending on patterns |
|
39
|
|
|
* it also set the victoire_locale wich is the locale of the application admin. |
|
40
|
|
|
*/ |
|
41
|
|
|
public function resolve(Request $request) |
|
42
|
|
|
{ |
|
43
|
|
|
//locale |
|
44
|
|
|
switch ($this->localePattern) { |
|
45
|
|
|
case self::PATTERN_DOMAIN: |
|
46
|
|
|
$locale = $this->resolveFromDomain($request); |
|
47
|
|
|
$request->setLocale($locale); |
|
48
|
|
|
break; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
return $request->getLocale(); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param Request $request |
|
56
|
|
|
* |
|
57
|
|
|
* @throws \Exception |
|
58
|
|
|
* |
|
59
|
|
|
* @return string |
|
60
|
|
|
* |
|
61
|
|
|
* resolves the locale from httpHost or host |
|
62
|
|
|
*/ |
|
63
|
|
|
public function resolveFromDomain(Request $request) |
|
64
|
|
|
{ |
|
65
|
|
|
$host = $request->getHost(); |
|
66
|
|
|
$httpHost = $request->getHttpHost(); |
|
67
|
|
|
|
|
68
|
|
|
if (array_key_exists($host, $this->localeDomainConfig)) { |
|
69
|
|
|
return $this->localeDomainConfig[$host]; |
|
70
|
|
|
} elseif (array_key_exists($httpHost, $this->localeDomainConfig)) { |
|
71
|
|
|
return $this->localeDomainConfig[$httpHost]; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return $request->getLocale(); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @return string |
|
79
|
|
|
* |
|
80
|
|
|
* This method resolves the domain from locale |
|
81
|
|
|
*/ |
|
82
|
|
|
public function resolveDomainForLocale($locale) |
|
83
|
|
|
{ |
|
84
|
|
|
foreach ($this->localeDomainConfig as $domain => $domainLocale) { |
|
|
|
|
|
|
85
|
|
|
if ($locale === $domainLocale) { |
|
86
|
|
|
return $domain; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
return $this->defaultLocale; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @return string |
|
95
|
|
|
*/ |
|
96
|
|
|
public function getAvailableLocales() |
|
97
|
|
|
{ |
|
98
|
|
|
return $this->availableLocales; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|