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.8.0 |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Quantum\Libraries\Lang; |
16
|
|
|
|
17
|
|
|
use Quantum\Libraries\Storage\FileSystem; |
18
|
|
|
use Quantum\Exceptions\LangException; |
19
|
|
|
use Dflydev\DotAccessData\Data; |
20
|
|
|
use Quantum\Loader\Loader; |
21
|
|
|
use Quantum\Http\Request; |
22
|
|
|
use Quantum\Loader\Setup; |
23
|
|
|
use Quantum\Di\Di; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Language class |
27
|
|
|
* @package Quantum\Libraries\Lang |
28
|
|
|
*/ |
29
|
|
|
class Lang |
30
|
|
|
{ |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Config key for defined languages |
34
|
|
|
*/ |
35
|
|
|
const LANGS_DEFINED = 'langs'; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Config key for lang segment |
39
|
|
|
*/ |
40
|
|
|
const LANG_SEGMENT = 'lang_segment'; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Current language |
44
|
|
|
* @var string |
45
|
|
|
*/ |
46
|
|
|
private static $currentLang; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Translations |
50
|
|
|
* @var \Dflydev\DotAccessData\Data |
51
|
|
|
*/ |
52
|
|
|
private static $translations = null; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Instance of Lang |
56
|
|
|
* @var \Quantum\Libraries\Lang\Lang |
57
|
|
|
*/ |
58
|
|
|
private static $instance = null; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* GetInstance |
62
|
|
|
* @return \Quantum\Libraries\Lang\Lang |
63
|
|
|
*/ |
64
|
|
|
public static function getInstance(int $langSegmentIndex = 1): Lang |
65
|
|
|
{ |
66
|
|
|
if (self::$instance === null) { |
67
|
|
|
self::$instance = new self(); |
68
|
|
|
|
69
|
|
|
if (!empty(route_prefix()) && $langSegmentIndex == 1) { |
70
|
|
|
$langSegmentIndex += 1; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$lang = Request::getSegment($langSegmentIndex); |
74
|
|
|
|
75
|
|
|
if (empty($lang) && !config()->get('lang_default')) { |
76
|
|
|
throw LangException::misconfiguredDefaultConfig(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
if (empty($lang) || !in_array($lang, (array) config()->get('langs'))) { |
80
|
|
|
$lang = config()->get('lang_default'); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
self::$instance->setLang($lang); |
|
|
|
|
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return self::$instance; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Loads translations |
91
|
|
|
* @throws \Quantum\Exceptions\LangException |
92
|
|
|
* @throws \Quantum\Exceptions\DiException |
93
|
|
|
* @throws \ReflectionException |
94
|
|
|
*/ |
95
|
|
|
public function load() |
96
|
|
|
{ |
97
|
|
|
$fs = Di::get(FileSystem::class); |
98
|
|
|
|
99
|
|
|
$langDir = modules_dir() . DS . current_module() . DS . 'Resources' . DS . 'lang' . DS . $this->getLang(); |
100
|
|
|
|
101
|
|
|
$files = $fs->glob($langDir . DS . "*.php"); |
102
|
|
|
|
103
|
|
|
if (is_array($files) && !count($files)) { |
104
|
|
|
$langDir = base_dir() . DS . 'shared' . DS . 'Resources' . DS . 'lang' . DS . $this->getLang(); |
105
|
|
|
|
106
|
|
|
$files = $fs->glob($langDir . DS . "*.php"); |
107
|
|
|
|
108
|
|
|
if (is_array($files) && !count($files)) { |
109
|
|
|
throw LangException::translationsNotFound($this->getLang()); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
self::$translations = new Data(); |
114
|
|
|
|
115
|
|
|
foreach ($files as $file) { |
116
|
|
|
$fileName = $fs->fileName($file); |
117
|
|
|
|
118
|
|
|
$setup = new Setup(); |
119
|
|
|
$setup->setPathPrefix('Resources' . DS . 'lang' . DS . $this->getLang()); |
120
|
|
|
$setup->setFilename($fileName); |
121
|
|
|
$setup->setHierarchy(true); |
122
|
|
|
|
123
|
|
|
self::$translations->import([$fileName => Di::get(Loader::class)->setup($setup)->load()]); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Sets current language |
129
|
|
|
* @param string $lang |
130
|
|
|
* @return $this |
131
|
|
|
*/ |
132
|
|
|
public function setLang(string $lang): Lang |
133
|
|
|
{ |
134
|
|
|
self::$currentLang = $lang; |
135
|
|
|
return $this; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Gets the current language |
140
|
|
|
* @return string|null |
141
|
|
|
*/ |
142
|
|
|
public function getLang(): ?string |
143
|
|
|
{ |
144
|
|
|
return self::$currentLang; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Gets the whole translations of current language |
149
|
|
|
* @return \Dflydev\DotAccessData\Data |
150
|
|
|
*/ |
151
|
|
|
public function getTranslations(): ?Data |
152
|
|
|
{ |
153
|
|
|
return self::$translations; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Gets the translation by given key |
158
|
|
|
* @param string $key |
159
|
|
|
* @param string|array $params |
160
|
|
|
* @return string|null |
161
|
|
|
*/ |
162
|
|
|
public function getTranslation(string $key, $params = null): ?string |
163
|
|
|
{ |
164
|
|
|
if (self::$translations && self::$translations->has($key)) { |
165
|
|
|
if (!is_null($params)) { |
166
|
|
|
return _message(self::$translations->get($key), $params); |
167
|
|
|
} else { |
168
|
|
|
return self::$translations->get($key); |
169
|
|
|
} |
170
|
|
|
} else { |
171
|
|
|
return $key; |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Flushes loaded translations |
177
|
|
|
*/ |
178
|
|
|
public function flush() |
179
|
|
|
{ |
180
|
|
|
self::$translations = null; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
} |
184
|
|
|
|