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