1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This File is Part of the Validus Translation package. |
4
|
|
|
* |
5
|
|
|
* @copyright (c) 2018 Validus <https://github.com/ValidusPHP/> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
declare(strict_types=1); |
12
|
|
|
|
13
|
|
|
namespace Validus\Translation\Middleware; |
14
|
|
|
|
15
|
|
|
use Negotiation\AcceptLanguage; |
16
|
|
|
use Negotiation\LanguageNegotiator; |
17
|
|
|
use Psr\Http\Message\ResponseInterface; |
18
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
19
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
20
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
21
|
|
|
use Symfony\Component\Translation\Translator; |
22
|
|
|
|
23
|
|
|
class TranslatorMiddleware implements MiddlewareInterface |
24
|
|
|
{ |
25
|
|
|
/** @var Translator $translator */ |
26
|
|
|
protected $translator; |
27
|
|
|
|
28
|
|
|
/** @var LanguageNegotiator $negotiator */ |
29
|
|
|
protected $negotiator; |
30
|
|
|
|
31
|
|
|
/** @var array $priorities */ |
32
|
|
|
protected $priorities; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* LocalizationMiddleware constructor. |
36
|
|
|
* |
37
|
|
|
* @param Translator $translator |
38
|
|
|
* @param array|null $priorities |
39
|
|
|
*/ |
40
|
|
|
public function __construct(Translator $translator, ?array $priorities = null) |
41
|
|
|
{ |
42
|
|
|
$this->translator = $translator; |
43
|
|
|
$this->priorities = $priorities ?? [$translator->getLocale()]; |
44
|
|
|
$this->negotiator = new LanguageNegotiator(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Process an incoming server request and return a response, optionally delegating |
49
|
|
|
* response creation to a handler. |
50
|
|
|
* |
51
|
|
|
* @param ServerRequestInterface $request |
52
|
|
|
* @param RequestHandlerInterface $handler |
53
|
|
|
* |
54
|
|
|
* @return ResponseInterface |
55
|
|
|
*/ |
56
|
|
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
57
|
|
|
{ |
58
|
|
|
$local = $this->getLocaleFromRequest($request); |
59
|
|
|
|
60
|
|
|
if (null !== $local) { |
61
|
|
|
$this->translator->setLocale($local); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return $handler->handle($request); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param ServerRequestInterface $request |
69
|
|
|
* |
70
|
|
|
* @return string|null |
71
|
|
|
*/ |
72
|
|
|
protected function getLocaleFromRequest(ServerRequestInterface $request): ?string |
73
|
|
|
{ |
74
|
|
|
$header = $request->getHeaderLine('Accept-Language'); |
75
|
|
|
|
76
|
|
|
if ('' === $header) { |
77
|
|
|
return null; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** @var AcceptLanguage $language */ |
81
|
|
|
$language = $this->negotiator->getBest($header, $this->priorities); |
82
|
|
|
|
83
|
|
|
return null === $language ? null : $language->getValue(); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|