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