1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Vluzrmos\LanguageDetector; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use Illuminate\Contracts\Translation\Translator as IlluminateTranslator; |
7
|
|
|
use Symfony\Component\Translation\TranslatorInterface as SymfonyTranslator; |
8
|
|
|
use Vluzrmos\LanguageDetector\Contracts\DetectorDriverInterface as Driver; |
9
|
|
|
use Vluzrmos\LanguageDetector\Contracts\LanguageDetectorInterface; |
10
|
|
|
use Vluzrmos\LanguageDetector\Contracts\ShouldPrefixRoutesInterface as ShouldPrefixRoute; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class LanguageDetector. |
14
|
|
|
*/ |
15
|
|
|
class LanguageDetector implements LanguageDetectorInterface |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* Translator instance. |
19
|
|
|
* @var SymfonyTranslator|IlluminateTranslator |
20
|
|
|
*/ |
21
|
|
|
protected $translator; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Driver to detect and apply the language. |
25
|
|
|
* @var Driver |
26
|
|
|
*/ |
27
|
|
|
protected $driver; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
protected $callbacks = []; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Indicates cookie name or false to do not use cookies. |
36
|
|
|
* @var string|false|null |
37
|
|
|
*/ |
38
|
|
|
protected $cookie; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param SymfonyTranslator|IlluminateTranslator $translator |
42
|
|
|
* @param Driver $driver |
43
|
|
|
*/ |
44
|
|
|
public function __construct($translator, Driver $driver = null) |
45
|
|
|
{ |
46
|
|
|
if (!$translator instanceof SymfonyTranslator && !$translator instanceof IlluminateTranslator) { |
47
|
|
|
throw new \InvalidArgumentException("Translator must implement the 'Symfony\\Component\\Translation\\TranslatorInterface' or 'Illuminate\\Contracts\\Translation\\Translator' interface."); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$this->translator = $translator; |
51
|
|
|
$this->driver = $driver; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Detect and apply the detected language. |
56
|
|
|
* |
57
|
|
|
* @return string|null Returns the detected locale or null. |
58
|
|
|
*/ |
59
|
|
|
public function detectAndApply() |
60
|
|
|
{ |
61
|
|
|
$language = $this->detect(); |
|
|
|
|
62
|
|
|
|
63
|
|
|
if ($language) { |
64
|
|
|
$this->apply($language); |
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $language; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Detect the language. |
72
|
|
|
* |
73
|
|
|
* @return string |
74
|
|
|
*/ |
75
|
|
|
public function detect() |
76
|
|
|
{ |
77
|
|
|
return $this->getLanguageFromCookie() ?: $this->getDriver()->detect(); |
|
|
|
|
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
|
|
public function getLanguageFromCookie() |
84
|
|
|
{ |
85
|
|
|
if ($this->cookie) { |
86
|
|
|
/** @var \Illuminate\Http\Request $request */ |
87
|
|
|
$request = $this->getDriver()->getRequest(); |
88
|
|
|
|
89
|
|
|
return $request->cookie($this->cookie); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return false; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Add cookie with locale to queue. |
97
|
|
|
* |
98
|
|
|
* @param string $locale |
99
|
|
|
*/ |
100
|
|
|
public function addCookieToQueue($locale) |
101
|
|
|
{ |
102
|
|
|
if ($this->cookie) { |
103
|
|
|
/** @var \Illuminate\Cookie\CookieJar $cookieJar */ |
104
|
|
|
$cookieJar = cookie(); |
105
|
|
|
|
106
|
|
|
$cookieJar->queue($cookieJar->forever($this->cookie, $locale)); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Determine if should use cookies. |
112
|
|
|
* false or null will disable feature, string will set cookie name. |
113
|
|
|
* |
114
|
|
|
* @param string|bool|null $cookieName |
115
|
|
|
*/ |
116
|
|
|
public function useCookies($cookieName = 'locale') |
117
|
|
|
{ |
118
|
|
|
$this->cookie = empty($cookieName) ? false : $cookieName; |
|
|
|
|
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Get the driver. |
123
|
|
|
* |
124
|
|
|
* @return Driver |
125
|
|
|
*/ |
126
|
|
|
public function getDriver() |
127
|
|
|
{ |
128
|
|
|
return $this->driver; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Set driver to detect language. |
133
|
|
|
* |
134
|
|
|
* @param Driver $driver |
135
|
|
|
*/ |
136
|
|
|
public function setDriver(Driver $driver) |
137
|
|
|
{ |
138
|
|
|
$this->driver = $driver; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Set locale to the application. |
143
|
|
|
* |
144
|
|
|
* @param string $locale |
145
|
|
|
*/ |
146
|
|
|
public function apply($locale) |
147
|
|
|
{ |
148
|
|
|
$this->translator->setLocale($locale); |
149
|
|
|
|
150
|
|
|
if (!$this->getLanguageFromCookie()) { |
151
|
|
|
$this->addCookieToQueue($locale); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
$this->applyCallbacks($locale); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Add a callback to call after applying the detected locale. |
159
|
|
|
* @param Closure $callback |
160
|
|
|
*/ |
161
|
|
|
public function addCallback(Closure $callback) |
162
|
|
|
{ |
163
|
|
|
$this->callbacks[] = $callback; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Call all registered callbacks. |
168
|
|
|
* |
169
|
|
|
* @param string $language |
170
|
|
|
*/ |
171
|
|
|
protected function applyCallbacks($language) |
172
|
|
|
{ |
173
|
|
|
foreach ($this->callbacks as $callback) { |
174
|
|
|
call_user_func($callback, $language, $this); |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Get the route prefix. |
180
|
|
|
* |
181
|
|
|
* @return string |
182
|
|
|
*/ |
183
|
|
|
public function routePrefix() |
184
|
|
|
{ |
185
|
|
|
$driver = $this->getDriver(); |
186
|
|
|
|
187
|
|
|
if ($driver instanceof ShouldPrefixRoute) { |
188
|
|
|
return $driver->routePrefix($this->translator->getLocale()); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
return ''; |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|