1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Quantum PHP Framework |
5
|
|
|
* |
6
|
|
|
* An open source software development framework for PHP |
7
|
|
|
* |
8
|
|
|
* @package Quantum |
9
|
|
|
* @author Arman Ag. <[email protected]> |
10
|
|
|
* @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org) |
11
|
|
|
* @link http://quantum.softberg.org/ |
12
|
|
|
* @since 2.9.5 |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Quantum\Libraries\Lang; |
16
|
|
|
|
17
|
|
|
use Quantum\Libraries\Storage\FileSystem; |
18
|
|
|
use Quantum\Exceptions\DiException; |
19
|
|
|
use Dflydev\DotAccessData\Data; |
20
|
|
|
use Quantum\Loader\Loader; |
21
|
|
|
use Quantum\Http\Request; |
22
|
|
|
use Quantum\Loader\Setup; |
23
|
|
|
use ReflectionException; |
24
|
|
|
use Quantum\Di\Di; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Language class |
28
|
|
|
* @package Quantum\Libraries\Lang |
29
|
|
|
*/ |
30
|
|
|
class Lang |
31
|
|
|
{ |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Config key for lang segment |
35
|
|
|
*/ |
36
|
|
|
const LANG_SEGMENT = 'lang_segment'; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Current language |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
private static $currentLang; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Translations |
46
|
|
|
* @var Data |
47
|
|
|
*/ |
48
|
|
|
private static $translations = null; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var bool |
52
|
|
|
*/ |
53
|
|
|
private $isEnabled; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Instance of Lang |
57
|
|
|
* @var Lang |
58
|
|
|
*/ |
59
|
|
|
private static $instance = null; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @throws LangException |
63
|
|
|
*/ |
64
|
|
|
private function __construct() |
65
|
|
|
{ |
66
|
|
|
$this->isEnabled = filter_var(config()->get('multilang'), FILTER_VALIDATE_BOOLEAN); |
67
|
|
|
|
68
|
|
|
$langSegmentIndex = (int)config()->get(Lang::LANG_SEGMENT); |
69
|
|
|
|
70
|
|
|
if (!empty(route_prefix()) && $langSegmentIndex == 1) { |
71
|
|
|
$langSegmentIndex++; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$lang = Request::getSegment($langSegmentIndex); |
75
|
|
|
|
76
|
|
|
if (empty($lang) && !config()->get('lang_default')) { |
77
|
|
|
throw LangException::misconfiguredDefaultConfig(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
if (empty($lang) || !in_array($lang, (array)config()->get('langs'))) { |
81
|
|
|
$lang = config()->get('lang_default'); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$this->setLang($lang); |
|
|
|
|
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* GetInstance |
89
|
|
|
* @return Lang |
90
|
|
|
*/ |
91
|
|
|
public static function getInstance(): Lang |
92
|
|
|
{ |
93
|
|
|
if (self::$instance === null) { |
94
|
|
|
self::$instance = new self(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return self::$instance; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return bool |
102
|
|
|
*/ |
103
|
|
|
public function isEnabled(): bool |
104
|
|
|
{ |
105
|
|
|
return $this->isEnabled; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Loads translations |
110
|
|
|
* @throws LangException |
111
|
|
|
* @throws DiException |
112
|
|
|
* @throws ReflectionException |
113
|
|
|
*/ |
114
|
|
|
public function load() |
115
|
|
|
{ |
116
|
|
|
$fs = Di::get(FileSystem::class); |
117
|
|
|
|
118
|
|
|
$langDir = modules_dir() . DS . current_module() . DS . 'Resources' . DS . 'lang' . DS . $this->getLang(); |
119
|
|
|
|
120
|
|
|
$files = $fs->glob($langDir . DS . "*.php"); |
121
|
|
|
|
122
|
|
|
if (is_array($files) && !count($files)) { |
123
|
|
|
$langDir = base_dir() . DS . 'shared' . DS . 'resources' . DS . 'lang' . DS . $this->getLang(); |
124
|
|
|
|
125
|
|
|
$files = $fs->glob($langDir . DS . "*.php"); |
126
|
|
|
|
127
|
|
|
if (is_array($files) && !count($files)) { |
128
|
|
|
throw LangException::translationsNotFound($this->getLang()); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
$translations = []; |
133
|
|
|
|
134
|
|
|
foreach ($files as $file) { |
135
|
|
|
$fileName = $fs->fileName($file); |
136
|
|
|
|
137
|
|
|
$setup = new Setup(); |
138
|
|
|
$setup->setPathPrefix('Resources' . DS . 'lang' . DS . $this->getLang()); |
139
|
|
|
$setup->setFilename($fileName); |
140
|
|
|
$setup->setHierarchy(true); |
141
|
|
|
|
142
|
|
|
$translations[$fileName] = Di::get(Loader::class)->setup($setup)->load(); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
$this->setTranslations($translations); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Sets current language |
150
|
|
|
* @param string $lang |
151
|
|
|
* @return $this |
152
|
|
|
*/ |
153
|
|
|
public function setLang(string $lang): Lang |
154
|
|
|
{ |
155
|
|
|
self::$currentLang = $lang; |
156
|
|
|
return $this; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Gets the current language |
161
|
|
|
* @return string|null |
162
|
|
|
*/ |
163
|
|
|
public function getLang(): ?string |
164
|
|
|
{ |
165
|
|
|
return self::$currentLang; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Sets translations manually (for testing purposes) |
170
|
|
|
* @param array $translations |
171
|
|
|
*/ |
172
|
|
|
public function setTranslations(array $translations) |
173
|
|
|
{ |
174
|
|
|
if (self::$translations === null) { |
175
|
|
|
self::$translations = new Data(); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
self::$translations->import($translations); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Gets the whole translations of current language |
183
|
|
|
* @return Data |
184
|
|
|
*/ |
185
|
|
|
public function getTranslations(): ?Data |
186
|
|
|
{ |
187
|
|
|
return self::$translations; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Gets the translation by given key |
192
|
|
|
* @param string $key |
193
|
|
|
* @param string|array $params |
194
|
|
|
* @return string|null |
195
|
|
|
*/ |
196
|
|
|
public function getTranslation(string $key, $params = null): ?string |
197
|
|
|
{ |
198
|
|
|
if (self::$translations && self::$translations->has($key)) { |
199
|
|
|
$message = self::$translations->get($key); |
200
|
|
|
return $params ? _message($message, $params) : $message; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
return $key; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Flushes loaded translations |
208
|
|
|
*/ |
209
|
|
|
public function flush() |
210
|
|
|
{ |
211
|
|
|
self::$translations = null; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
} |
215
|
|
|
|