1 | <?php |
||
18 | class TransManager implements TransManagerContract |
||
19 | { |
||
20 | /* ----------------------------------------------------------------- |
||
21 | | Properties |
||
22 | | ----------------------------------------------------------------- |
||
23 | */ |
||
24 | |||
25 | /** |
||
26 | * Lang directories paths. |
||
27 | * |
||
28 | * @var array |
||
29 | */ |
||
30 | private $paths = []; |
||
31 | |||
32 | /** |
||
33 | * Translations collection. |
||
34 | * |
||
35 | * @var array |
||
36 | */ |
||
37 | private $locales = []; |
||
38 | |||
39 | /** |
||
40 | * The filesystem instance. |
||
41 | * |
||
42 | * @var \Illuminate\Filesystem\Filesystem |
||
43 | */ |
||
44 | private $filesystem; |
||
45 | |||
46 | /** |
||
47 | * Excluded folders. |
||
48 | * |
||
49 | * @var array |
||
50 | */ |
||
51 | private $excludedFolders = [ |
||
52 | 'script', |
||
53 | 'vendor', |
||
54 | ]; |
||
55 | |||
56 | /* ----------------------------------------------------------------- |
||
57 | | Constructor |
||
58 | | ----------------------------------------------------------------- |
||
59 | */ |
||
60 | |||
61 | /** |
||
62 | * Make TransManager instance. |
||
63 | * |
||
64 | * @param \Illuminate\Filesystem\Filesystem $filesystem |
||
65 | * @param array $paths |
||
66 | */ |
||
67 | 88 | public function __construct(Filesystem $filesystem, array $paths) |
|
68 | { |
||
69 | 88 | $this->filesystem = $filesystem; |
|
70 | 88 | $this->paths = $paths; |
|
71 | |||
72 | 88 | $this->load(); |
|
73 | 88 | } |
|
74 | |||
75 | /* ----------------------------------------------------------------- |
||
76 | | Getters & Setters |
||
77 | | ----------------------------------------------------------------- |
||
78 | */ |
||
79 | |||
80 | /** |
||
81 | * Get the translation paths. |
||
82 | * |
||
83 | * @return array |
||
84 | */ |
||
85 | 88 | public function getPaths(): array |
|
86 | { |
||
87 | 88 | return $this->paths; |
|
88 | } |
||
89 | |||
90 | /* ----------------------------------------------------------------- |
||
91 | | Main Methods |
||
92 | | ----------------------------------------------------------------- |
||
93 | */ |
||
94 | |||
95 | /** |
||
96 | * Load lang files. |
||
97 | */ |
||
98 | 88 | private function load(): void |
|
99 | { |
||
100 | 88 | foreach ($this->getPaths() as $group => $path) { |
|
101 | 88 | $this->locales[$group] = $group === 'vendor-json' |
|
102 | 88 | ? $this->loadJsonDirectory($path) |
|
103 | 88 | : $this->loadDirectories($path); |
|
104 | } |
||
105 | 88 | } |
|
106 | |||
107 | /** |
||
108 | * @param string $path |
||
109 | * |
||
110 | * @return \Arcanedev\LaravelLang\Entities\LocaleCollection |
||
111 | */ |
||
112 | 88 | private function loadJsonDirectory(string $path): LocaleCollection |
|
124 | |||
125 | /** |
||
126 | * Load directories. |
||
127 | * |
||
128 | * @param string $dirPath |
||
129 | * |
||
130 | * @return \Arcanedev\LaravelLang\Entities\LocaleCollection |
||
131 | */ |
||
132 | 88 | private function loadDirectories(string $dirPath): LocaleCollection |
|
148 | |||
149 | /** |
||
150 | * Load the locale translation files. |
||
151 | * |
||
152 | * @param string $path |
||
153 | * |
||
154 | * @return array |
||
155 | */ |
||
156 | 88 | private function loadLocaleFiles(string $path): array |
|
174 | |||
175 | /** |
||
176 | * Get locale collection by group location. |
||
177 | * |
||
178 | * @param string $group |
||
179 | * @param mixed|null $default |
||
180 | * |
||
181 | * @return \Arcanedev\LaravelLang\Entities\LocaleCollection|null |
||
182 | */ |
||
183 | 68 | public function getCollection(string $group, $default = null): ?LocaleCollection |
|
187 | |||
188 | /** |
||
189 | * Get a locale translations from a group. |
||
190 | * |
||
191 | * @param string $group |
||
192 | * @param string $locale |
||
193 | * @param null $default |
||
194 | * |
||
195 | * @return \Arcanedev\LaravelLang\Entities\Locale|null |
||
196 | */ |
||
197 | 12 | public function getFrom(string $group, string $locale, $default = null): ?Locale |
|
207 | |||
208 | /** |
||
209 | * Get locale keys. |
||
210 | * |
||
211 | * @return array |
||
212 | */ |
||
213 | 8 | public function keys(): array |
|
214 | { |
||
215 | 8 | $locales = array_map(function (LocaleCollection $locales) { |
|
216 | 8 | $keys = $locales->keys()->toArray(); |
|
217 | 8 | return array_combine($keys, $keys); |
|
218 | 8 | }, $this->locales); |
|
219 | |||
220 | 8 | $all = []; |
|
221 | |||
222 | 8 | foreach ($locales as $keys) { |
|
223 | 8 | $all = array_merge($all, $keys); |
|
224 | } |
||
225 | |||
226 | 8 | return $all; |
|
227 | } |
||
228 | |||
229 | /** |
||
230 | * Get locales count. |
||
231 | * |
||
232 | * @return int |
||
233 | */ |
||
234 | 4 | public function count(): int |
|
238 | |||
239 | /* ----------------------------------------------------------------- |
||
240 | | Check Methods |
||
241 | | ----------------------------------------------------------------- |
||
242 | */ |
||
243 | |||
244 | /** |
||
245 | * Check if a translation group exists. |
||
246 | * |
||
247 | * @param string $group |
||
248 | * |
||
249 | * @return bool |
||
250 | */ |
||
251 | 16 | public function hasCollection(string $group): bool |
|
255 | |||
256 | /** |
||
257 | * Check if the given path is excluded. |
||
258 | * |
||
259 | * @param string $path |
||
260 | * |
||
261 | * @return bool |
||
262 | */ |
||
263 | 88 | private function isExcluded(string $path): bool |
|
267 | } |
||
268 |