1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NicolasBeauvais\Transcribe\Commands; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
6
|
|
|
use Illuminate\Support\Str; |
7
|
|
|
use NicolasBeauvais\Transcribe\Manager; |
8
|
|
|
|
9
|
|
|
class TransCommand extends Command |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* The name and signature of the console command. |
13
|
|
|
* |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
protected $signature = 'transcribe:trans {key} {--lang=}'; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* The name and signature of the console command. |
20
|
|
|
* |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $description = 'Add or update translations for the given key.'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* The name of the language file we're going to alter. |
27
|
|
|
* |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $fileName; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* The name of the package. |
34
|
|
|
* |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
protected $packageName; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* The name of the only language we're going to alter its file. |
41
|
|
|
* |
42
|
|
|
* @var null|string |
43
|
|
|
*/ |
44
|
|
|
protected $languageKey; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* The name of the key we're going to update or create. |
48
|
|
|
* |
49
|
|
|
* @var string |
50
|
|
|
*/ |
51
|
|
|
protected $key; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* The Languages manager instance. |
55
|
|
|
* |
56
|
|
|
* @var \NicolasBeauvais\Transcribe\Manager |
57
|
|
|
*/ |
58
|
|
|
private $manager; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Array of requested file in different languages. |
62
|
|
|
* |
63
|
|
|
* @var array |
64
|
|
|
*/ |
65
|
|
|
protected $files; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* ListCommand constructor. |
69
|
|
|
* |
70
|
|
|
* @param \NicolasBeauvais\Transcribe\Manager $manager |
71
|
|
|
* |
72
|
|
|
* @return void |
73
|
|
|
*/ |
74
|
|
|
public function __construct(Manager $manager) |
75
|
|
|
{ |
76
|
|
|
parent::__construct(); |
77
|
|
|
|
78
|
|
|
$this->manager = $manager; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Execute the console command. |
83
|
|
|
* |
84
|
|
|
* @return mixed |
85
|
|
|
*/ |
86
|
|
|
public function handle() |
87
|
|
|
{ |
88
|
|
|
if (!$this->parseKey()) { |
89
|
|
|
return; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$this->languageKey = $this->option('lang'); |
93
|
|
|
|
94
|
|
|
if (empty($this->files = $this->filesFromKey())) { |
95
|
|
|
return; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$this->fillKey(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Parse the given key argument. |
103
|
|
|
* |
104
|
|
|
* @return bool |
105
|
|
|
*/ |
106
|
|
|
private function parseKey() |
107
|
|
|
{ |
108
|
|
|
try { |
109
|
|
|
preg_match('/^([^\.]*)\.([^\:]*)/', $this->argument('key'), $matches); |
|
|
|
|
110
|
|
|
|
111
|
|
|
$this->fileName = $matches[1]; |
112
|
|
|
$this->key = $matches[2]; |
113
|
|
|
} catch (\ErrorException $e) { |
114
|
|
|
if (!$this->key) { |
115
|
|
|
$this->error('Could not recognize the key you want to translate.'); |
116
|
|
|
|
117
|
|
|
return false; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
if (Str::contains($this->fileName, '::')) { |
122
|
|
|
try { |
123
|
|
|
$parts = explode('::', $this->fileName); |
124
|
|
|
|
125
|
|
|
$this->manager->setPathToVendorPackage($parts[0]); |
126
|
|
|
|
127
|
|
|
$this->packageName = $parts[0]; |
128
|
|
|
} catch (\ErrorException $e) { |
129
|
|
|
$this->error('Could not recognize the package.'); |
130
|
|
|
|
131
|
|
|
return false; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
return true; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Array of requested file in different languages. |
140
|
|
|
* |
141
|
|
|
* @return array |
142
|
|
|
*/ |
143
|
|
|
private function filesFromKey() |
144
|
|
|
{ |
145
|
|
|
try { |
146
|
|
|
return $this->manager->files()[$this->fileName]; |
147
|
|
|
} catch (\ErrorException $e) { |
148
|
|
|
if ($this->confirm(sprintf('Language file %s.php not found, would you like to create it?', $this->fileName))) { |
149
|
|
|
$this->manager->createFile(str_replace($this->packageName.'::', '', $this->fileName)); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
return []; |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Fill a translation key in all languages. |
158
|
|
|
* |
159
|
|
|
* @return void |
160
|
|
|
*/ |
161
|
|
|
private function fillKey() |
162
|
|
|
{ |
163
|
|
|
$languages = $this->manager->languages(); |
164
|
|
|
|
165
|
|
|
if ($this->languageKey) { |
166
|
|
|
if (!in_array($this->languageKey, $languages)) { |
167
|
|
|
$this->error(sprintf('Language (%s) could not be found!', $this->languageKey)); |
168
|
|
|
|
169
|
|
|
return; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
// If a language key was specified then we prompt for it only. |
173
|
|
|
$languages = [$this->languageKey]; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
$values = $this->collectValues($languages); |
177
|
|
|
|
178
|
|
|
$this->manager->fillKeys( |
179
|
|
|
str_replace($this->packageName.'::', '', $this->fileName), |
180
|
|
|
[$this->key => $values] |
181
|
|
|
); |
182
|
|
|
|
183
|
|
|
foreach ($values as $languageKey => $value) { |
184
|
|
|
$this->line("<fg=yellow>{$this->fileName}.{$this->key}:{$languageKey}</> was set to \"<fg=yellow>{$value}</>\" successfully."); |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Collect translation values from console via questions. |
190
|
|
|
* |
191
|
|
|
* @param $languages |
192
|
|
|
* |
193
|
|
|
* @return array |
194
|
|
|
*/ |
195
|
|
|
private function collectValues($languages) |
196
|
|
|
{ |
197
|
|
|
$values = []; |
198
|
|
|
|
199
|
|
|
foreach ($languages as $languageKey) { |
200
|
|
|
$languageContent = $this->manager->getFileContent($this->files[$languageKey]); |
201
|
|
|
|
202
|
|
|
$values[$languageKey] = $this->ask( |
203
|
|
|
sprintf( |
204
|
|
|
'<fg=yellow>%s.%s:%s</> translation', |
205
|
|
|
$this->fileName, |
206
|
|
|
$this->key, |
207
|
|
|
$languageKey |
208
|
|
|
), |
209
|
|
|
isset($languageContent[$this->key]) ? $languageContent[$this->key] : null |
210
|
|
|
); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
return $values; |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|