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