Skip to content

[FrameworkBundle] [Translation] Update XLIFF source tags with new translation:update-xliff-sources command #53630

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

Open
wants to merge 3 commits into
base: 7.4
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Move to a dedicated command in the Translation component
  • Loading branch information
squrious committed Sep 21, 2024
commit cdf99d3c8edf30d5653ccc225aaf5c4fa35d0c20
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ protected function configure(): void
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'),
new InputOption('clean', null, InputOption::VALUE_NONE, 'Should clean not found messages'),
new InputOption('update-source', null, InputOption::VALUE_NONE, 'Should update source tag in XLIFF'),
new InputOption('domain', null, InputOption::VALUE_OPTIONAL, 'Specify the domain to extract'),
new InputOption('sort', null, InputOption::VALUE_OPTIONAL, 'Return list of messages sorted alphabetically (only works with --dump-messages)', 'asc'),
new InputOption('as-tree', null, InputOption::VALUE_OPTIONAL, 'Dump the messages as a tree-like structure: The given value defines the level where to switch to inline YAML'),
Expand Down Expand Up @@ -267,31 +266,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$bundleTransPath = end($transPaths);
}

$resultCatalogue = $operation->getResult();
$writerOptions = ['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('update-source')) {
$defaultLocaleCatalogue = $this->loadCurrentMessages($this->defaultLocale, $transPaths);

$domains = (null !== $domain) ? [$domain] : $resultCatalogue->getDomains();

// Update source metadata with default locale target for each message in result catalogue
foreach ($domains as $domain) {
foreach ($resultCatalogue->all($domain) as $key => $value) {
if (!$defaultLocaleCatalogue->has($key, $domain)) {
continue;
}

$resultMetadata = $resultCatalogue->getMetadata($key, $domain);
if (!isset($resultMetadata['source']) || $resultMetadata['source'] === $key) {
$resultMetadata['source'] = $defaultLocaleCatalogue->get($key, $domain);
$resultCatalogue->setMetadata($key, $resultMetadata, $domain);
}
}
}
}

$this->writer->write($resultCatalogue, $format, $writerOptions);
$this->writer->write($operation->getResult(), $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')) {
$resultMessage .= ' and translation files were updated';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1420,6 +1420,7 @@ private function registerTranslatorConfiguration(array $config, ContainerBuilder
$container->removeDefinition('console.command.translation_pull');
$container->removeDefinition('console.command.translation_push');
$container->removeDefinition('console.command.translation_lint');
$container->removeDefinition('console.command.translation_xliff_update_sources');

return;
}
Expand Down Expand Up @@ -1497,6 +1498,12 @@ private function registerTranslatorConfiguration(array $config, ContainerBuilder
$container->getDefinition('console.command.translation_extract')->replaceArgument(6, $transPaths);
}

if ($container->hasDefinition('console.command.translation_xliff_update_sources')) {
$container->getDefinition('console.command.translation_xliff_update_sources')
->replaceArgument(3, array_merge($transPaths, [$config['default_path']]))
;
}

if (null === $defaultDir) {
// allow null
} elseif ($container->fileExists($defaultDir)) {
Expand Down
11 changes: 11 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/Resources/config/console.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
use Symfony\Component\Translation\Command\TranslationPullCommand;
use Symfony\Component\Translation\Command\TranslationPushCommand;
use Symfony\Component\Translation\Command\XliffLintCommand;
use Symfony\Component\Translation\Command\XliffUpdateSourcesCommand;
use Symfony\Component\Validator\Command\DebugCommand as ValidatorDebugCommand;

return static function (ContainerConfigurator $container) {
Expand Down Expand Up @@ -306,6 +307,16 @@
])
->tag('console.command', ['command' => 'translation:push'])

->set('console.command.translation_xliff_update_sources', XliffUpdateSourcesCommand::class)
->args([
service('translation.writer'),
service('translation.reader'),
param('kernel.default_locale'),
[], // Translator paths
param('kernel.enabled_locales'),
])
->tag('console.command')

->set('console.command.workflow_dump', WorkflowDumpCommand::class)
->args([
tagged_locator('workflow', 'name'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,6 @@ public function testFilterDuplicateTransPaths()
$this->assertEquals($expectedPaths, $filteredTransPaths);
}

public function testWriteMessagesWithUpdateSourceOption()
{
$tester = $this->createCommandTester(['messages' => ['foo' => 'foo']]);
$tester->execute(['command' => 'translation:extract', 'locale' => 'en', '--force' => true, '--update-source' => true]);
$this->assertMatchesRegularExpression('/Translation files were successfully updated\./', $tester->getDisplay());
}

protected function setUp(): void
{
$this->fs = new Filesystem();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Translation\Command;

use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Completion\CompletionInput;
use Symfony\Component\Console\Completion\CompletionSuggestions;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Translation\MessageCatalogue;
use Symfony\Component\Translation\MetadataAwareInterface;
use Symfony\Component\Translation\Reader\TranslationReaderInterface;
use Symfony\Component\Translation\Writer\TranslationWriterInterface;

/**
* @author Nicolas Rigaud <squrious@protonmail.com>
*/
#[AsCommand(name: 'translation:update-xliff-sources', description: 'Update source tags with default locale targets in XLIFF files.')]
class XliffUpdateSourcesCommand extends Command
{
use TranslationTrait;

private const FORMATS = [
'xlf12' => ['xlf', '1.2'],
'xlf20' => ['xlf', '2.0'],
];

public function __construct(
private readonly TranslationWriterInterface $writer,
private readonly TranslationReaderInterface $reader,
private readonly string $defaultLocale,
private readonly array $transPaths = [],
private readonly array $enabledLocales = [],
) {
parent::__construct();
}

protected function configure(): void
{
$this
->setDefinition([
new InputArgument('paths', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, 'Paths to look for translations.'),
new InputOption('format', null, InputOption::VALUE_REQUIRED, 'Override the default output format.', 'xlf12'),
new InputOption('domains', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'Specify the domain to update.'),
new InputOption('locales', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Specify the locale to update.', $this->enabledLocales),
])
->setHelp(<<<EOF
The <info>%command.name%</info> command updates the source tags in XLIFF files with the default locale translation if available.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
The <info>%command.name%</info> command updates the source tags in XLIFF files with the default locale translation if available.
The <info>%command.name%</info> command updates the source tags in XLIFF files with the default translation locale if available.

?


You can specify directories to update in the command arguments:

<info>php %command.full_name% path/to/dir path/to/another/dir</info>

To restrict the updates to one or more locales, including the default locale itself, use the <comment>--locales</comment> option:

<info>php %command.full_name% --locales en --locales fr</info>

You can specify one or more domains to target with the <comment>--domains</comment> option. By default, all available domains for the targeted locales are used.

<info>php %command.full_name% --domains messages</info>

EOF
)
;
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);

$format = $input->getOption('format');

if (!\array_key_exists($format, self::FORMATS)) {
$io->error(sprintf('Unknown format "%s". Available formats are: %s.', $format, implode(', ', array_map(fn ($f) => '"'.$f.'"', array_keys(self::FORMATS)))));

return self::INVALID;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

shouldn't we move this to initialize method?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure about that, I can't return any code from the initialize method to indicate the option is invalid, so I would have to throw an exception instead.

I used the same logic as in TranslationUpdateCommand.


[$format, $xliffVersion] = self::FORMATS[$format];

$locales = $input->getOption('locales');

if (!$locales) {
$io->error('No locales provided in --locales options and no defaults provided to the command.');

return self::INVALID;
}

$transPaths = $input->getArgument('paths') ?: $this->transPaths;

if (!$transPaths) {
$io->error('No paths specified in arguments, and no default paths provided to the command.');

return self::INVALID;
}

$domains = $input->getOption('domains');

$io->title('XLIFF Source Tag Updater');

foreach ($transPaths as $transPath) {
$io->comment(sprintf('Updating XLIFF files in <info>%s</info>...', $transPath));

$translatorBag = $this->readLocalTranslations(array_unique(array_merge($locales, [$this->defaultLocale])), $domains, [$transPath]);

$defaultLocaleCatalogue = $translatorBag->getCatalogue($this->defaultLocale);

if (!$defaultLocaleCatalogue instanceof MetadataAwareInterface) {
$io->error(sprintf('The default locale catalogue must implement "%s" to be used in this tool.', MetadataAwareInterface::class));
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
$io->error(sprintf('The default locale catalogue must implement "%s" to be used in this tool.', MetadataAwareInterface::class));
$io->error(sprintf('The default locale catalogue must implement "%s" to be used in by this command.', MetadataAwareInterface::class));


return self::FAILURE;
}

foreach ($locales as $locale) {
$currentCatalogue = $translatorBag->getCatalogue($locale);

if (!$currentCatalogue instanceof MessageCatalogue) {
$io->warning(sprintf('The catalogue for locale "%s" must be an instance of "%s" to be used in this tool.', $locale, MessageCatalogue::class));
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
$io->warning(sprintf('The catalogue for locale "%s" must be an instance of "%s" to be used in this tool.', $locale, MessageCatalogue::class));
$io->warning(sprintf('The catalogue for locale "%s" must be an instance of "%s" to be used by this command.', $locale, MessageCatalogue::class));


continue;
}

if (!\count($currentCatalogue->getDomains())) {
$io->warning(sprintf('No messages found for locale "%s".', $locale));

continue;
}

$updateSourceCount = 0;

foreach ($currentCatalogue->getDomains() as $domain) {
// Update source metadata with default locale target for each message in result catalogue
foreach ($currentCatalogue->all($domain) as $key => $value) {
if (!$defaultLocaleCatalogue->has($key, $domain)) {
continue;
}

$resultMetadata = $currentCatalogue->getMetadata($key, $domain);
$defaultTranslation = $defaultLocaleCatalogue->get($key, $domain);
if (!isset($resultMetadata['source']) || $resultMetadata['source'] !== $defaultTranslation) {
++$updateSourceCount;
$resultMetadata['source'] = $defaultTranslation;
$currentCatalogue->setMetadata($key, $resultMetadata, $domain);
}
}
}

$this->writer->write($currentCatalogue, $format, ['path' => $transPath, 'default_locale' => $this->defaultLocale, 'xliff_version' => $xliffVersion]);

if (0 === $updateSourceCount) {
$message = sprintf('All source tags are already up-to-date for locale "%s".', $locale);
} else {
$message = sprintf('Updated %d source tag%s for locale "%s".', $updateSourceCount, $updateSourceCount > 1 ? 's' : '', $locale);
}

$io->info($message);
}
}

$io->success('Operation succeeded.');

return self::SUCCESS;
}

public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void
{
if ($input->mustSuggestOptionValuesFor('locales')) {
$suggestions->suggestValues($this->enabledLocales);

return;
}

if ($input->mustSuggestOptionValuesFor('format')) {
$suggestions->suggestValues(array_keys(self::FORMATS));

return;
}

if ($input->mustSuggestOptionValuesFor('domains') && $locales = $input->getOption('locales')) {
$suggestedDomains = [];
$translatorBag = $this->readLocalTranslations($locales, [], $input->getArgument('paths') ?: $this->transPaths);
foreach ($translatorBag->getCatalogues() as $catalogue) {
array_push($suggestedDomains, ...$catalogue->getDomains());
}
if ($suggestedDomains) {
$suggestions->suggestValues(array_unique($suggestedDomains));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,9 @@ private function extractXliff2(\DOMDocument $dom, MessageCatalogue $catalogue, s
$catalogue->set((string) $source, $target, $domain);

$metadata = [];

$metadata['source'] = (string) $segment->source;

if ($segment->attributes()) {
$metadata['segment-attributes'] = [];
foreach ($segment->attributes() as $key => $value) {
Expand Down
Loading
Loading