1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Locastic\ApiPlatformTranslationBundle\Translation; |
6
|
|
|
|
7
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
8
|
|
|
use Symfony\Component\Translation\TranslatorInterface; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class Translator |
12
|
|
|
* |
13
|
|
|
* @package Locastic\ApiPlatformTranslationBundle\Translation |
14
|
|
|
*/ |
15
|
|
|
class Translator implements TranslatorInterface |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var TranslatorInterface |
19
|
|
|
*/ |
20
|
|
|
private $translator; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var RequestStack |
24
|
|
|
*/ |
25
|
|
|
private $requestStack; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
private $defaultLocale; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Translator constructor. |
34
|
|
|
* |
35
|
|
|
* @param TranslatorInterface $translator |
36
|
|
|
* @param RequestStack $requestStack |
37
|
|
|
* @param string $defaultLocale |
38
|
|
|
*/ |
39
|
|
|
public function __construct(TranslatorInterface $translator, RequestStack $requestStack, string $defaultLocale) |
40
|
|
|
{ |
41
|
|
|
$this->translator = $translator; |
42
|
|
|
$this->requestStack = $requestStack; |
43
|
|
|
$this->defaultLocale = $defaultLocale; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* {@inheritdoc} |
48
|
|
|
*/ |
49
|
|
|
public function trans($id, array $parameters = array(), $domain = null, $locale = null): string |
50
|
|
|
{ |
51
|
|
|
if (!$locale) { |
|
|
|
|
52
|
|
|
$locale = $this->loadCurrentLocale(); |
|
|
|
|
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return $this->translator->trans($id, $parameters, $domain, $locale); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* {@inheritdoc} |
60
|
|
|
*/ |
61
|
|
|
public function transChoice($id, $number, array $parameters = array(), $domain = null, $locale = null): string |
62
|
|
|
{ |
63
|
|
|
if (!$locale) { |
|
|
|
|
64
|
|
|
$locale = $this->loadCurrentLocale(); |
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $this->translator->transChoice($id, $number, $parameters, $domain, $locale); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return string |
72
|
|
|
*/ |
73
|
|
|
public function loadCurrentLocale(): string |
74
|
|
|
{ |
75
|
|
|
$request = $this->requestStack->getCurrentRequest(); |
76
|
|
|
|
77
|
|
|
if (!$request) { |
78
|
|
|
return $this->defaultLocale; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$localeCode = $request->query->get('locale'); |
82
|
|
|
|
83
|
|
|
if (!$localeCode) { |
84
|
|
|
$preferredLanguage = $request->getPreferredLanguage(); |
85
|
|
|
$preferredLanguage = '' === $preferredLanguage ? null : $preferredLanguage; |
86
|
|
|
|
87
|
|
|
return $preferredLanguage ?? $this->defaultLocale; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $localeCode; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* {@inheritdoc} |
95
|
|
|
* @codeCoverageIgnore |
96
|
|
|
*/ |
97
|
|
|
public function setLocale($locale = 'en'): void |
98
|
|
|
{ |
99
|
|
|
$this->translator->setLocale($locale); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* {@inheritdoc} |
104
|
|
|
* @codeCoverageIgnore |
105
|
|
|
*/ |
106
|
|
|
public function getLocale(): string |
107
|
|
|
{ |
108
|
|
|
return $this->translator->getLocale(); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: