1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\Middleware; |
6
|
|
|
|
7
|
|
|
use DateInterval; |
8
|
|
|
use Psr\Http\Message\ResponseFactoryInterface; |
9
|
|
|
use Psr\Http\Message\ResponseInterface; |
10
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
11
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
12
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
13
|
|
|
use Psr\Log\LoggerInterface; |
14
|
|
|
use Yiisoft\Cookies\Cookie; |
15
|
|
|
use Yiisoft\Http\Header; |
16
|
|
|
use Yiisoft\Http\Status; |
17
|
|
|
use Yiisoft\Router\UrlGeneratorInterface; |
18
|
|
|
use Yiisoft\Session\SessionInterface; |
19
|
|
|
use Yiisoft\Translator\TranslatorInterface; |
20
|
|
|
|
21
|
|
|
final class Locale implements MiddlewareInterface |
22
|
|
|
{ |
23
|
|
|
private const DEFAULT_LOCALE = 'en'; |
24
|
|
|
private const DEFAULT_LOCALE_NAME = '_language'; |
25
|
|
|
|
26
|
|
|
private bool $enableSaveLocale = true; |
27
|
|
|
private bool $enableDetectLocale = false; |
28
|
|
|
private string $defaultLocale = self::DEFAULT_LOCALE; |
29
|
|
|
private string $queryParameterName = self::DEFAULT_LOCALE_NAME; |
30
|
|
|
private string $sessionName = self::DEFAULT_LOCALE_NAME; |
31
|
|
|
private ?DateInterval $cookieDuration; |
32
|
|
|
|
33
|
|
|
public function __construct( |
34
|
|
|
private TranslatorInterface $translator, |
35
|
|
|
private UrlGeneratorInterface $urlGenerator, |
36
|
|
|
private SessionInterface $session, |
37
|
|
|
private LoggerInterface $logger, |
38
|
|
|
private ResponseFactoryInterface $responseFactory, |
39
|
|
|
private array $locales = [], |
40
|
|
|
private bool $cookieSecure = false |
41
|
|
|
) { |
42
|
|
|
$this->cookieDuration = new DateInterval('P30D'); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
46
|
|
|
{ |
47
|
|
|
if ($this->locales === []) { |
48
|
|
|
return $handler->handle($request); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$uri = $request->getUri(); |
52
|
|
|
$path = $uri->getPath(); |
53
|
|
|
[$locale, $country] = $this->getLocaleFromPath($path); |
54
|
|
|
|
55
|
|
|
if ($locale !== null) { |
56
|
|
|
$response = $handler->handle($request); |
57
|
|
|
$newPath = null; |
58
|
|
|
if ($this->isDefaultLocale($locale, $country) && $request->getMethod() === 'GET') { |
59
|
|
|
$length = strlen($locale); |
60
|
|
|
$newPath = substr($path, $length + 1); |
61
|
|
|
} |
62
|
|
|
return $this->applyLocaleFromPath($locale, $response, $newPath); |
63
|
|
|
} |
64
|
|
|
if ($this->enableSaveLocale) { |
65
|
|
|
[$locale, $country] = $this->getLocaleFromRequest($request); |
66
|
|
|
} |
67
|
|
|
if ($locale === null && $this->enableDetectLocale) { |
68
|
|
|
[$locale, $country] = $this->detectLocale($request); |
69
|
|
|
} |
70
|
|
|
if ($locale === null || $this->isDefaultLocale($locale, $country)) { |
71
|
|
|
$this->urlGenerator->setDefaultArgument($this->queryParameterName, null); |
72
|
|
|
$request = $request->withUri($uri->withPath('/' . $this->defaultLocale . $path)); |
73
|
|
|
return $handler->handle($request); |
74
|
|
|
} |
75
|
|
|
$this->urlGenerator->setDefaultArgument($this->queryParameterName, $locale); |
76
|
|
|
|
77
|
|
|
if ($request->getMethod() === 'GET') { |
78
|
|
|
return $this->responseFactory |
79
|
|
|
->createResponse(Status::FOUND) |
80
|
|
|
->withHeader(Header::LOCATION, '/' . $locale . $path); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $handler->handle($request); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
private function applyLocaleFromPath( |
87
|
|
|
string $locale, |
88
|
|
|
ResponseInterface $response, |
89
|
|
|
?string $newPath = null |
90
|
|
|
): ResponseInterface { |
91
|
|
|
if ($newPath === '') { |
92
|
|
|
$newPath = '/'; |
93
|
|
|
} |
94
|
|
|
$this->translator->setLocale($locale); |
95
|
|
|
$this->urlGenerator->setDefaultArgument($this->queryParameterName, $locale); |
96
|
|
|
|
97
|
|
|
if ($newPath !== null) { |
98
|
|
|
$response = $this->responseFactory |
99
|
|
|
->createResponse(Status::FOUND) |
100
|
|
|
->withHeader(Header::LOCATION, $newPath); |
101
|
|
|
} |
102
|
|
|
if ($this->enableSaveLocale) { |
103
|
|
|
$response = $this->saveLocale($locale, $response); |
104
|
|
|
} |
105
|
|
|
return $response; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
private function getLocaleFromPath(string $path): array |
109
|
|
|
{ |
110
|
|
|
$parts = []; |
111
|
|
|
foreach ($this->locales as $code => $locale) { |
112
|
|
|
$lang = is_string($code) ? $code : $locale; |
113
|
|
|
$parts[] = $lang; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
$pattern = implode('|', $parts); |
117
|
|
|
if (preg_match("#^/($pattern)\b(/?)#i", $path, $matches)) { |
118
|
|
|
$locale = $matches[1]; |
119
|
|
|
[$locale, $country] = $this->parseLocale($locale); |
120
|
|
|
if (isset($this->locales[$locale])) { |
121
|
|
|
$this->logger->info(sprintf("Locale '%s' found in URL", $locale)); |
122
|
|
|
return [$locale, $country]; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
return [null, null]; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
private function getLocaleFromRequest(ServerRequestInterface $request): array |
129
|
|
|
{ |
130
|
|
|
$cookies = $request->getCookieParams(); |
131
|
|
|
$queryParameters = $request->getQueryParams(); |
132
|
|
|
if (isset($cookies[$this->sessionName])) { |
133
|
|
|
$this->logger->info(sprintf("Locale '%s' found in cookies", $cookies[$this->sessionName])); |
134
|
|
|
return $this->parseLocale($cookies[$this->sessionName]); |
135
|
|
|
} |
136
|
|
|
if (isset($queryParameters[$this->queryParameterName])) { |
137
|
|
|
$this->logger->info( |
138
|
|
|
sprintf("Locale '%s' found in query string", $queryParameters[$this->queryParameterName]) |
139
|
|
|
); |
140
|
|
|
return $this->parseLocale($queryParameters[$this->queryParameterName]); |
141
|
|
|
} |
142
|
|
|
return [null, null]; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
private function isDefaultLocale(string $locale, ?string $country): bool |
146
|
|
|
{ |
147
|
|
|
return $locale === $this->defaultLocale || ($country !== null && $this->defaultLocale === "$locale-$country"); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
private function detectLocale(ServerRequestInterface $request): array |
151
|
|
|
{ |
152
|
|
|
foreach ($request->getHeader(Header::ACCEPT_LANGUAGE) as $language) { |
153
|
|
|
return $this->parseLocale($language); |
154
|
|
|
} |
155
|
|
|
return [null, null]; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
private function saveLocale(string $locale, ResponseInterface $response): ResponseInterface |
159
|
|
|
{ |
160
|
|
|
$this->logger->info('Saving found locale to cookies'); |
161
|
|
|
$this->session->set($this->sessionName, $locale); |
162
|
|
|
$cookie = new Cookie(name: $this->sessionName, value: $locale, secure: $this->cookieSecure); |
163
|
|
|
if ($this->cookieDuration !== null) { |
164
|
|
|
$cookie = $cookie->withMaxAge($this->cookieDuration); |
165
|
|
|
} |
166
|
|
|
return $cookie->addToResponse($response); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
private function parseLocale(string $locale): array |
170
|
|
|
{ |
171
|
|
|
if (str_contains($locale, '-')) { |
172
|
|
|
return explode('-', $locale, 2); |
173
|
|
|
} |
174
|
|
|
if (isset($this->locales[$locale]) && str_contains($this->locales[$locale], '-')) { |
175
|
|
|
return explode('-', $this->locales[$locale], 2); |
176
|
|
|
} |
177
|
|
|
return [$locale, null]; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
public function withLocales(array $locales): self |
181
|
|
|
{ |
182
|
|
|
$new = clone $this; |
183
|
|
|
$new->locales = $locales; |
184
|
|
|
return $new; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
public function withDefaultLocale(string $defaultLocale): self |
188
|
|
|
{ |
189
|
|
|
$new = clone $this; |
190
|
|
|
$new->defaultLocale = $defaultLocale; |
191
|
|
|
return $new; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
public function withQueryParameterName(string $queryParameterName): self |
195
|
|
|
{ |
196
|
|
|
$new = clone $this; |
197
|
|
|
$new->queryParameterName = $queryParameterName; |
198
|
|
|
return $new; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
public function withSessionName(string $sessionName): self |
202
|
|
|
{ |
203
|
|
|
$new = clone $this; |
204
|
|
|
$new->sessionName = $sessionName; |
205
|
|
|
return $new; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
public function withEnableSaveLocale(bool $enableSaveLocale): self |
209
|
|
|
{ |
210
|
|
|
$new = clone $this; |
211
|
|
|
$new->enableDetectLocale = $enableSaveLocale; |
212
|
|
|
return $new; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
public function withEnableDetectLocale(bool $enableDetectLocale): self |
216
|
|
|
{ |
217
|
|
|
$new = clone $this; |
218
|
|
|
$new->enableDetectLocale = $enableDetectLocale; |
219
|
|
|
return $new; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
public function withCookieSecure(bool $secure): self |
223
|
|
|
{ |
224
|
|
|
$new = clone $this; |
225
|
|
|
$new->cookieSecure = $secure; |
226
|
|
|
return $new; |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
|