Skip to content

[FrameworkBundle] Add --no-fill option to translation:extract command #58506

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ CHANGELOG
* Add the ability to use an existing service as a lock/semaphore resource
* Add support for configuring multiple serializer instances via the configuration
* Add support for `SYMFONY_TRUSTED_PROXIES`, `SYMFONY_TRUSTED_HEADERS`, `SYMFONY_TRUST_X_SENDFILE_TYPE_HEADER` and `SYMFONY_TRUSTED_HOSTS` env vars
* Add `--no-fill` option to `translation:extract` command

7.1
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\ConsoleOutputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\HttpKernel\KernelInterface;
Expand Down Expand Up @@ -49,6 +48,7 @@ class TranslationUpdateCommand extends Command
'xlf12' => ['xlf', '1.2'],
'xlf20' => ['xlf', '2.0'],
];
private const NO_FILL_PREFIX = "\0NoFill\0";

public function __construct(
private TranslationWriterInterface $writer,
Expand All @@ -71,6 +71,7 @@ protected function configure(): void
new InputArgument('locale', InputArgument::REQUIRED, 'The locale'),
new InputArgument('bundle', InputArgument::OPTIONAL, 'The bundle name or directory where to load the messages'),
new InputOption('prefix', null, InputOption::VALUE_OPTIONAL, 'Override the default prefix', '__'),
new InputOption('no-fill', null, InputOption::VALUE_NONE, 'Extract translation keys without filling in values'),
new InputOption('format', null, InputOption::VALUE_OPTIONAL, 'Override the default output format', 'xlf12'),
new InputOption('dump-messages', null, InputOption::VALUE_NONE, 'Should the messages be dumped in the console'),
new InputOption('force', null, InputOption::VALUE_NONE, 'Should the extract be done'),
Expand All @@ -85,7 +86,8 @@ protected function configure(): void
the new ones into the translation files.

When new translation strings are found it can automatically add a prefix to the translation
message.
message. However, if the <comment>--no-fill</comment> option is used, the <comment>--prefix</comment>
option has no effect, since the translation values are left empty.

Example running against a Bundle (AcmeBundle)

Expand Down Expand Up @@ -113,9 +115,6 @@ protected function configure(): void

protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$errorIo = $output instanceof ConsoleOutputInterface ? new SymfonyStyle($input, $output->getErrorOutput()) : $io;

$io = new SymfonyStyle($input, $output);
$errorIo = $io->getErrorStyle();

Expand Down Expand Up @@ -181,7 +180,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$io->comment(\sprintf('Generating "<info>%s</info>" translation files for "<info>%s</info>"', $input->getArgument('locale'), $currentName));

$io->comment('Parsing templates...');
$extractedCatalogue = $this->extractMessages($input->getArgument('locale'), $codePaths, $input->getOption('prefix'));
$prefix = $input->getOption('no-fill') ? self::NO_FILL_PREFIX : $input->getOption('prefix');
$extractedCatalogue = $this->extractMessages($input->getArgument('locale'), $codePaths, $prefix);

$io->comment('Loading translation files...');
$currentCatalogue = $this->loadCurrentMessages($input->getArgument('locale'), $transPaths);
Expand Down Expand Up @@ -271,6 +271,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$operationResult = $this->sortCatalogue($operationResult, $sort);
}

if (true === $input->getOption('no-fill')) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (true === $input->getOption('no-fill')) {
if ($input->getOption('no-fill')) {

$this->removeNoFillTranslations($operationResult);
}

$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]);

if (true === $input->getOption('dump-messages')) {
Expand Down Expand Up @@ -485,4 +489,13 @@ private function getRootCodePaths(KernelInterface $kernel): array

return $codePaths;
}

private function removeNoFillTranslations(MessageCatalogueInterface $operation): void
{
foreach ($operation->all('messages') as $key => $message) {
if (str_starts_with($message, self::NO_FILL_PREFIX)) {
$operation->set($key, '', 'messages');
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Translation\Extractor\ExtractorInterface;
use Symfony\Component\Translation\MessageCatalogue;
use Symfony\Component\Translation\MessageCatalogueInterface;
use Symfony\Component\Translation\Reader\TranslationReader;
use Symfony\Component\Translation\Translator;
use Symfony\Component\Translation\Writer\TranslationWriter;
Expand Down Expand Up @@ -176,6 +177,45 @@ public function testFilterDuplicateTransPaths()
$this->assertEquals($expectedPaths, $filteredTransPaths);
}

/**
* @dataProvider removeNoFillProvider
*/
public function testRemoveNoFillTranslationsMethod($noFillCounter, $messages)
{
// Preparing mock
$operation = $this->createMock(MessageCatalogueInterface::class);
$operation
->method('all')
->with('messages')
->willReturn($messages);
$operation
->expects($this->exactly($noFillCounter))
->method('set');

// Calling private method
$translationUpdate = $this->createMock(TranslationUpdateCommand::class);
$reflection = new \ReflectionObject($translationUpdate);
$method = $reflection->getMethod('removeNoFillTranslations');
$method->invokeArgs($translationUpdate, [$operation]);
}

public function removeNoFillProvider(): array
{
return [
[0, []],
[0, ['foo' => 'foo', 'bar' => 'bar', 'baz' => 'baz']],
[0, ['foo' => "\0foo"]],
[0, ['foo' => "foo\0NoFill\0"]],
[0, ['foo' => "f\0NoFill\000"]],
[0, ['foo' => 'foo', 'bar' => 'bar']],
[1, ['foo' => "\0NoFill\0foo"]],
[1, ['foo' => "\0NoFill\0foo", 'bar' => 'bar']],
[1, ['foo' => 'foo', 'bar' => "\0NoFill\0bar"]],
[2, ['foo' => "\0NoFill\0foo", 'bar' => "\0NoFill\0bar"]],
[3, ['foo' => "\0NoFill\0foo", 'bar' => "\0NoFill\0bar", 'baz' => "\0NoFill\0baz"]],
];
}

protected function setUp(): void
{
$this->fs = new Filesystem();
Expand Down
Loading