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