|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace NicolasBeauvais\Transcribe\Commands; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
|
6
|
|
|
use Illuminate\Support\Arr; |
|
7
|
|
|
use NicolasBeauvais\Transcribe\Manager; |
|
8
|
|
|
|
|
9
|
|
|
class MissingCommand extends Command |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* The name and signature of the console command. |
|
13
|
|
|
* |
|
14
|
|
|
* @var string |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $signature = 'transcribe:missing {--default}'; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* The name and signature of the console command. |
|
20
|
|
|
* |
|
21
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $description = 'Find missing translation values and fill them.'; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* The Languages manager instance. |
|
27
|
|
|
* |
|
28
|
|
|
* @var \NicolasBeauvais\Transcribe\Manager |
|
29
|
|
|
*/ |
|
30
|
|
|
private $manager; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Array of requested file in different languages. |
|
34
|
|
|
* |
|
35
|
|
|
* @var array |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $files; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* ListCommand constructor. |
|
41
|
|
|
* |
|
42
|
|
|
* @param \NicolasBeauvais\Transcribe\Manager $manager |
|
43
|
|
|
* |
|
44
|
|
|
* @return void |
|
45
|
|
|
*/ |
|
46
|
|
|
public function __construct(Manager $manager) |
|
47
|
|
|
{ |
|
48
|
|
|
parent::__construct(); |
|
49
|
|
|
|
|
50
|
|
|
$this->manager = $manager; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Execute the console command. |
|
55
|
|
|
* |
|
56
|
|
|
* @return mixed |
|
57
|
|
|
*/ |
|
58
|
|
|
public function handle() |
|
59
|
|
|
{ |
|
60
|
|
|
$this->info('Looking for missing translations...'); |
|
61
|
|
|
|
|
62
|
|
|
$this->translateValues($this->getMissing()); |
|
63
|
|
|
|
|
64
|
|
|
$this->info('Done!'); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Collect translation values from console via questions. |
|
69
|
|
|
* |
|
70
|
|
|
* @param array $missing |
|
71
|
|
|
* |
|
72
|
|
|
* @return array |
|
73
|
|
|
*/ |
|
74
|
|
|
private function translateValues(array $missing) |
|
75
|
|
|
{ |
|
76
|
|
|
$values = []; |
|
77
|
|
|
|
|
78
|
|
|
foreach ($missing as $missingKey) { |
|
79
|
|
|
$default = $this->getDefaultValue($missingKey); |
|
80
|
|
|
$value = $this->ask( |
|
81
|
|
|
"<fg=yellow>{$missingKey}</> translation [".config('app.fallback_locale').": $default]", |
|
82
|
|
|
$this->option('default') ? $default : null |
|
83
|
|
|
); |
|
84
|
|
|
|
|
85
|
|
|
preg_match('/^([^\.]*)\.(.*):(.*)/', $missingKey, $matches); |
|
86
|
|
|
|
|
87
|
|
|
$this->manager->fillKeys( |
|
88
|
|
|
$matches[1], |
|
89
|
|
|
[$matches[2] => [$matches[3] => $value]] |
|
90
|
|
|
); |
|
91
|
|
|
|
|
92
|
|
|
$this->line("\"<fg=yellow>{$missingKey}</>\" was set to \"<fg=yellow>{$value}</>\" successfully."); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
return $values; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Get translation in default locale for the given key. |
|
100
|
|
|
* |
|
101
|
|
|
* @param string $missingKey |
|
102
|
|
|
* |
|
103
|
|
|
* @return string |
|
104
|
|
|
*/ |
|
105
|
|
|
private function getDefaultValue($missingKey) |
|
106
|
|
|
{ |
|
107
|
|
|
try { |
|
108
|
|
|
$missingKey = explode(':', $missingKey)[0]; |
|
109
|
|
|
|
|
110
|
|
|
$decomposedKey = explode('.', $missingKey); |
|
111
|
|
|
$file = array_shift($decomposedKey); |
|
112
|
|
|
$key = implode('.', $decomposedKey); |
|
113
|
|
|
|
|
114
|
|
|
$filePath = $this->manager->files()[$file][config('app.fallback_locale')]; |
|
115
|
|
|
|
|
116
|
|
|
return array_get($this->manager->getFileContent($filePath), $key); |
|
117
|
|
|
} catch (\Exception $e) { |
|
118
|
|
|
return; |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Get an array of keys that have missing values with a hint |
|
124
|
|
|
* from another language translation file if possible. |
|
125
|
|
|
* |
|
126
|
|
|
* ex: [ ['key' => 'product.color.nl', 'hint' => 'en = "color"'] ] |
|
127
|
|
|
* |
|
128
|
|
|
* @param array $languages |
|
129
|
|
|
* |
|
130
|
|
|
* @return array |
|
131
|
|
|
*/ |
|
132
|
|
|
private function getMissing() |
|
133
|
|
|
{ |
|
134
|
|
|
$files = $this->manager->files(); |
|
135
|
|
|
|
|
136
|
|
|
// Array of content of all files indexed by fileName.languageKey |
|
137
|
|
|
$filesResults = []; |
|
138
|
|
|
|
|
139
|
|
|
// The final output of the method |
|
140
|
|
|
$missing = []; |
|
141
|
|
|
|
|
142
|
|
|
// Here we collect the file results |
|
143
|
|
|
foreach ($files as $fileName => $languageFiles) { |
|
144
|
|
|
foreach ($languageFiles as $languageKey => $filePath) { |
|
145
|
|
|
$filesResults[$fileName][$languageKey] = $this->manager->getFileContent($filePath); |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
$values = Arr::dot($filesResults); |
|
150
|
|
|
|
|
151
|
|
|
$emptyValues = array_filter($values, function ($value) { |
|
152
|
|
|
return $value == ''; |
|
153
|
|
|
}); |
|
154
|
|
|
|
|
155
|
|
|
// Adding all keys that has values = '' |
|
156
|
|
|
foreach ($emptyValues as $dottedValue => $emptyValue) { |
|
157
|
|
|
list($fileName, $languageKey, $key) = explode('.', $dottedValue, 3); |
|
158
|
|
|
|
|
159
|
|
|
$missing[] = "{$fileName}.{$key}:{$languageKey}"; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
$missing = array_merge($missing, $this->manager->getKeysExistingInALanguageButNotTheOther($values)); |
|
163
|
|
|
|
|
164
|
|
|
return $missing; |
|
165
|
|
|
} |
|
166
|
|
|
} |
|
167
|
|
|
|