TranslationPullCommand.php
1 month ago
TranslationPushCommand.php
1 month ago
TranslationTrait.php
2 years ago
XliffLintCommand.php
1 month ago
TranslationPushCommand.php
147 lines
| 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 | namespace IAWPSCOPED\Symfony\Component\Translation\Command; |
| 12 | |
| 13 | use IAWPSCOPED\Symfony\Component\Console\Attribute\AsCommand; |
| 14 | use IAWPSCOPED\Symfony\Component\Console\Command\Command; |
| 15 | use IAWPSCOPED\Symfony\Component\Console\Completion\CompletionInput; |
| 16 | use IAWPSCOPED\Symfony\Component\Console\Completion\CompletionSuggestions; |
| 17 | use IAWPSCOPED\Symfony\Component\Console\Exception\InvalidArgumentException; |
| 18 | use IAWPSCOPED\Symfony\Component\Console\Input\InputArgument; |
| 19 | use IAWPSCOPED\Symfony\Component\Console\Input\InputInterface; |
| 20 | use IAWPSCOPED\Symfony\Component\Console\Input\InputOption; |
| 21 | use IAWPSCOPED\Symfony\Component\Console\Output\OutputInterface; |
| 22 | use IAWPSCOPED\Symfony\Component\Console\Style\SymfonyStyle; |
| 23 | use IAWPSCOPED\Symfony\Component\Translation\Provider\FilteringProvider; |
| 24 | use IAWPSCOPED\Symfony\Component\Translation\Provider\TranslationProviderCollection; |
| 25 | use IAWPSCOPED\Symfony\Component\Translation\Reader\TranslationReaderInterface; |
| 26 | use IAWPSCOPED\Symfony\Component\Translation\TranslatorBag; |
| 27 | /** |
| 28 | * @author Mathieu Santostefano <msantostefano@protonmail.com> |
| 29 | * @internal |
| 30 | */ |
| 31 | #[AsCommand(name: 'translation:push', description: 'Push translations to a given provider.')] |
| 32 | final class TranslationPushCommand extends Command |
| 33 | { |
| 34 | use TranslationTrait; |
| 35 | private $providers; |
| 36 | private $reader; |
| 37 | private array $transPaths; |
| 38 | private array $enabledLocales; |
| 39 | public function __construct(TranslationProviderCollection $providers, TranslationReaderInterface $reader, array $transPaths = [], array $enabledLocales = []) |
| 40 | { |
| 41 | $this->providers = $providers; |
| 42 | $this->reader = $reader; |
| 43 | $this->transPaths = $transPaths; |
| 44 | $this->enabledLocales = $enabledLocales; |
| 45 | parent::__construct(); |
| 46 | } |
| 47 | public function complete(CompletionInput $input, CompletionSuggestions $suggestions) : void |
| 48 | { |
| 49 | if ($input->mustSuggestArgumentValuesFor('provider')) { |
| 50 | $suggestions->suggestValues($this->providers->keys()); |
| 51 | return; |
| 52 | } |
| 53 | if ($input->mustSuggestOptionValuesFor('domains')) { |
| 54 | $provider = $this->providers->get($input->getArgument('provider')); |
| 55 | if ($provider && \method_exists($provider, 'getDomains')) { |
| 56 | $domains = $provider->getDomains(); |
| 57 | $suggestions->suggestValues($domains); |
| 58 | } |
| 59 | return; |
| 60 | } |
| 61 | if ($input->mustSuggestOptionValuesFor('locales')) { |
| 62 | $suggestions->suggestValues($this->enabledLocales); |
| 63 | } |
| 64 | } |
| 65 | /** |
| 66 | * {@inheritdoc} |
| 67 | */ |
| 68 | protected function configure() |
| 69 | { |
| 70 | $keys = $this->providers->keys(); |
| 71 | $defaultProvider = 1 === \count($keys) ? $keys[0] : null; |
| 72 | $this->setDefinition([new InputArgument('provider', null !== $defaultProvider ? InputArgument::OPTIONAL : InputArgument::REQUIRED, 'The provider to push translations to.', $defaultProvider), new InputOption('force', null, InputOption::VALUE_NONE, 'Override existing translations with local ones (it will delete not synchronized messages).'), new InputOption('delete-missing', null, InputOption::VALUE_NONE, 'Delete translations available on provider but not locally.'), new InputOption('domains', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'Specify the domains to push.'), new InputOption('locales', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'Specify the locales to push.', $this->enabledLocales)])->setHelp(<<<'EOF' |
| 73 | The <info>%command.name%</> command pushes translations to the given provider. Only new |
| 74 | translations are pushed, existing ones are not overwritten. |
| 75 | |
| 76 | You can overwrite existing translations by using the <comment>--force</> flag: |
| 77 | |
| 78 | <info>php %command.full_name% --force provider</> |
| 79 | |
| 80 | You can delete provider translations which are not present locally by using the <comment>--delete-missing</> flag: |
| 81 | |
| 82 | <info>php %command.full_name% --delete-missing provider</> |
| 83 | |
| 84 | Full example: |
| 85 | |
| 86 | <info>php %command.full_name% provider --force --delete-missing --domains=messages --domains=validators --locales=en</> |
| 87 | |
| 88 | This command pushes all translations associated with the <comment>messages</> and <comment>validators</> domains for the <comment>en</> locale. |
| 89 | Provider translations for the specified domains and locale are deleted if they're not present locally and overwritten if it's the case. |
| 90 | Provider translations for others domains and locales are ignored. |
| 91 | EOF |
| 92 | ); |
| 93 | } |
| 94 | /** |
| 95 | * {@inheritdoc} |
| 96 | */ |
| 97 | protected function execute(InputInterface $input, OutputInterface $output) : int |
| 98 | { |
| 99 | $provider = $this->providers->get($input->getArgument('provider')); |
| 100 | if (!$this->enabledLocales) { |
| 101 | throw new InvalidArgumentException(\sprintf('You must define "framework.translator.enabled_locales" or "framework.translator.providers.%s.locales" config key in order to work with translation providers.', \parse_url($provider, \PHP_URL_SCHEME))); |
| 102 | } |
| 103 | $io = new SymfonyStyle($input, $output); |
| 104 | $domains = $input->getOption('domains'); |
| 105 | $locales = $input->getOption('locales'); |
| 106 | $force = $input->getOption('force'); |
| 107 | $deleteMissing = $input->getOption('delete-missing'); |
| 108 | if (!$domains && $provider instanceof FilteringProvider) { |
| 109 | $domains = $provider->getDomains(); |
| 110 | } |
| 111 | // Reading local translations must be done after retrieving the domains from the provider |
| 112 | // in order to manage only translations from configured domains |
| 113 | $localTranslations = $this->readLocalTranslations($locales, $domains, $this->transPaths); |
| 114 | if (!$domains) { |
| 115 | $domains = $this->getDomainsFromTranslatorBag($localTranslations); |
| 116 | } |
| 117 | if (!$deleteMissing && $force) { |
| 118 | $provider->write($localTranslations); |
| 119 | $io->success(\sprintf('All local translations has been sent to "%s" (for "%s" locale(s), and "%s" domain(s)).', \parse_url($provider, \PHP_URL_SCHEME), \implode(', ', $locales), \implode(', ', $domains))); |
| 120 | return 0; |
| 121 | } |
| 122 | $providerTranslations = $provider->read($domains, $locales); |
| 123 | if ($deleteMissing) { |
| 124 | $provider->delete($providerTranslations->diff($localTranslations)); |
| 125 | $io->success(\sprintf('Missing translations on "%s" has been deleted (for "%s" locale(s), and "%s" domain(s)).', \parse_url($provider, \PHP_URL_SCHEME), \implode(', ', $locales), \implode(', ', $domains))); |
| 126 | // Read provider translations again, after missing translations deletion, |
| 127 | // to avoid push freshly deleted translations. |
| 128 | $providerTranslations = $provider->read($domains, $locales); |
| 129 | } |
| 130 | $translationsToWrite = $localTranslations->diff($providerTranslations); |
| 131 | if ($force) { |
| 132 | $translationsToWrite->addBag($localTranslations->intersect($providerTranslations)); |
| 133 | } |
| 134 | $provider->write($translationsToWrite); |
| 135 | $io->success(\sprintf('%s local translations has been sent to "%s" (for "%s" locale(s), and "%s" domain(s)).', $force ? 'All' : 'New', \parse_url($provider, \PHP_URL_SCHEME), \implode(', ', $locales), \implode(', ', $domains))); |
| 136 | return 0; |
| 137 | } |
| 138 | private function getDomainsFromTranslatorBag(TranslatorBag $translatorBag) : array |
| 139 | { |
| 140 | $domains = []; |
| 141 | foreach ($translatorBag->getCatalogues() as $catalogue) { |
| 142 | $domains += $catalogue->getDomains(); |
| 143 | } |
| 144 | return \array_unique($domains); |
| 145 | } |
| 146 | } |
| 147 |