| 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\Input\InputArgument; |
| 19 |
use Symfony\Component\Console\Input\InputInterface; |
| 20 |
use Symfony\Component\Console\Input\InputOption; |
| 21 |
use Symfony\Component\Console\Output\OutputInterface; |
| 22 |
use Symfony\Component\Console\Style\SymfonyStyle; |
| 23 |
use Symfony\Component\Translation\Catalogue\TargetOperation; |
| 24 |
use Symfony\Component\Translation\MessageCatalogue; |
| 25 |
use Symfony\Component\Translation\Provider\TranslationProviderCollection; |
| 26 |
use Symfony\Component\Translation\Reader\TranslationReaderInterface; |
| 27 |
use Symfony\Component\Translation\Writer\TranslationWriterInterface; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
#[AsCommand(name: 'translation:pull', description: 'Pull translations from a given provider.')] |
| 33 |
final class TranslationPullCommand extends Command |
| 34 |
{ |
| 35 |
use TranslationTrait; |
| 36 |
|
| 37 |
public function __construct( |
| 38 |
private TranslationProviderCollection $providerCollection, |
| 39 |
private TranslationWriterInterface $writer, |
| 40 |
private TranslationReaderInterface $reader, |
| 41 |
private string $defaultLocale, |
| 42 |
private array $transPaths = [], |
| 43 |
private array $enabledLocales = [], |
| 44 |
) { |
| 45 |
parent::__construct(); |
| 46 |
} |
| 47 |
|
| 48 |
public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void |
| 49 |
{ |
| 50 |
if ($input->mustSuggestArgumentValuesFor('provider')) { |
| 51 |
$suggestions->suggestValues($this->providerCollection->keys()); |
| 52 |
|
| 53 |
return; |
| 54 |
} |
| 55 |
|
| 56 |
if ($input->mustSuggestOptionValuesFor('domains')) { |
| 57 |
$provider = $this->providerCollection->get($input->getArgument('provider')); |
| 58 |
|
| 59 |
if (method_exists($provider, 'getDomains')) { |
| 60 |
$suggestions->suggestValues($provider->getDomains()); |
| 61 |
} |
| 62 |
|
| 63 |
return; |
| 64 |
} |
| 65 |
|
| 66 |
if ($input->mustSuggestOptionValuesFor('locales')) { |
| 67 |
$suggestions->suggestValues($this->enabledLocales); |
| 68 |
|
| 69 |
return; |
| 70 |
} |
| 71 |
|
| 72 |
if ($input->mustSuggestOptionValuesFor('format')) { |
| 73 |
$suggestions->suggestValues(['php', 'xlf', 'xlf12', 'xlf20', 'po', 'mo', 'yml', 'yaml', 'ts', 'csv', 'json', 'ini', 'res']); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
protected function configure(): void |
| 78 |
{ |
| 79 |
$keys = $this->providerCollection->keys(); |
| 80 |
$defaultProvider = 1 === \count($keys) ? $keys[0] : null; |
| 81 |
|
| 82 |
$this |
| 83 |
->setDefinition([ |
| 84 |
new InputArgument('provider', null !== $defaultProvider ? InputArgument::OPTIONAL : InputArgument::REQUIRED, 'The provider to pull translations from.', $defaultProvider), |
| 85 |
new InputOption('force', null, InputOption::VALUE_NONE, 'Override existing translations with provider ones (it will delete not synchronized messages).'), |
| 86 |
new InputOption('intl-icu', null, InputOption::VALUE_NONE, 'Associated to --force option, it will write messages in "%domain%+intl-icu.%locale%.xlf" files.'), |
| 87 |
new InputOption('domains', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Specify the domains to pull.'), |
| 88 |
new InputOption('locales', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Specify the locales to pull.'), |
| 89 |
new InputOption('format', null, InputOption::VALUE_REQUIRED, 'Override the default output format.', 'xlf12'), |
| 90 |
new InputOption('as-tree', null, InputOption::VALUE_REQUIRED, 'Write messages as a tree-like structure. Needs --format=yaml. The given value defines the level where to switch to inline YAML'), |
| 91 |
]) |
| 92 |
->setHelp(<<<'EOF' |
| 93 |
The <info>%command.name%</> command pulls translations from the given provider. Only |
| 94 |
new translations are pulled, existing ones are not overwritten. |
| 95 |
|
| 96 |
You can overwrite existing translations (and remove the missing ones on local side) by using the <info>--force</> flag: |
| 97 |
|
| 98 |
<info>php %command.full_name% --force provider</> |
| 99 |
|
| 100 |
Full example: |
| 101 |
|
| 102 |
<info>php %command.full_name% provider --force --domains=messages --domains=validators --locales=en</> |
| 103 |
|
| 104 |
This command pulls all translations associated with the <info>messages</> and <info>validators</> domains for the <info>en</> locale. |
| 105 |
Local translations for the specified domains and locale are deleted if they're not present on the provider and overwritten if it's the case. |
| 106 |
Local translations for others domains and locales are ignored. |
| 107 |
|
| 108 |
EOF |
| 109 |
) |
| 110 |
; |
| 111 |
} |
| 112 |
|
| 113 |
protected function execute(InputInterface $input, OutputInterface $output): int |
| 114 |
{ |
| 115 |
$io = new SymfonyStyle($input, $output); |
| 116 |
|
| 117 |
$provider = $this->providerCollection->get($input->getArgument('provider')); |
| 118 |
$force = $input->getOption('force'); |
| 119 |
$intlIcu = $input->getOption('intl-icu'); |
| 120 |
$locales = $input->getOption('locales') ?: $this->enabledLocales; |
| 121 |
$domains = $input->getOption('domains'); |
| 122 |
$format = $input->getOption('format'); |
| 123 |
$asTree = (int) $input->getOption('as-tree'); |
| 124 |
$xliffVersion = '1.2'; |
| 125 |
|
| 126 |
if ($intlIcu && !$force) { |
| 127 |
$io->note('--intl-icu option only has an effect when used with --force. Here, it will be ignored.'); |
| 128 |
} |
| 129 |
|
| 130 |
switch ($format) { |
| 131 |
case 'xlf20': $xliffVersion = '2.0'; |
| 132 |
|
| 133 |
case 'xlf12': $format = 'xlf'; |
| 134 |
} |
| 135 |
|
| 136 |
$writeOptions = [ |
| 137 |
'path' => end($this->transPaths), |
| 138 |
'xliff_version' => $xliffVersion, |
| 139 |
'default_locale' => $this->defaultLocale, |
| 140 |
'as_tree' => (bool) $asTree, |
| 141 |
'inline' => $asTree, |
| 142 |
]; |
| 143 |
|
| 144 |
if (!$domains) { |
| 145 |
$domains = $provider->getDomains(); |
| 146 |
} |
| 147 |
|
| 148 |
$providerTranslations = $provider->read($domains, $locales); |
| 149 |
|
| 150 |
if ($force) { |
| 151 |
foreach ($providerTranslations->getCatalogues() as $catalogue) { |
| 152 |
$operation = new TargetOperation(new MessageCatalogue($catalogue->getLocale()), $catalogue); |
| 153 |
if ($intlIcu) { |
| 154 |
$operation->moveMessagesToIntlDomainsIfPossible(); |
| 155 |
} |
| 156 |
$this->writer->write($operation->getResult(), $format, $writeOptions); |
| 157 |
} |
| 158 |
|
| 159 |
$io->success(\sprintf('Local translations has been updated from "%s" (for "%s" locale(s), and "%s" domain(s)).', parse_url($provider, \PHP_URL_SCHEME), implode(', ', $locales), implode(', ', $domains))); |
| 160 |
|
| 161 |
return 0; |
| 162 |
} |
| 163 |
|
| 164 |
$localTranslations = $this->readLocalTranslations($locales, $domains, $this->transPaths); |
| 165 |
|
| 166 |
|
| 167 |
$localTranslations->addBag($providerTranslations->diff($localTranslations)); |
| 168 |
|
| 169 |
foreach ($localTranslations->getCatalogues() as $catalogue) { |
| 170 |
$this->writer->write($catalogue, $format, $writeOptions); |
| 171 |
} |
| 172 |
|
| 173 |
$io->success(\sprintf('New translations from "%s" has been written locally (for "%s" locale(s), and "%s" domain(s)).', parse_url($provider, \PHP_URL_SCHEME), implode(', ', $locales), implode(', ', $domains))); |
| 174 |
|
| 175 |
return 0; |
| 176 |
} |
| 177 |
} |