1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nikaia\TranslationSheet\Translation; |
4
|
|
|
|
5
|
|
|
use Illuminate\Filesystem\Filesystem; |
6
|
|
|
use Illuminate\Foundation\Application; |
7
|
|
|
use Illuminate\Support\Collection; |
8
|
|
|
|
9
|
|
|
class Reader |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Translation items. |
13
|
|
|
* |
14
|
|
|
* @var \Illuminate\Support\Collection |
15
|
|
|
*/ |
16
|
|
|
protected $translations; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var Application |
20
|
|
|
*/ |
21
|
|
|
protected $app; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var \Illuminate\Filesystem\Filesystem |
25
|
|
|
*/ |
26
|
|
|
protected $files; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string specify locale that we need to scan. |
30
|
|
|
* Returns all if no locale specified. |
31
|
|
|
*/ |
32
|
|
|
private $locale; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Reader. |
36
|
|
|
* |
37
|
|
|
* @param Application $app |
38
|
|
|
* @param Filesystem $files |
39
|
|
|
*/ |
40
|
3 |
|
public function __construct(Application $app, Filesystem $files) |
41
|
|
|
{ |
42
|
3 |
|
$this->app = $app; |
43
|
3 |
|
$this->files = $files; |
44
|
3 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Set reader locale. |
48
|
|
|
* |
49
|
|
|
* @param $locale |
50
|
|
|
* @return $this |
51
|
|
|
*/ |
52
|
|
|
public function setLocale($locale) |
53
|
|
|
{ |
54
|
|
|
$this->locale = $locale; |
55
|
|
|
|
56
|
|
|
return $this; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Scan modules, app and overridden packages lang |
61
|
|
|
* and return all defined translations. |
62
|
|
|
* |
63
|
|
|
* @return Collection |
64
|
|
|
* @return array |
65
|
|
|
*/ |
66
|
2 |
|
public function scan() |
67
|
|
|
{ |
68
|
|
|
// Reset |
69
|
2 |
|
$this->translations = new Collection; |
70
|
|
|
|
71
|
|
|
// App directory |
72
|
2 |
|
$this->scanDirectory($this->app->make('path.lang')); |
73
|
|
|
|
74
|
2 |
|
return $this->translations; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Scan a directory. |
79
|
|
|
* |
80
|
|
|
* @param string $path to directory to scan |
81
|
|
|
*/ |
82
|
2 |
|
protected function scanDirectory($path) |
83
|
|
|
{ |
84
|
2 |
|
foreach ($this->files->directories($path) as $directory) { |
85
|
2 |
|
if ($this->isVendorDirectory($directory)) { |
86
|
2 |
|
$this->scanVendorDirectory($directory); |
87
|
|
|
} |
88
|
|
|
else { |
89
|
2 |
|
$this->loadTranslationsInDirectory($directory, $this->getLocaleFromDirectory($directory), null); |
90
|
|
|
} |
91
|
|
|
} |
92
|
2 |
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Scan overridden packages lang. |
96
|
|
|
* |
97
|
|
|
* @param $vendorsDirectory |
98
|
|
|
*/ |
99
|
2 |
|
private function scanVendorDirectory($vendorsDirectory) |
100
|
|
|
{ |
101
|
2 |
|
foreach ($this->files->directories($vendorsDirectory) as $vendorPath) { |
102
|
2 |
|
$namespace = basename($vendorPath); |
103
|
2 |
|
foreach ($this->files->directories($vendorPath) as $localePath) { |
104
|
2 |
|
$this->loadTranslationsInDirectory($localePath, basename($localePath), $namespace); |
105
|
|
|
} |
106
|
|
|
} |
107
|
2 |
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Load all directory file translation (multiple group) into translations collection. |
111
|
|
|
* |
112
|
|
|
* @param $directory |
113
|
|
|
* @param $locale |
114
|
|
|
* @param $namespace |
115
|
|
|
*/ |
116
|
2 |
|
private function loadTranslationsInDirectory($directory, $locale, $namespace) |
117
|
|
|
{ |
118
|
2 |
|
if (! $this->requestedLocale($locale)) { |
119
|
|
|
return; |
120
|
|
|
} |
121
|
|
|
|
122
|
2 |
|
foreach ($this->files->files($directory) as $file) { |
123
|
2 |
|
$info = pathinfo($file); |
124
|
2 |
|
$group = $info['filename']; |
125
|
2 |
|
$this->loadTranslations($locale, $group, $namespace, $file); |
126
|
|
|
} |
127
|
2 |
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Load file translation (group) into translations collection. |
131
|
|
|
* |
132
|
|
|
* @param $locale |
133
|
|
|
* @param $group |
134
|
|
|
* @param $namespace |
135
|
|
|
* @param $file |
136
|
|
|
*/ |
137
|
2 |
|
private function loadTranslations($locale, $group, $namespace, $file) |
138
|
|
|
{ |
139
|
2 |
|
$translations = array_dot($this->app['translator']->getLoader()->load($locale, $group, $namespace)); |
140
|
|
|
|
141
|
2 |
|
foreach ($translations as $key => $value) { |
142
|
|
|
|
143
|
|
|
// Avoid break in this case : |
144
|
|
|
// Case of 'car.messages = []', the key 'messages' is specified in the translation |
145
|
|
|
// file but no items defined inside. |
146
|
2 |
|
if (is_array($value) && count($value) === 0) { |
147
|
|
|
continue; |
148
|
|
|
} |
149
|
|
|
|
150
|
2 |
|
$entity = new Item; |
151
|
2 |
|
$entity->namespace = $namespace; |
152
|
2 |
|
$entity->locale = $locale; |
153
|
2 |
|
$entity->group = $group; |
154
|
2 |
|
$entity->key = $key; |
155
|
2 |
|
$entity->full_key = $this->fullKey($namespace, $group, $key); |
156
|
2 |
|
$entity->value = (string) $value; |
157
|
2 |
|
$entity->source_file = $this->sourceFile($file); |
158
|
|
|
|
159
|
2 |
|
$this->translations->push($entity); |
160
|
|
|
} |
161
|
2 |
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Return a full lang key. |
165
|
|
|
* |
166
|
|
|
* @param $namespace |
167
|
|
|
* @param $group |
168
|
|
|
* @param $key |
169
|
|
|
* @return string |
170
|
|
|
*/ |
171
|
2 |
|
private function fullKey($namespace, $group, $key) |
172
|
|
|
{ |
173
|
|
|
return |
174
|
2 |
|
($namespace ? "$namespace::" : '') |
175
|
2 |
|
.$group.'.' |
176
|
2 |
|
.$key; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Return relative path of language file. |
181
|
|
|
* |
182
|
|
|
* @param $file |
183
|
|
|
* @return mixed |
184
|
|
|
*/ |
185
|
2 |
|
private function sourceFile($file) |
186
|
|
|
{ |
187
|
2 |
|
return $this->toRelative(realpath($file)); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Return relative path related to base_path(). |
192
|
|
|
* |
193
|
|
|
* @param $path |
194
|
|
|
* @return string |
195
|
|
|
*/ |
196
|
2 |
|
private function toRelative($path) |
197
|
|
|
{ |
198
|
2 |
|
$relative = str_replace($this->app->make('path.lang').DIRECTORY_SEPARATOR, '', $path); |
199
|
2 |
|
$relative = str_replace('\\', '/', $relative); |
200
|
|
|
|
201
|
2 |
|
return $relative; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Determine if a found locale is requested for scanning. |
206
|
|
|
* If $this->locale is not set, we assume that all the locales were requested. |
207
|
|
|
* |
208
|
|
|
* @param string $locale the locale to check |
209
|
|
|
* @return bool |
210
|
|
|
*/ |
211
|
2 |
|
private function requestedLocale($locale) |
212
|
|
|
{ |
213
|
2 |
|
if (empty($this->locale)) { |
214
|
2 |
|
return true; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
return $locale === $this->locale; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Return locale from directory |
222
|
|
|
* ie. resources/lang/en -> en. |
223
|
|
|
* |
224
|
|
|
* @param $directory |
225
|
|
|
* @return string |
226
|
|
|
*/ |
227
|
2 |
|
private function getLocaleFromDirectory($directory) |
228
|
|
|
{ |
229
|
2 |
|
return basename($directory); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Return true if it is the vendor directory. |
234
|
|
|
* |
235
|
|
|
* @param $directory |
236
|
|
|
* @return bool |
237
|
|
|
*/ |
238
|
3 |
|
private function isVendorDirectory($directory) |
239
|
|
|
{ |
240
|
3 |
|
return basename($directory) === 'vendor'; |
241
|
|
|
} |
242
|
|
|
} |
243
|
|
|
|