1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Artisan Command to cache Translations from the Database. |
5
|
|
|
*/ |
6
|
|
|
namespace Hokan22\LaravelTranslator\commands; |
7
|
|
|
|
8
|
|
|
use Illuminate\Console\Command; |
9
|
|
|
use Illuminate\Support\Facades\File; |
10
|
|
|
use Symfony\Component\Console\Helper\ProgressBar; |
11
|
|
|
use Hokan22\LaravelTranslator\TranslatorFacade; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class SearchTranslationsCommand |
15
|
|
|
* |
16
|
|
|
* @package Hokan22\LaravelTranslator\commands |
17
|
|
|
* @author Alexander Viertel <[email protected]> |
18
|
|
|
* @license http://opensource.org/licenses/MIT MIT |
19
|
|
|
*/ |
20
|
|
|
class SearchTranslationsCommand extends Command |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* The name and signature of the console command. |
24
|
|
|
* |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $signature = "translator:search"; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The console command description. |
31
|
|
|
* |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected $description = "Searches through all used translations in PHP files. After gathering Strings to translate, they will be inserted into the Database for further treatment."; |
35
|
|
|
|
36
|
|
|
protected $found_identifier = 0; |
37
|
|
|
protected $found_parameters = 0; |
38
|
|
|
protected $found_invalid = 0; |
39
|
|
|
protected $new_identifier = 0; |
40
|
|
|
protected $dupl_identifier = 0; |
41
|
|
|
|
42
|
|
|
/**@var ProgressBar $bar Progressbar for progress of iterating through files */ |
43
|
|
|
protected $bar; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Create a new command instance. |
47
|
|
|
*/ |
48
|
|
|
public function __construct() { |
49
|
|
|
parent::__construct(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Execute the console command. |
54
|
|
|
* |
55
|
|
|
* @return mixed |
56
|
|
|
* @throws \Exception |
57
|
|
|
*/ |
58
|
|
|
public function handle() { |
59
|
|
|
$start = microtime(true); |
60
|
|
|
|
61
|
|
|
$this->line(''); |
62
|
|
|
|
63
|
|
|
$aFiles = []; |
64
|
|
|
|
65
|
|
|
$valid_extensions = ['php', 'html', 'js']; |
66
|
|
|
$folders = [ |
67
|
|
|
'app', |
68
|
|
|
'resources/views', |
69
|
|
|
'resources/assets', |
70
|
|
|
]; |
71
|
|
|
|
72
|
|
|
$regexes = [ |
73
|
|
|
'default' => '/(?:[\@|\_]t\()\'(?\'identifier\'.*?)\'(?:\)|(?:, (?\'parameters\'\[.*\]))(?:\)|, \'(?\'locale\'\w*?)\'))/', |
74
|
|
|
'js' => '/\$filter\(\'translate\'\)\(\'(?\'identifier\'.*?)\'\)/' |
75
|
|
|
]; |
76
|
|
|
|
77
|
|
|
foreach ($folders as $folder) { |
78
|
|
|
$aFiles = array_merge($aFiles, File::allFiles(base_path() . '/' . $folder)); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
TranslatorFacade::setLocale('de_DE'); |
82
|
|
|
|
83
|
|
|
$num_files = count($aFiles); |
84
|
|
|
|
85
|
|
|
$this->bar = $this->output->createProgressBar($num_files); |
86
|
|
|
$this->bar->setMessage('Analyzing ' . $num_files . ' files'); |
87
|
|
|
$this->bar->setFormat('very_verbose'); |
88
|
|
|
|
89
|
|
|
foreach ($aFiles as $file) { |
90
|
|
|
|
91
|
|
|
$extension = $file->getExtension(); |
92
|
|
|
|
93
|
|
|
if (in_array($extension, $valid_extensions)) { |
94
|
|
|
$content = file_get_contents($file); |
95
|
|
|
|
96
|
|
|
foreach ($regexes as $key => $regex) { |
97
|
|
|
preg_match_all($regex, $content, $result, PREG_SET_ORDER); |
98
|
|
|
|
99
|
|
|
if (!empty($result[0])) { |
100
|
|
|
$this->addMissing($result, $key); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
$this->bar->advance(); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$this->bar->finish(); |
108
|
|
|
$this->line(''); |
109
|
|
|
$this->line(''); |
110
|
|
|
|
111
|
|
|
$this->table( |
112
|
|
|
['Num', 'Translations...'], |
113
|
|
|
[ |
114
|
|
|
[$this->found_identifier, "Found"], |
115
|
|
|
[$this->new_identifier, "New"], |
116
|
|
|
[$this->dupl_identifier, "Duplicates"], |
117
|
|
|
[$this->found_parameters, "With Parameters"], |
118
|
|
|
[$this->found_invalid, "Invalid"], |
119
|
|
|
] |
120
|
|
|
); |
121
|
|
|
|
122
|
|
|
$this->line(''); |
123
|
|
|
|
124
|
|
|
$this->info('Finished in: ' . number_format(microtime(true) - $start, 2) . 'sec'); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Add missing Identifiers to the Database |
129
|
|
|
* |
130
|
|
|
* @param array $result Array with |
131
|
|
|
* @param string $group |
132
|
|
|
*/ |
133
|
|
|
protected function addMissing($result, $group) { |
134
|
|
|
foreach ($result as $item) { |
135
|
|
|
try { |
136
|
|
|
$identifier = trim($item['identifier']); |
137
|
|
|
$this->found_identifier++; |
138
|
|
|
|
139
|
|
|
$parameters = null; |
140
|
|
|
|
141
|
|
|
if (isset($item['parameters'])) { |
142
|
|
|
$parameter_string = $item['parameters']; |
143
|
|
|
|
144
|
|
|
if (substr($parameter_string, 0, 1) === '[') { |
145
|
|
|
$parameter_string = substr($parameter_string, 1, -1); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
$parameter_array = explode(",", $parameter_string); |
149
|
|
|
$parameters = array(); |
150
|
|
|
|
151
|
|
|
foreach ($parameter_array as $parameter) { |
152
|
|
|
$parameter = explode("=>", $parameter); |
153
|
|
|
|
154
|
|
|
$key = str_replace([" ", "'"], "", $parameter[0]); |
155
|
|
|
$value = str_replace([" ", "'"], "", $parameter[1]); |
156
|
|
|
|
157
|
|
|
$parameters[$key] = $value; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
$this->found_parameters++; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
if (!isset($aTranslations[$identifier])) { |
164
|
|
|
$aTranslations[$identifier] = TranslatorFacade::hasIdentifier($identifier); |
165
|
|
|
|
166
|
|
|
if (!$aTranslations[$identifier]) { |
167
|
|
|
TranslatorFacade::addMissingIdentifier($identifier, $parameters, $group); |
168
|
|
|
$this->bar->clear(); |
169
|
|
|
$this->info('Adding: "' . $identifier . '" to database'); |
170
|
|
|
$this->bar->display(); |
171
|
|
|
$this->new_identifier++; |
172
|
|
|
} |
173
|
|
|
} else { |
174
|
|
|
$this->dupl_identifier++; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
} catch (\Exception $e) { |
178
|
|
|
$this->bar->clear(); |
179
|
|
|
$this->info($identifier . ' ' . strlen($identifier)); |
180
|
|
|
$this->info($e->getMessage()); |
181
|
|
|
$this->line(''); |
182
|
|
|
$this->bar->display(); |
183
|
|
|
$this->found_invalid++; |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
} |