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