1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Arcanedev\LaravelLang; |
6
|
|
|
|
7
|
|
|
use Arcanedev\LaravelLang\Contracts\TransManager as TransManagerContract; |
8
|
|
|
use Arcanedev\LaravelLang\Entities\{Locale, LocaleCollection}; |
9
|
|
|
use Illuminate\Filesystem\Filesystem; |
10
|
|
|
use Illuminate\Support\Arr; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class TransManager |
14
|
|
|
* |
15
|
|
|
* @author ARCANEDEV <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
class TransManager implements TransManagerContract |
18
|
|
|
{ |
19
|
|
|
/* ----------------------------------------------------------------- |
20
|
|
|
| Properties |
21
|
|
|
| ----------------------------------------------------------------- |
22
|
|
|
*/ |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Lang directories paths. |
26
|
|
|
* |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
private $paths = []; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Translations collection. |
33
|
|
|
* |
34
|
|
|
* @var array |
35
|
|
|
*/ |
36
|
|
|
private $locales = []; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* The filesystem instance. |
40
|
|
|
* |
41
|
|
|
* @var \Illuminate\Filesystem\Filesystem |
42
|
|
|
*/ |
43
|
|
|
private $filesystem; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Excluded folders. |
47
|
|
|
* |
48
|
|
|
* @var array |
49
|
|
|
*/ |
50
|
|
|
private $excludedFolders = [ |
51
|
|
|
'script', |
52
|
|
|
'vendor', |
53
|
|
|
]; |
54
|
|
|
|
55
|
|
|
/* ----------------------------------------------------------------- |
56
|
|
|
| Constructor |
57
|
|
|
| ----------------------------------------------------------------- |
58
|
|
|
*/ |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Make TransManager instance. |
62
|
|
|
* |
63
|
|
|
* @param \Illuminate\Filesystem\Filesystem $filesystem |
64
|
|
|
* @param array $paths |
65
|
|
|
*/ |
66
|
132 |
|
public function __construct(Filesystem $filesystem, array $paths) |
67
|
|
|
{ |
68
|
132 |
|
$this->filesystem = $filesystem; |
69
|
132 |
|
$this->paths = $paths; |
70
|
|
|
|
71
|
132 |
|
$this->load(); |
72
|
132 |
|
} |
73
|
|
|
|
74
|
|
|
/* ----------------------------------------------------------------- |
75
|
|
|
| Getters & Setters |
76
|
|
|
| ----------------------------------------------------------------- |
77
|
|
|
*/ |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Get the translation paths. |
81
|
|
|
* |
82
|
|
|
* @return array |
83
|
|
|
*/ |
84
|
132 |
|
public function getPaths(): array |
85
|
|
|
{ |
86
|
132 |
|
return $this->paths; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/* ----------------------------------------------------------------- |
90
|
|
|
| Main Methods |
91
|
|
|
| ----------------------------------------------------------------- |
92
|
|
|
*/ |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Load lang files. |
96
|
|
|
*/ |
97
|
132 |
|
private function load(): void |
98
|
|
|
{ |
99
|
132 |
|
$paths = $this->getPaths(); |
100
|
|
|
|
101
|
132 |
|
$this->locales['app'] = $this->loadTranslations($paths['app']); |
102
|
|
|
|
103
|
132 |
|
foreach ($paths['vendors'] as $path) { |
104
|
132 |
|
$this->locales['vendor-php'] = $this->loadTranslations($path); |
105
|
132 |
|
$this->locales['vendor-json'] = $this->loadJsonTranslations($path); |
106
|
|
|
} |
107
|
132 |
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Load translation files. |
111
|
|
|
* |
112
|
|
|
* @param string $dirPath |
113
|
|
|
* |
114
|
|
|
* @return \Arcanedev\LaravelLang\Entities\LocaleCollection |
115
|
|
|
*/ |
116
|
132 |
|
private function loadTranslations(string $dirPath): LocaleCollection |
117
|
|
|
{ |
118
|
132 |
|
$locales = new LocaleCollection; |
119
|
|
|
|
120
|
132 |
|
foreach ($this->filesystem->directories($dirPath) as $path) { |
121
|
132 |
|
if ($this->isExcluded($path)) |
122
|
132 |
|
continue; |
123
|
|
|
|
124
|
132 |
|
$locales->addOne( |
125
|
132 |
|
new Locale(basename($path), $path, $this->loadTranslationFiles($path)) |
126
|
|
|
); |
127
|
|
|
} |
128
|
|
|
|
129
|
132 |
|
return $locales; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Load the locale translation files. |
134
|
|
|
* |
135
|
|
|
* @param string $path |
136
|
|
|
* |
137
|
|
|
* @return array |
138
|
|
|
*/ |
139
|
132 |
|
private function loadTranslationFiles(string $path): array |
140
|
|
|
{ |
141
|
132 |
|
$files = []; |
142
|
|
|
|
143
|
132 |
|
foreach ($this->filesystem->allFiles($path) as $file) { |
144
|
132 |
|
if ($file->getExtension() !== 'php') |
145
|
132 |
|
continue; |
146
|
|
|
|
147
|
132 |
|
$key = str_replace( |
148
|
132 |
|
['.php', DIRECTORY_SEPARATOR], ['', '.'], $file->getRelativePathname() |
149
|
|
|
); |
150
|
|
|
|
151
|
132 |
|
$files[$key] = [ |
152
|
132 |
|
'path' => $file->getRealPath(), |
153
|
132 |
|
'content' => $this->filesystem->getRequire($file), |
154
|
|
|
]; |
155
|
|
|
} |
156
|
|
|
|
157
|
132 |
|
return $files; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Load json translation files. |
162
|
|
|
* |
163
|
|
|
* @param string $dirPath |
164
|
|
|
* |
165
|
|
|
* @return \Arcanedev\LaravelLang\Entities\LocaleCollection |
166
|
|
|
*/ |
167
|
132 |
|
private function loadJsonTranslations(string $dirPath): LocaleCollection |
168
|
|
|
{ |
169
|
132 |
|
$locales = new LocaleCollection; |
170
|
|
|
|
171
|
132 |
|
foreach ($this->filesystem->directories($dirPath) as $path) { |
172
|
132 |
|
if ($this->isExcluded($path)) |
173
|
|
|
continue; |
174
|
|
|
|
175
|
132 |
|
foreach ($this->filesystem->files($path) as $file) { |
176
|
132 |
|
if ($file->getExtension() === 'json') { |
177
|
132 |
|
$locales->addOne( |
178
|
132 |
|
new Locale($file->getBasename('.json'), $file->getRealPath()) |
179
|
|
|
); |
180
|
132 |
|
break; |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|
185
|
132 |
|
return $locales; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Get locale collection by group location. |
190
|
|
|
* |
191
|
|
|
* @param string $group |
192
|
|
|
* @param mixed|null $default |
193
|
|
|
* |
194
|
|
|
* @return \Arcanedev\LaravelLang\Entities\LocaleCollection|null |
195
|
|
|
*/ |
196
|
102 |
|
public function getCollection(string $group, $default = null): ?LocaleCollection |
197
|
|
|
{ |
198
|
102 |
|
return Arr::get($this->locales, $group, $default); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Get a locale translations from a group. |
203
|
|
|
* |
204
|
|
|
* @param string $group |
205
|
|
|
* @param string $locale |
206
|
|
|
* @param null $default |
207
|
|
|
* |
208
|
|
|
* @return \Arcanedev\LaravelLang\Entities\Locale|null |
209
|
|
|
*/ |
210
|
18 |
|
public function getFrom(string $group, string $locale, $default = null): ?Locale |
211
|
|
|
{ |
212
|
18 |
|
if ( ! $this->hasCollection($group)) { |
213
|
6 |
|
return $default; |
214
|
|
|
} |
215
|
|
|
|
216
|
12 |
|
$locales = $this->getCollection($group); |
217
|
|
|
|
218
|
12 |
|
return $locales->get($locale, $default); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Get locale keys. |
223
|
|
|
* |
224
|
|
|
* @return array |
225
|
|
|
*/ |
226
|
12 |
|
public function keys(): array |
227
|
|
|
{ |
228
|
12 |
|
$locales = array_map(function (LocaleCollection $locales) { |
229
|
12 |
|
$keys = $locales->keys()->toArray(); |
230
|
12 |
|
return array_combine($keys, $keys); |
231
|
12 |
|
}, $this->locales); |
232
|
|
|
|
233
|
12 |
|
$all = []; |
234
|
|
|
|
235
|
12 |
|
foreach ($locales as $keys) { |
236
|
12 |
|
$all = array_merge($all, $keys); |
237
|
|
|
} |
238
|
|
|
|
239
|
12 |
|
return $all; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* Get locales count. |
244
|
|
|
* |
245
|
|
|
* @return int |
246
|
|
|
*/ |
247
|
6 |
|
public function count(): int |
248
|
|
|
{ |
249
|
6 |
|
return count($this->keys()); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/* ----------------------------------------------------------------- |
253
|
|
|
| Check Methods |
254
|
|
|
| ----------------------------------------------------------------- |
255
|
|
|
*/ |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* Check if a translation group exists. |
259
|
|
|
* |
260
|
|
|
* @param string $group |
261
|
|
|
* |
262
|
|
|
* @return bool |
263
|
|
|
*/ |
264
|
24 |
|
public function hasCollection(string $group): bool |
265
|
|
|
{ |
266
|
24 |
|
return Arr::has($this->locales, $group); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* Check if the given path is excluded. |
271
|
|
|
* |
272
|
|
|
* @param string $path |
273
|
|
|
* |
274
|
|
|
* @return bool |
275
|
|
|
*/ |
276
|
132 |
|
private function isExcluded(string $path): bool |
277
|
|
|
{ |
278
|
132 |
|
return in_array(basename($path), $this->excludedFolders); |
279
|
|
|
} |
280
|
|
|
} |
281
|
|
|
|