|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Bone\I18n\Http\Middleware; |
|
4
|
|
|
|
|
5
|
|
|
use Locale; |
|
6
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
7
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
8
|
|
|
use Psr\Http\Message\UriInterface; |
|
9
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
|
10
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
|
11
|
|
|
use Laminas\I18n\Translator\Translator; |
|
12
|
|
|
|
|
13
|
|
|
class I18nMiddleware implements MiddlewareInterface |
|
14
|
|
|
{ |
|
15
|
|
|
const REGEX_LOCALE = '#^/(?P<locale>[a-z]{2}[-_][a-zA-Z]{2})(?:/|$)#'; |
|
16
|
|
|
|
|
17
|
|
|
/** @var Translator$translator */ |
|
|
|
|
|
|
18
|
|
|
private $translator; |
|
19
|
|
|
|
|
20
|
|
|
/** @var array $supportedLocales */ |
|
21
|
|
|
private $supportedLocales; |
|
22
|
|
|
|
|
23
|
|
|
/** @var string $defaultLocale */ |
|
24
|
|
|
private $defaultLocale; |
|
25
|
|
|
|
|
26
|
|
|
/** @var bool $enabled */ |
|
27
|
|
|
private $enabled; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* InternationalisationMiddleware constructor. |
|
31
|
|
|
* @param $helper |
|
32
|
|
|
* @param string|null $defaultLocale |
|
33
|
|
|
*/ |
|
34
|
4 |
|
public function __construct(Translator $translator, array $supportedLocales, string $defaultLocale, bool $enabled) |
|
35
|
|
|
{ |
|
36
|
4 |
|
$this->translator = $translator; |
|
37
|
4 |
|
$this->supportedLocales = $supportedLocales; |
|
38
|
4 |
|
$this->defaultLocale = $defaultLocale; |
|
39
|
4 |
|
$this->enabled = $enabled; |
|
40
|
4 |
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param ServerRequestInterface $request |
|
44
|
|
|
* @param RequestHandlerInterface $handler |
|
45
|
|
|
* @return ResponseInterface |
|
46
|
|
|
*/ |
|
47
|
1 |
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
|
48
|
|
|
{ |
|
49
|
1 |
|
$uri = $request->getUri(); |
|
50
|
1 |
|
$path = $uri->getPath(); |
|
51
|
1 |
|
preg_match(self::REGEX_LOCALE, $path, $matches); |
|
52
|
1 |
|
$locale = $this->defaultLocale; |
|
53
|
|
|
|
|
54
|
1 |
|
if (isset($matches['locale'])) { |
|
55
|
1 |
|
$locale = in_array($matches['locale'], $this->supportedLocales) ? $matches['locale'] : $this->defaultLocale; |
|
56
|
1 |
|
$request = $this->stripLocaleFromUri($uri, $path, $locale, $request); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
1 |
|
Locale::canonicalize($locale); |
|
60
|
1 |
|
Locale::setDefault($locale); |
|
61
|
1 |
|
$request = $request->withAttribute('locale', $locale); |
|
62
|
|
|
|
|
63
|
1 |
|
if ($this->enabled === true) { |
|
64
|
1 |
|
$this->translator->setLocale($locale); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
1 |
|
return $handler->handle($request); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param UriInterface $uri |
|
72
|
|
|
* @param string $path |
|
73
|
|
|
* @param string $locale |
|
74
|
|
|
* @param ServerRequestInterface $request |
|
75
|
|
|
* @return ServerRequestInterface |
|
76
|
|
|
*/ |
|
77
|
1 |
|
private function stripLocaleFromUri(UriInterface $uri, string $path, string $locale, ServerRequestInterface $request): ServerRequestInterface |
|
78
|
|
|
{ |
|
79
|
1 |
|
$path = substr($path, strlen($locale) + 1) ?? '/'; |
|
80
|
1 |
|
$uri = $uri->withPath($path); |
|
81
|
1 |
|
$request = $request->withUri($uri); |
|
82
|
|
|
|
|
83
|
1 |
|
return $request; |
|
84
|
|
|
} |
|
85
|
|
|
} |