13
13
14
14
use Symfony \Component \Console \Attribute \AsCommand ;
15
15
use Symfony \Component \Console \Command \Command ;
16
+ use Symfony \Component \Console \Completion \CompletionInput ;
17
+ use Symfony \Component \Console \Completion \CompletionSuggestions ;
16
18
use Symfony \Component \Console \Exception \InvalidArgumentException ;
17
19
use Symfony \Component \Console \Input \InputArgument ;
18
20
use Symfony \Component \Console \Input \InputInterface ;
@@ -43,6 +45,10 @@ class TranslationUpdateCommand extends Command
43
45
private const ASC = 'asc ' ;
44
46
private const DESC = 'desc ' ;
45
47
private const SORT_ORDERS = [self ::ASC , self ::DESC ];
48
+ private const FORMATS = [
49
+ 'xlf12 ' => ['xlf ' , '1.2 ' ],
50
+ 'xlf20 ' => ['xlf ' , '2.0 ' ],
51
+ ];
46
52
47
53
private $ writer ;
48
54
private $ reader ;
@@ -52,8 +58,9 @@ class TranslationUpdateCommand extends Command
52
58
private $ defaultViewsPath ;
53
59
private $ transPaths ;
54
60
private $ codePaths ;
61
+ private $ enabledLocales ;
55
62
56
- public function __construct (TranslationWriterInterface $ writer , TranslationReaderInterface $ reader , ExtractorInterface $ extractor , string $ defaultLocale , string $ defaultTransPath = null , string $ defaultViewsPath = null , array $ transPaths = [], array $ codePaths = [])
63
+ public function __construct (TranslationWriterInterface $ writer , TranslationReaderInterface $ reader , ExtractorInterface $ extractor , string $ defaultLocale , string $ defaultTransPath = null , string $ defaultViewsPath = null , array $ transPaths = [], array $ codePaths = [], array $ enabledLocales = [] )
57
64
{
58
65
parent ::__construct ();
59
66
@@ -65,6 +72,7 @@ public function __construct(TranslationWriterInterface $writer, TranslationReade
65
72
$ this ->defaultViewsPath = $ defaultViewsPath ;
66
73
$ this ->transPaths = $ transPaths ;
67
74
$ this ->codePaths = $ codePaths ;
75
+ $ this ->enabledLocales = $ enabledLocales ;
68
76
}
69
77
70
78
/**
@@ -143,10 +151,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
143
151
$ format = $ input ->getOption ('format ' );
144
152
$ xliffVersion = '1.2 ' ;
145
153
146
- switch ($ format ) {
147
- case 'xlf20 ' : $ xliffVersion = '2.0 ' ;
148
- // no break
149
- case 'xlf12 ' : $ format = 'xlf ' ;
154
+ if (\in_array ($ format , array_keys (self ::FORMATS ), true )) {
155
+ [$ format , $ xliffVersion ] = self ::FORMATS [$ format ];
150
156
}
151
157
152
158
// check format
@@ -161,15 +167,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int
161
167
$ kernel = $ this ->getApplication ()->getKernel ();
162
168
163
169
// Define Root Paths
164
- $ transPaths = $ this ->transPaths ;
165
- if ($ this ->defaultTransPath ) {
166
- $ transPaths [] = $ this ->defaultTransPath ;
167
- }
168
- $ codePaths = $ this ->codePaths ;
169
- $ codePaths [] = $ kernel ->getProjectDir ().'/src ' ;
170
- if ($ this ->defaultViewsPath ) {
171
- $ codePaths [] = $ this ->defaultViewsPath ;
172
- }
170
+ $ transPaths = $ this ->getRootTransPaths ();
171
+ $ codePaths = $ this ->getRootCodePaths ($ kernel );
172
+
173
173
$ currentName = 'default directory ' ;
174
174
175
175
// Override with provided Bundle info
@@ -202,24 +202,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int
202
202
$ io ->title ('Translation Messages Extractor and Dumper ' );
203
203
$ io ->comment (sprintf ('Generating "<info>%s</info>" translation files for "<info>%s</info>" ' , $ input ->getArgument ('locale ' ), $ currentName ));
204
204
205
- // load any messages from templates
206
- $ extractedCatalogue = new MessageCatalogue ($ input ->getArgument ('locale ' ));
207
205
$ io ->comment ('Parsing templates... ' );
208
- $ this ->extractor ->setPrefix ($ input ->getOption ('prefix ' ));
209
- foreach ($ codePaths as $ path ) {
210
- if (is_dir ($ path ) || is_file ($ path )) {
211
- $ this ->extractor ->extract ($ path , $ extractedCatalogue );
212
- }
213
- }
206
+ $ extractedCatalogue = $ this ->extractMessages ($ input ->getArgument ('locale ' ), $ codePaths , $ input ->getOption ('prefix ' ));
214
207
215
- // load any existing messages from the translation files
216
- $ currentCatalogue = new MessageCatalogue ($ input ->getArgument ('locale ' ));
217
208
$ io ->comment ('Loading translation files... ' );
218
- foreach ($ transPaths as $ path ) {
219
- if (is_dir ($ path )) {
220
- $ this ->reader ->read ($ path , $ currentCatalogue );
221
- }
222
- }
209
+ $ currentCatalogue = $ this ->loadCurrentMessages ($ input ->getArgument ('locale ' ), $ transPaths );
223
210
224
211
if (null !== $ domain = $ input ->getOption ('domain ' )) {
225
212
$ currentCatalogue = $ this ->filterCatalogue ($ currentCatalogue , $ domain );
@@ -317,6 +304,60 @@ protected function execute(InputInterface $input, OutputInterface $output): int
317
304
return 0 ;
318
305
}
319
306
307
+ public function complete (CompletionInput $ input , CompletionSuggestions $ suggestions ): void
308
+ {
309
+ if ($ input ->mustSuggestArgumentValuesFor ('locale ' )) {
310
+ $ suggestions ->suggestValues ($ this ->enabledLocales );
311
+
312
+ return ;
313
+ }
314
+
315
+ /** @var KernelInterface $kernel */
316
+ $ kernel = $ this ->getApplication ()->getKernel ();
317
+ if ($ input ->mustSuggestArgumentValuesFor ('bundle ' )) {
318
+ $ bundles = [];
319
+
320
+ foreach ($ kernel ->getBundles () as $ bundle ) {
321
+ $ bundles [] = $ bundle ->getName ();
322
+ if ($ bundle ->getContainerExtension ()) {
323
+ $ bundles [] = $ bundle ->getContainerExtension ()->getAlias ();
324
+ }
325
+ }
326
+
327
+ $ suggestions ->suggestValues ($ bundles );
328
+
329
+ return ;
330
+ }
331
+
332
+ if ($ input ->mustSuggestOptionValuesFor ('format ' )) {
333
+ $ suggestions ->suggestValues (array_merge (
334
+ $ this ->writer ->getFormats (),
335
+ array_keys (self ::FORMATS )
336
+ ));
337
+
338
+ return ;
339
+ }
340
+
341
+ if ($ input ->mustSuggestOptionValuesFor ('domain ' ) && $ locale = $ input ->getArgument ('locale ' )) {
342
+ $ extractedCatalogue = $ this ->extractMessages ($ locale , $ this ->getRootCodePaths ($ kernel ), $ input ->getOption ('prefix ' ));
343
+
344
+ $ currentCatalogue = $ this ->loadCurrentMessages ($ locale , $ this ->getRootTransPaths ());
345
+
346
+ // process catalogues
347
+ $ operation = $ input ->getOption ('clean ' )
348
+ ? new TargetOperation ($ currentCatalogue , $ extractedCatalogue )
349
+ : new MergeOperation ($ currentCatalogue , $ extractedCatalogue );
350
+
351
+ $ suggestions ->suggestValues ($ operation ->getDomains ());
352
+
353
+ return ;
354
+ }
355
+
356
+ if ($ input ->mustSuggestOptionValuesFor ('sort ' )) {
357
+ $ suggestions ->suggestValues (self ::SORT_ORDERS );
358
+ }
359
+ }
360
+
320
361
private function filterCatalogue (MessageCatalogue $ catalogue , string $ domain ): MessageCatalogue
321
362
{
322
363
$ filteredCatalogue = new MessageCatalogue ($ catalogue ->getLocale ());
@@ -349,4 +390,50 @@ private function filterCatalogue(MessageCatalogue $catalogue, string $domain): M
349
390
350
391
return $ filteredCatalogue ;
351
392
}
393
+
394
+ private function extractMessages (string $ locale , array $ transPaths , string $ prefix ): MessageCatalogue
395
+ {
396
+ $ extractedCatalogue = new MessageCatalogue ($ locale );
397
+ $ this ->extractor ->setPrefix ($ prefix );
398
+ foreach ($ transPaths as $ path ) {
399
+ if (is_dir ($ path ) || is_file ($ path )) {
400
+ $ this ->extractor ->extract ($ path , $ extractedCatalogue );
401
+ }
402
+ }
403
+
404
+ return $ extractedCatalogue ;
405
+ }
406
+
407
+ private function loadCurrentMessages (string $ locale , array $ transPaths ): MessageCatalogue
408
+ {
409
+ $ currentCatalogue = new MessageCatalogue ($ locale );
410
+ foreach ($ transPaths as $ path ) {
411
+ if (is_dir ($ path )) {
412
+ $ this ->reader ->read ($ path , $ currentCatalogue );
413
+ }
414
+ }
415
+
416
+ return $ currentCatalogue ;
417
+ }
418
+
419
+ private function getRootTransPaths (): array
420
+ {
421
+ $ transPaths = $ this ->transPaths ;
422
+ if ($ this ->defaultTransPath ) {
423
+ $ transPaths [] = $ this ->defaultTransPath ;
424
+ }
425
+
426
+ return $ transPaths ;
427
+ }
428
+
429
+ private function getRootCodePaths (KernelInterface $ kernel ): array
430
+ {
431
+ $ codePaths = $ this ->codePaths ;
432
+ $ codePaths [] = $ kernel ->getProjectDir ().'/src ' ;
433
+ if ($ this ->defaultViewsPath ) {
434
+ $ codePaths [] = $ this ->defaultViewsPath ;
435
+ }
436
+
437
+ return $ codePaths ;
438
+ }
352
439
}
0 commit comments