1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Terranet\Administrator\Services; |
4
|
|
|
|
5
|
|
|
use Illuminate\Filesystem\Filesystem; |
6
|
|
|
use Illuminate\Support\Collection; |
7
|
|
|
use Illuminate\Support\Str; |
8
|
|
|
use Terranet\Administrator\Services\Translations\Reader; |
9
|
|
|
use Zend\Code\Generator\ValueGenerator; |
10
|
|
|
|
11
|
|
|
class TranslationsManager |
12
|
|
|
{ |
13
|
|
|
/** @var Collection */ |
14
|
|
|
protected $locales; |
15
|
|
|
|
16
|
|
|
/** @var Translations\Reader */ |
17
|
|
|
protected $reader; |
18
|
|
|
|
19
|
|
|
/** @var Filesystem */ |
20
|
|
|
protected $filesystem; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* TranslationsManager constructor. |
24
|
|
|
*/ |
25
|
|
|
public function __construct(Reader $translationsReader, Filesystem $filesystem) |
26
|
|
|
{ |
27
|
|
|
$this->reader = $translationsReader; |
28
|
|
|
$this->filesystem = $filesystem; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param Collection $locales |
33
|
|
|
* @return $this |
34
|
|
|
*/ |
35
|
|
|
public function setLocales(Collection $locales) |
36
|
|
|
{ |
37
|
|
|
$this->locales = $locales; |
38
|
|
|
|
39
|
|
|
return $this; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param null $term |
|
|
|
|
44
|
|
|
* @param null $only |
|
|
|
|
45
|
|
|
* @param int $page |
46
|
|
|
* @param int $perPage |
47
|
|
|
* @return \Illuminate\Pagination\LengthAwarePaginator |
|
|
|
|
48
|
|
|
*/ |
49
|
|
|
public function load($term = null, $only = null, $page = 1, $perPage = 20) |
50
|
|
|
{ |
51
|
|
|
return (new Translations\Finder())->find( |
52
|
|
|
$this->reader->read($this->files($only), $this->locales()), |
53
|
|
|
$term, $page, $perPage |
54
|
|
|
); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return Translations\Exporter |
59
|
|
|
*/ |
60
|
|
|
public function exporter() |
61
|
|
|
{ |
62
|
|
|
return new Translations\Exporter(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Retrieves public filters. |
67
|
|
|
* |
68
|
|
|
* @return mixed |
69
|
|
|
*/ |
70
|
|
|
public function filters() |
71
|
|
|
{ |
72
|
|
|
$filters = $this->files(); |
73
|
|
|
|
74
|
|
|
if ($only = config('administrator.translations.filters.only', [])) { |
|
|
|
|
75
|
|
|
$filters = $filters->intersect($only); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
if ($except = config('administrator.translations.filters.except', [])) { |
79
|
|
|
$filters = $filters->diff($except); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $filters; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param $translation |
87
|
|
|
* @param $locale |
88
|
|
|
*/ |
89
|
|
|
public function save($translation, $locale) |
90
|
|
|
{ |
91
|
|
|
$translations = []; |
92
|
|
|
|
93
|
|
|
foreach ($translation as $key => $value) { |
94
|
|
|
$this->keyToArray($translations, $key, $value[$locale]); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
foreach ($translations as $key => $value) { |
98
|
|
|
if (!$translations = $this->loadTranslationFile($key, $locale)) { |
99
|
|
|
continue; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$data = array_replace_recursive($translations['content'], $value); |
103
|
|
|
|
104
|
|
|
$content = '<?php'.str_repeat(PHP_EOL, 2).'return '.$this->arrayToString($data).';'; |
105
|
|
|
|
106
|
|
|
$this->filesystem->put($translations['path'], $content, true); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param null $only |
|
|
|
|
112
|
|
|
* @return Collection |
113
|
|
|
*/ |
114
|
|
|
protected function files($only = null): Collection |
115
|
|
|
{ |
116
|
|
|
static $files = null; |
117
|
|
|
|
118
|
|
|
if (null === $files) { |
119
|
|
|
$path = 'lang'.\DIRECTORY_SEPARATOR.\localizer\locale()->iso6391(); |
120
|
|
|
|
121
|
|
|
$files = collect( |
122
|
|
|
glob(resource_path("{$path}/*.php")) |
|
|
|
|
123
|
|
|
); |
124
|
|
|
|
125
|
|
|
$files = $files->map(function ($file) { |
126
|
|
|
return Str::replaceLast('.php', '', basename($file)); |
127
|
|
|
}, $files); |
|
|
|
|
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return $files->when($only, function (Collection $files) use ($only) { |
131
|
|
|
return $files->intersect(\is_array($only) ? $only : [$only]); |
|
|
|
|
132
|
|
|
}); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @param $file |
137
|
|
|
* @param $locale |
138
|
|
|
* @return array |
139
|
|
|
*/ |
140
|
|
|
protected function loadTranslationFile($file, $locale) |
141
|
|
|
{ |
142
|
|
|
if (!file_exists($path = $this->reader->pathToFile($file, $locale))) { |
143
|
|
|
$this->makeFile($file, $locale); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
return [ |
147
|
|
|
'path' => $path, |
148
|
|
|
'content' => include_once $path, |
149
|
|
|
]; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @param $file |
154
|
|
|
* @param $locale |
155
|
|
|
*/ |
156
|
|
|
protected function makeFile($file, $locale) |
157
|
|
|
{ |
158
|
|
|
$directoryTranslationsPath = resource_path('lang'.\DIRECTORY_SEPARATOR.$locale); |
|
|
|
|
159
|
|
|
|
160
|
|
|
if (!$this->filesystem->exists($directoryTranslationsPath)) { |
161
|
|
|
$this->filesystem->makeDirectory($directoryTranslationsPath); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
$content = '<?php'.str_repeat(PHP_EOL, 2).'return [];'; |
165
|
|
|
|
166
|
|
|
$this->filesystem->put($directoryTranslationsPath.\DIRECTORY_SEPARATOR.$file.'.php', $content, true); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @param $arr |
171
|
|
|
* @param $path |
172
|
|
|
* @param $value |
173
|
|
|
* @param string $separator |
174
|
|
|
*/ |
175
|
|
|
protected function keyToArray(&$arr, $path, $value, $separator = '.') |
176
|
|
|
{ |
177
|
|
|
foreach ($keys = explode($separator, $path) as $key) { |
|
|
|
|
178
|
|
|
$arr = &$arr[$key]; |
179
|
|
|
} |
180
|
|
|
$arr = $value; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @param $data |
185
|
|
|
* @return string |
186
|
|
|
*/ |
187
|
|
|
protected function arrayToString($data): string |
188
|
|
|
{ |
189
|
|
|
$generator = new ValueGenerator($data, ValueGenerator::TYPE_ARRAY_SHORT); |
190
|
|
|
$generator->setIndentation(' '); // 4 spaces |
191
|
|
|
|
192
|
|
|
return $generator->generate(); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @return Collection |
197
|
|
|
*/ |
198
|
|
|
protected function locales(): Collection |
199
|
|
|
{ |
200
|
|
|
return collect($this->locales); |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|