19
19
use Symfony \Component \Console \Input \InputArgument ;
20
20
use Symfony \Component \Console \Input \InputInterface ;
21
21
use Symfony \Component \Console \Input \InputOption ;
22
- use Symfony \Component \Console \Output \ConsoleOutputInterface ;
23
22
use Symfony \Component \Console \Output \OutputInterface ;
24
23
use Symfony \Component \Console \Style \SymfonyStyle ;
25
24
use Symfony \Component \HttpKernel \KernelInterface ;
@@ -49,6 +48,7 @@ class TranslationUpdateCommand extends Command
49
48
'xlf12 ' => ['xlf ' , '1.2 ' ],
50
49
'xlf20 ' => ['xlf ' , '2.0 ' ],
51
50
];
51
+ private const NO_FILL_PREFIX = "\0NoFill \0" ;
52
52
53
53
public function __construct (
54
54
private TranslationWriterInterface $ writer ,
@@ -71,6 +71,7 @@ protected function configure(): void
71
71
new InputArgument ('locale ' , InputArgument::REQUIRED , 'The locale ' ),
72
72
new InputArgument ('bundle ' , InputArgument::OPTIONAL , 'The bundle name or directory where to load the messages ' ),
73
73
new InputOption ('prefix ' , null , InputOption::VALUE_OPTIONAL , 'Override the default prefix ' , '__ ' ),
74
+ new InputOption ('no-fill ' , null , InputOption::VALUE_NONE , 'Extract translation keys without filling in values ' ),
74
75
new InputOption ('format ' , null , InputOption::VALUE_OPTIONAL , 'Override the default output format ' , 'xlf12 ' ),
75
76
new InputOption ('dump-messages ' , null , InputOption::VALUE_NONE , 'Should the messages be dumped in the console ' ),
76
77
new InputOption ('force ' , null , InputOption::VALUE_NONE , 'Should the extract be done ' ),
@@ -85,7 +86,8 @@ protected function configure(): void
85
86
the new ones into the translation files.
86
87
87
88
When new translation strings are found it can automatically add a prefix to the translation
88
- message.
89
+ message. However, if the <comment>--no-fill</comment> option is used, the <comment>--prefix</comment>
90
+ option has no effect, since the translation values are left empty.
89
91
90
92
Example running against a Bundle (AcmeBundle)
91
93
@@ -113,9 +115,6 @@ protected function configure(): void
113
115
114
116
protected function execute (InputInterface $ input , OutputInterface $ output ): int
115
117
{
116
- $ io = new SymfonyStyle ($ input , $ output );
117
- $ errorIo = $ output instanceof ConsoleOutputInterface ? new SymfonyStyle ($ input , $ output ->getErrorOutput ()) : $ io ;
118
-
119
118
$ io = new SymfonyStyle ($ input , $ output );
120
119
$ errorIo = $ io ->getErrorStyle ();
121
120
@@ -181,7 +180,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
181
180
$ io ->comment (\sprintf ('Generating "<info>%s</info>" translation files for "<info>%s</info>" ' , $ input ->getArgument ('locale ' ), $ currentName ));
182
181
183
182
$ io ->comment ('Parsing templates... ' );
184
- $ extractedCatalogue = $ this ->extractMessages ($ input ->getArgument ('locale ' ), $ codePaths , $ input ->getOption ('prefix ' ));
183
+ $ prefix = $ input ->getOption ('no-fill ' ) ? self ::NO_FILL_PREFIX : $ input ->getOption ('prefix ' );
184
+ $ extractedCatalogue = $ this ->extractMessages ($ input ->getArgument ('locale ' ), $ codePaths , $ prefix );
185
185
186
186
$ io ->comment ('Loading translation files... ' );
187
187
$ currentCatalogue = $ this ->loadCurrentMessages ($ input ->getArgument ('locale ' ), $ transPaths );
@@ -271,6 +271,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int
271
271
$ operationResult = $ this ->sortCatalogue ($ operationResult , $ sort );
272
272
}
273
273
274
+ if (true === $ input ->getOption ('no-fill ' )) {
275
+ $ this ->removeNoFillTranslations ($ operationResult );
276
+ }
277
+
274
278
$ this ->writer ->write ($ operationResult , $ format , ['path ' => $ bundleTransPath , 'default_locale ' => $ this ->defaultLocale , 'xliff_version ' => $ xliffVersion , 'as_tree ' => $ input ->getOption ('as-tree ' ), 'inline ' => $ input ->getOption ('as-tree ' ) ?? 0 ]);
275
279
276
280
if (true === $ input ->getOption ('dump-messages ' )) {
@@ -485,4 +489,13 @@ private function getRootCodePaths(KernelInterface $kernel): array
485
489
486
490
return $ codePaths ;
487
491
}
492
+
493
+ private function removeNoFillTranslations (MessageCatalogueInterface $ operation ): void
494
+ {
495
+ foreach ($ operation ->all ('messages ' ) as $ key => $ message ) {
496
+ if (str_starts_with ($ message , self ::NO_FILL_PREFIX )) {
497
+ $ operation ->set ($ key , '' , 'messages ' );
498
+ }
499
+ }
500
+ }
488
501
}
0 commit comments