PluginProbe ʕ •ᴥ•ʔ
Independent Analytics – WordPress Analytics Plugin / 2.15.5
Independent Analytics – WordPress Analytics Plugin v2.15.5
2.15.5 2.15.4 2.15.3 2.15.2 2.15.1 2.15.0 2.14.10 trunk 1.1 1.10 1.10.1 1.11 1.12 1.13 1.14 1.15 1.16 1.17 1.17.1 1.17.2 1.17.3 1.17.4 1.18 1.18.1 1.19.0 1.19.1 1.2 1.20.0 1.21.0 1.22.0 1.22.1 1.23.0 1.23.1 1.24.0 1.24.1 1.25.0 1.25.1 1.26.0 1.27.0 1.28.0 1.28.1 1.28.2 1.28.3 1.29.0 1.3 1.30.0 1.30.1 1.4 1.5 1.6 1.7 1.8 1.9 2.0.0 2.0.1 2.1.4 2.1.5 2.1.6 2.10.0 2.10.1 2.10.2 2.10.3 2.10.4 2.11.0 2.11.1 2.11.10 2.11.2 2.11.3 2.11.4 2.11.5 2.11.6 2.11.7 2.11.8 2.11.9 2.12.0 2.12.1 2.12.2 2.13.1 2.13.2 2.13.5 2.13.6 2.14.0 2.14.1 2.14.2 2.14.4 2.14.6 2.14.7 2.14.8 2.14.9 2.2.0 2.2.1 2.3.1 2.3.2 2.4.2 2.4.3 2.5.0 2.5.1 2.6.0 2.6.1 2.6.2 2.6.3 2.6.4 2.7.0 2.7.1 2.7.2 2.7.3 2.8.2 2.8.3 2.8.4 2.8.5 2.8.6 2.8.7 2.8.8 2.8.9 2.9.2 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7
independent-analytics / vendor / symfony / translation / Command / TranslationPullCommand.php
independent-analytics / vendor / symfony / translation / Command Last commit date
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