|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Symfony\Component\Translation\Command; |
| 4 | + |
| 5 | +use Symfony\Component\Console\Attribute\AsCommand; |
| 6 | +use Symfony\Component\Console\Command\Command; |
| 7 | +use Symfony\Component\Console\Completion\CompletionInput; |
| 8 | +use Symfony\Component\Console\Completion\CompletionSuggestions; |
| 9 | +use Symfony\Component\Console\Input\InputArgument; |
| 10 | +use Symfony\Component\Console\Input\InputInterface; |
| 11 | +use Symfony\Component\Console\Input\InputOption; |
| 12 | +use Symfony\Component\Console\Output\OutputInterface; |
| 13 | +use Symfony\Component\Console\Style\SymfonyStyle; |
| 14 | +use Symfony\Component\Translation\MessageCatalogue; |
| 15 | +use Symfony\Component\Translation\MetadataAwareInterface; |
| 16 | +use Symfony\Component\Translation\Reader\TranslationReaderInterface; |
| 17 | +use Symfony\Component\Translation\Writer\TranslationWriterInterface; |
| 18 | + |
| 19 | +/** |
| 20 | + * @author Nicolas Rigaud <squrious@protonmail.com> |
| 21 | + */ |
| 22 | +#[AsCommand(name: 'translation:update-xliff-sources', description: 'Update source tags with default locale targets in XLIFF files.')] |
| 23 | +class XliffUpdateSourcesCommand extends Command |
| 24 | +{ |
| 25 | + use TranslationTrait; |
| 26 | + |
| 27 | + private const FORMATS = [ |
| 28 | + 'xlf12' => ['xlf', '1.2'], |
| 29 | + 'xlf20' => ['xlf', '2.0'], |
| 30 | + ]; |
| 31 | + |
| 32 | + public function __construct( |
| 33 | + private readonly TranslationWriterInterface $writer, |
| 34 | + private readonly TranslationReaderInterface $reader, |
| 35 | + private readonly string $defaultLocale, |
| 36 | + private readonly array $transPaths = [], |
| 37 | + private readonly array $enabledLocales = [], |
| 38 | + ) { |
| 39 | + parent::__construct(); |
| 40 | + } |
| 41 | + |
| 42 | + protected function configure(): void |
| 43 | + { |
| 44 | + $this |
| 45 | + ->setDefinition([ |
| 46 | + new InputArgument('paths', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, 'Paths to look for translations.'), |
| 47 | + new InputOption('format', null, InputOption::VALUE_REQUIRED, 'Override the default output format.', 'xlf12'), |
| 48 | + new InputOption('domains', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'Specify the domain to update.'), |
| 49 | + new InputOption('locales', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Specify the locale to update.', $this->enabledLocales), |
| 50 | + ]) |
| 51 | + ->setHelp(<<<EOF |
| 52 | +The <info>%command.name%</info> command updates the source tags in XLIFF files with the default locale translation if available. |
| 53 | +
|
| 54 | +You can specify directories to update in the command arguments: |
| 55 | +
|
| 56 | + <info>php %command.full_name% path/to/dir path/to/file.xlf</info> |
| 57 | +
|
| 58 | +To restrict the updates to one or more locales, including the default locale itself, use the <comment>--locales</comment> option: |
| 59 | +
|
| 60 | + <info>php %command.full_name% --locales en --locales fr</info> |
| 61 | +
|
| 62 | +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. |
| 63 | +
|
| 64 | + <info>php %command.full_name% --domains messages</info> |
| 65 | +
|
| 66 | +EOF |
| 67 | + ) |
| 68 | + ; |
| 69 | + } |
| 70 | + |
| 71 | + protected function execute(InputInterface $input, OutputInterface $output): int |
| 72 | + { |
| 73 | + $io = new SymfonyStyle($input, $output); |
| 74 | + |
| 75 | + $format = $input->getOption('format'); |
| 76 | + |
| 77 | + if (!\array_key_exists($format, self::FORMATS)) { |
| 78 | + $io->error(sprintf('Unknown format "%s". Available formats are: %s.', $format, \implode(', ', \array_map(fn($f) => '"'.$f.'"', \array_keys(self::FORMATS))))); |
| 79 | + |
| 80 | + return self::INVALID; |
| 81 | + } |
| 82 | + |
| 83 | + [$format, $xliffVersion] = self::FORMATS[$format]; |
| 84 | + |
| 85 | + $locales = $input->getOption('locales'); |
| 86 | + |
| 87 | + if (!$locales) { |
| 88 | + $io->error('No locales provided in --locales options and no defaults provided to the command.'); |
| 89 | + |
| 90 | + return self::INVALID; |
| 91 | + } |
| 92 | + |
| 93 | + $transPaths = $input->getArgument('paths') ?: $this->transPaths; |
| 94 | + |
| 95 | + if (!$transPaths) { |
| 96 | + $io->error('No paths specified in arguments, and no default paths provided to the command.'); |
| 97 | + |
| 98 | + return self::INVALID; |
| 99 | + } |
| 100 | + |
| 101 | + $domains = $input->getOption('domains'); |
| 102 | + |
| 103 | + $io->title('XLIFF Source Tag Updater'); |
| 104 | + |
| 105 | + foreach ($transPaths as $transPath) { |
| 106 | + $io->comment(sprintf('Updating XLIFF files in <info>%s</info>...', $transPath)); |
| 107 | + |
| 108 | + $translatorBag = $this->readLocalTranslations(\array_unique(\array_merge($locales, [$this->defaultLocale])), $domains, [$transPath]); |
| 109 | + |
| 110 | + $defaultLocaleCatalogue = $translatorBag->getCatalogue($this->defaultLocale); |
| 111 | + |
| 112 | + if (!$defaultLocaleCatalogue instanceof MetadataAwareInterface) { |
| 113 | + $io->error(sprintf('The default locale catalogue must implement "%s" to be used in this tool.', MetadataAwareInterface::class)); |
| 114 | + |
| 115 | + return self::FAILURE; |
| 116 | + } |
| 117 | + |
| 118 | + foreach ($locales as $locale) { |
| 119 | + $currentCatalogue = $translatorBag->getCatalogue($locale); |
| 120 | + |
| 121 | + if (!$currentCatalogue instanceof MessageCatalogue) { |
| 122 | + $io->warning(sprintf('The catalogue for locale "%s" must be an instance of "%s" to be used in this tool.', $locale, MessageCatalogue::class)); |
| 123 | + |
| 124 | + continue; |
| 125 | + } |
| 126 | + |
| 127 | + if (!\count($currentCatalogue->getDomains())) { |
| 128 | + $io->warning(sprintf('No messages found for locale "%s".', $locale)); |
| 129 | + |
| 130 | + continue; |
| 131 | + } |
| 132 | + |
| 133 | + $updateSourceCount = 0; |
| 134 | + |
| 135 | + foreach ($currentCatalogue->getDomains() as $domain) { |
| 136 | + // Update source metadata with default locale target for each message in result catalogue |
| 137 | + foreach ($currentCatalogue->all($domain) as $key => $value) { |
| 138 | + if (!$defaultLocaleCatalogue->has($key, $domain)) { |
| 139 | + continue; |
| 140 | + } |
| 141 | + |
| 142 | + $resultMetadata = $currentCatalogue->getMetadata($key, $domain); |
| 143 | + $defaultTranslation = $defaultLocaleCatalogue->get($key, $domain); |
| 144 | + if (!isset($resultMetadata['source']) || $resultMetadata['source'] !== $defaultTranslation) { |
| 145 | + $updateSourceCount++; |
| 146 | + $resultMetadata['source'] = $defaultTranslation; |
| 147 | + $currentCatalogue->setMetadata($key, $resultMetadata, $domain); |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + $this->writer->write($currentCatalogue, $format, ['path' => $transPath, 'default_locale' => $this->defaultLocale, 'xliff_version' => $xliffVersion ]); |
| 153 | + |
| 154 | + if (0 === $updateSourceCount) { |
| 155 | + $message = sprintf('All source tags are already up-to-date for locale "%s".', $locale); |
| 156 | + } else { |
| 157 | + $message = sprintf('Updated %d source tag%s for locale "%s".', $updateSourceCount, $updateSourceCount > 1 ? 's' : '',$locale); |
| 158 | + } |
| 159 | + |
| 160 | + $io->info($message); |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + $io->success('Operation succeeded.'); |
| 165 | + |
| 166 | + return self::SUCCESS; |
| 167 | + } |
| 168 | + |
| 169 | + public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void |
| 170 | + { |
| 171 | + if ($input->mustSuggestOptionValuesFor('locales')) { |
| 172 | + $suggestions->suggestValues($this->enabledLocales); |
| 173 | + |
| 174 | + return; |
| 175 | + } |
| 176 | + |
| 177 | + if ($input->mustSuggestOptionValuesFor('format')) { |
| 178 | + $suggestions->suggestValues(array_keys(self::FORMATS)); |
| 179 | + |
| 180 | + return; |
| 181 | + } |
| 182 | + |
| 183 | + if ($input->mustSuggestOptionValuesFor('domains') && $locales = $input->getOption('locales')) { |
| 184 | + $suggestedDomains = []; |
| 185 | + $translatorBag = $this->readLocalTranslations($locales, [], $input->getArgument('paths') ?: $this->transPaths); |
| 186 | + foreach ($translatorBag->getCatalogues() as $catalogue) { |
| 187 | + \array_push($suggestedDomains, ...$catalogue->getDomains()); |
| 188 | + } |
| 189 | + if ($suggestedDomains) { |
| 190 | + $suggestions->suggestValues(\array_unique($suggestedDomains)); |
| 191 | + } |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | +} |
0 commit comments