|
| 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 | +namespace Symfony\Bundle\FrameworkBundle\Command; |
| 13 | + |
| 14 | +use Symfony\Component\Console\Command\Command; |
| 15 | +use Symfony\Component\Console\Input\InputArgument; |
| 16 | +use Symfony\Component\Console\Input\InputInterface; |
| 17 | +use Symfony\Component\Console\Input\InputOption; |
| 18 | +use Symfony\Component\Console\Output\OutputInterface; |
| 19 | +use Symfony\Component\Console\Style\SymfonyStyle; |
| 20 | +use Symfony\Component\Translation\Catalogue\TargetOperation; |
| 21 | +use Symfony\Component\Translation\MessageCatalogue; |
| 22 | +use Symfony\Component\Translation\Provider\TranslationProviders; |
| 23 | +use Symfony\Component\Translation\Reader\TranslationReaderInterface; |
| 24 | +use Symfony\Component\Translation\Writer\TranslationWriterInterface; |
| 25 | + |
| 26 | +/** |
| 27 | + * @author Mathieu Santostefano <msantostefano@protonmail.com> |
| 28 | + * |
| 29 | + * @experimental in 5.3 |
| 30 | + */ |
| 31 | +final class TranslationPullCommand extends Command |
| 32 | +{ |
| 33 | + use TranslationTrait; |
| 34 | + |
| 35 | + protected static $defaultName = 'translation:pull'; |
| 36 | + |
| 37 | + private $providers; |
| 38 | + private $writer; |
| 39 | + private $reader; |
| 40 | + private $defaultLocale; |
| 41 | + private $transPaths; |
| 42 | + private $enabledLocales; |
| 43 | + |
| 44 | + public function __construct(TranslationProviders $providers, TranslationWriterInterface $writer, TranslationReaderInterface $reader, string $defaultLocale, string $defaultTransPath = null, array $transPaths = [], array $enabledLocales = []) |
| 45 | + { |
| 46 | + $this->providers = $providers; |
| 47 | + $this->writer = $writer; |
| 48 | + $this->reader = $reader; |
| 49 | + $this->defaultLocale = $defaultLocale; |
| 50 | + $this->transPaths = $transPaths; |
| 51 | + $this->enabledLocales = $enabledLocales; |
| 52 | + |
| 53 | + if (null !== $defaultTransPath) { |
| 54 | + $this->transPaths[] = $defaultTransPath; |
| 55 | + } |
| 56 | + |
| 57 | + parent::__construct(); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * {@inheritdoc} |
| 62 | + */ |
| 63 | + protected function configure() |
| 64 | + { |
| 65 | + $keys = $this->providers->keys(); |
| 66 | + $defaultProvider = 1 === \count($keys) ? $keys[0] : null; |
| 67 | + |
| 68 | + $this |
| 69 | + ->setDefinition([ |
| 70 | + new InputArgument('provider', null !== $defaultProvider ? InputArgument::OPTIONAL : InputArgument::REQUIRED, 'The provider to pull translations from.', $defaultProvider), |
| 71 | + new InputOption('force', null, InputOption::VALUE_NONE, 'Override existing translations with provider ones (it will delete not synchronized messages).'), |
| 72 | + new InputOption('delete-obsolete', null, InputOption::VALUE_NONE, 'Delete translations available locally but not on provider.'), |
| 73 | + new InputOption('domains', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'Specify the domains to pull.'), |
| 74 | + new InputOption('locales', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'Specify the locales to pull.'), |
| 75 | + new InputOption('output-format', null, InputOption::VALUE_OPTIONAL, 'Override the default output format.', 'xlf'), |
| 76 | + new InputOption('xliff-version', null, InputOption::VALUE_OPTIONAL, 'Override the default xliff version.', '1.2'), |
| 77 | + ]) |
| 78 | + ->setDescription('Pull translations from a given provider.') |
| 79 | + ->setHelp(<<<'EOF' |
| 80 | +The <info>%command.name%</info> pull translations from the given provider. Only |
| 81 | +new translations are pulled, existing ones are not overwritten. |
| 82 | +
|
| 83 | +You can overwrite existing translations: |
| 84 | +
|
| 85 | + <info>php %command.full_name% --force provider</info> |
| 86 | +
|
| 87 | +You can remove local translations which are not present on the provider: |
| 88 | +
|
| 89 | + <info>php %command.full_name% --delete-obsolete provider</info> |
| 90 | +
|
| 91 | +Full example: |
| 92 | +
|
| 93 | + <info>php %command.full_name% provider --force --delete-obsolete --domains=messages,validators --locales=en</info> |
| 94 | +
|
| 95 | +This command will pull all translations linked to domains messages and validators |
| 96 | +for the locale en. Local translations for the specified domains and locale will |
| 97 | +be erased if they're not present on the provider and overwritten if it's the |
| 98 | +case. Local translations for others domains and locales will be ignored. |
| 99 | +EOF |
| 100 | + ) |
| 101 | + ; |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * {@inheritdoc} |
| 106 | + */ |
| 107 | + protected function execute(InputInterface $input, OutputInterface $output): int |
| 108 | + { |
| 109 | + $io = new SymfonyStyle($input, $output); |
| 110 | + |
| 111 | + $provider = $this->providers->get($input->getArgument('provider')); |
| 112 | + $locales = $input->getOption('locales') ?: $this->enabledLocales; |
| 113 | + $domains = $input->getOption('domains'); |
| 114 | + $force = $input->getOption('force'); |
| 115 | + $deleteObsolete = $input->getOption('delete-obsolete'); |
| 116 | + |
| 117 | + $writeOptions = [ |
| 118 | + 'path' => end($this->transPaths), |
| 119 | + 'xliff_version' => $input->getOption('xliff-version'), |
| 120 | + ]; |
| 121 | + |
| 122 | + if (!$domains) { |
| 123 | + $domains = $provider->getDomains(); |
| 124 | + } |
| 125 | + |
| 126 | + $providerTranslations = $provider->read($domains, $locales); |
| 127 | + |
| 128 | + if ($force) { |
| 129 | + if ($deleteObsolete) { |
| 130 | + $io->note('The --delete-obsolete option is ineffective with --force'); |
| 131 | + } |
| 132 | + |
| 133 | + foreach ($providerTranslations->getCatalogues() as $catalogue) { |
| 134 | + $operation = new TargetOperation((new MessageCatalogue($catalogue->getLocale())), $catalogue); |
| 135 | + $operation->moveMessagesToIntlDomainsIfPossible(); |
| 136 | + $this->writer->write($operation->getResult(), $input->getOption('output-format'), $writeOptions); |
| 137 | + } |
| 138 | + |
| 139 | + $io->success(sprintf( |
| 140 | + 'Local translations has been updated from %s (for [%s] locale(s), and [%s] domain(s)).', |
| 141 | + $provider->getName(), |
| 142 | + implode(', ', $locales), |
| 143 | + implode(', ', $domains) |
| 144 | + )); |
| 145 | + |
| 146 | + return 0; |
| 147 | + } |
| 148 | + |
| 149 | + $localTranslations = $this->readLocalTranslations($locales, $domains, $this->transPaths); |
| 150 | + |
| 151 | + if ($deleteObsolete) { |
| 152 | + $obsoleteTranslations = $localTranslations->diff($providerTranslations); |
| 153 | + $translationsWithoutObsoleteToWrite = $localTranslations->diff($obsoleteTranslations); |
| 154 | + |
| 155 | + foreach ($translationsWithoutObsoleteToWrite->getCatalogues() as $catalogue) { |
| 156 | + $this->writer->write($catalogue, $input->getOption('output-format'), $writeOptions); |
| 157 | + } |
| 158 | + |
| 159 | + $io->success('Obsolete translations has been locally removed.'); |
| 160 | + } |
| 161 | + |
| 162 | + $translationsToWrite = $providerTranslations->diff($localTranslations); |
| 163 | + |
| 164 | + foreach ($translationsToWrite->getCatalogues() as $catalogue) { |
| 165 | + $this->writer->write($catalogue, $input->getOption('output-format'), $writeOptions); |
| 166 | + } |
| 167 | + |
| 168 | + $io->success(sprintf( |
| 169 | + 'New translations from %s has been written locally (for [%s] locale(s), and [%s] domain(s)).', |
| 170 | + $provider->getName(), |
| 171 | + implode(', ', $locales), |
| 172 | + implode(', ', $domains) |
| 173 | + )); |
| 174 | + |
| 175 | + return 0; |
| 176 | + } |
| 177 | +} |
0 commit comments