|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* WebHemi. |
|
4
|
|
|
* |
|
5
|
|
|
* PHP version 7.1 |
|
6
|
|
|
* |
|
7
|
|
|
* @copyright 2012 - 2017 Gixx-web (http://www.gixx-web.com) |
|
8
|
|
|
* @license https://opensource.org/licenses/MIT The MIT License (MIT) |
|
9
|
|
|
* |
|
10
|
|
|
* @link http://www.gixx-web.com |
|
11
|
|
|
*/ |
|
12
|
|
|
declare(strict_types = 1); |
|
13
|
|
|
|
|
14
|
|
|
namespace WebHemi\I18n\ServiceAdapter\Base; |
|
15
|
|
|
|
|
16
|
|
|
use InvalidArgumentException; |
|
17
|
|
|
use WebHemi\Configuration\ServiceInterface as ConfigurationInterface; |
|
18
|
|
|
use WebHemi\Environment\ServiceInterface as EnvironmentInterface; |
|
19
|
|
|
use WebHemi\I18n\ServiceInterface; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Class ServiceAdapter. |
|
23
|
|
|
*/ |
|
24
|
|
|
class ServiceAdapter implements ServiceInterface |
|
25
|
|
|
{ |
|
26
|
|
|
/** @var ConfigurationInterface */ |
|
27
|
|
|
protected $configuration; |
|
28
|
|
|
/** @var EnvironmentInterface */ |
|
29
|
|
|
protected $environmentManager; |
|
30
|
|
|
/** @var string */ |
|
31
|
|
|
protected $language; |
|
32
|
|
|
/** @var string */ |
|
33
|
|
|
protected $territory; |
|
34
|
|
|
/** @var string */ |
|
35
|
|
|
protected $codeSet; |
|
36
|
|
|
/** @var string */ |
|
37
|
|
|
protected $locale; |
|
38
|
|
|
/** @var string */ |
|
39
|
|
|
protected $timeZone; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* ServiceAdapter constructor. |
|
43
|
|
|
* |
|
44
|
|
|
* @param ConfigurationInterface $configuration |
|
45
|
|
|
* @param EnvironmentInterface $environmentManager |
|
46
|
|
|
*/ |
|
47
|
7 |
|
public function __construct( |
|
48
|
|
|
ConfigurationInterface $configuration, |
|
49
|
|
|
EnvironmentInterface $environmentManager |
|
50
|
|
|
) { |
|
51
|
7 |
|
$this->configuration = $configuration; |
|
52
|
7 |
|
$this->environmentManager = $environmentManager; |
|
53
|
|
|
|
|
54
|
|
|
$currentApplicationConfig = $configuration |
|
55
|
7 |
|
->getConfig('applications') |
|
56
|
7 |
|
->getData($environmentManager->getSelectedApplication()); |
|
57
|
|
|
|
|
58
|
7 |
|
$this->setLocale($currentApplicationConfig['locale']); |
|
59
|
7 |
|
$this->setTimeZone($currentApplicationConfig['timezone']); |
|
60
|
7 |
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Gets the language. |
|
64
|
|
|
* |
|
65
|
|
|
* @return string |
|
66
|
|
|
*/ |
|
67
|
4 |
|
public function getLanguage() : string |
|
68
|
|
|
{ |
|
69
|
4 |
|
return $this->language; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Sets the language. |
|
74
|
|
|
* |
|
75
|
|
|
* @param string $language |
|
76
|
|
|
*/ |
|
77
|
3 |
|
protected function setLanguage(string $language) : void |
|
78
|
|
|
{ |
|
79
|
3 |
|
$this->language = $language; |
|
80
|
|
|
|
|
81
|
3 |
|
putenv('LANGUAGE='.$language); |
|
82
|
3 |
|
putenv('LANG='.$language); |
|
83
|
3 |
|
setlocale(LC_TIME, ''); |
|
84
|
3 |
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Gets the territory. |
|
88
|
|
|
* |
|
89
|
|
|
* @return string |
|
90
|
|
|
*/ |
|
91
|
|
|
public function getTerrytory() : string |
|
92
|
|
|
{ |
|
93
|
|
|
return $this->territory; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Gets the Locale. |
|
98
|
|
|
* |
|
99
|
|
|
* @return string |
|
100
|
|
|
*/ |
|
101
|
|
|
public function getLocale() : string |
|
102
|
|
|
{ |
|
103
|
|
|
return $this->locale; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Gets the code set. |
|
108
|
|
|
* |
|
109
|
|
|
* @return string |
|
110
|
|
|
*/ |
|
111
|
|
|
public function getCodeSet() : string |
|
112
|
|
|
{ |
|
113
|
|
|
return $this->codeSet; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Sets the locale. |
|
118
|
|
|
* |
|
119
|
|
|
* @param string $locale |
|
120
|
|
|
* @throws InvalidArgumentException |
|
121
|
|
|
* @return ServiceAdapter |
|
122
|
|
|
*/ |
|
123
|
3 |
|
public function setLocale(string $locale) : ServiceAdapter |
|
124
|
|
|
{ |
|
125
|
3 |
|
$matches = []; |
|
126
|
|
|
|
|
127
|
3 |
|
if (preg_match('/^(?P<language>[a-z]+)_(?P<territory>[A-Z]+)(?:\.(?P<codeSet>.*))?/', $locale, $matches)) { |
|
128
|
3 |
|
$language = $matches['language']; |
|
129
|
3 |
|
$this->setLanguage($language); |
|
130
|
|
|
|
|
131
|
3 |
|
$this->territory = $matches['territory']; |
|
132
|
|
|
|
|
133
|
3 |
|
$this->codeSet = $matches['codeSet'] ?? 'utf8'; |
|
134
|
3 |
|
$localeCodeSet = strtolower(str_replace('-', '', $this->codeSet)); |
|
135
|
|
|
|
|
136
|
3 |
|
$composedLocale = $language.'_'.$this->territory.'.'.$localeCodeSet; |
|
137
|
|
|
|
|
138
|
3 |
|
putenv('LC_ALL='.$composedLocale); |
|
139
|
3 |
|
setlocale(LC_ALL, $composedLocale); |
|
140
|
3 |
|
setlocale(LC_MESSAGES, $composedLocale); |
|
141
|
3 |
|
setlocale(LC_CTYPE, $composedLocale); |
|
142
|
|
|
} else { |
|
143
|
|
|
throw new InvalidArgumentException(sprintf('Invalid locale: %s', $locale), 1000); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
3 |
|
return $this; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Sets the time zone. |
|
151
|
|
|
* |
|
152
|
|
|
* @param string $timeZone |
|
153
|
|
|
* @return ServiceAdapter |
|
154
|
|
|
*/ |
|
155
|
3 |
|
public function setTimeZone(string $timeZone) : ServiceAdapter |
|
156
|
|
|
{ |
|
157
|
3 |
|
$this->timeZone = $timeZone; |
|
158
|
|
|
|
|
159
|
3 |
|
date_default_timezone_set($timeZone); |
|
160
|
|
|
|
|
161
|
3 |
|
return $this; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Gets the time zone. |
|
166
|
|
|
* |
|
167
|
|
|
* @return string |
|
168
|
|
|
*/ |
|
169
|
|
|
public function getTimeZone() : string |
|
170
|
|
|
{ |
|
171
|
|
|
return $this->timeZone; |
|
172
|
|
|
} |
|
173
|
|
|
} |
|
174
|
|
|
|