| 148 |
lars |
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 |
|
|
|
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 |
* @author Mathieu Santostefano <msantostefano@protonmail.com>
|
|
|
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 |
private TranslationProviderCollection $providers;
|
|
|
38 |
private TranslationReaderInterface $reader;
|
|
|
39 |
private array $transPaths;
|
|
|
40 |
private array $enabledLocales;
|
|
|
41 |
|
|
|
42 |
public function __construct(TranslationProviderCollection $providers, TranslationReaderInterface $reader, array $transPaths = [], array $enabledLocales = [])
|
|
|
43 |
{
|
|
|
44 |
$this->providers = $providers;
|
|
|
45 |
$this->reader = $reader;
|
|
|
46 |
$this->transPaths = $transPaths;
|
|
|
47 |
$this->enabledLocales = $enabledLocales;
|
|
|
48 |
|
|
|
49 |
parent::__construct();
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void
|
|
|
53 |
{
|
|
|
54 |
if ($input->mustSuggestArgumentValuesFor('provider')) {
|
|
|
55 |
$suggestions->suggestValues($this->providers->keys());
|
|
|
56 |
|
|
|
57 |
return;
|
|
|
58 |
}
|
|
|
59 |
|
|
|
60 |
if ($input->mustSuggestOptionValuesFor('domains')) {
|
|
|
61 |
$provider = $this->providers->get($input->getArgument('provider'));
|
|
|
62 |
|
|
|
63 |
if ($provider && method_exists($provider, 'getDomains')) {
|
|
|
64 |
$domains = $provider->getDomains();
|
|
|
65 |
$suggestions->suggestValues($domains);
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
return;
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
if ($input->mustSuggestOptionValuesFor('locales')) {
|
|
|
72 |
$suggestions->suggestValues($this->enabledLocales);
|
|
|
73 |
}
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
protected function configure()
|
|
|
77 |
{
|
|
|
78 |
$keys = $this->providers->keys();
|
|
|
79 |
$defaultProvider = 1 === \count($keys) ? $keys[0] : null;
|
|
|
80 |
|
|
|
81 |
$this
|
|
|
82 |
->setDefinition([
|
|
|
83 |
new InputArgument('provider', null !== $defaultProvider ? InputArgument::OPTIONAL : InputArgument::REQUIRED, 'The provider to push translations to.', $defaultProvider),
|
|
|
84 |
new InputOption('force', null, InputOption::VALUE_NONE, 'Override existing translations with local ones (it will delete not synchronized messages).'),
|
|
|
85 |
new InputOption('delete-missing', null, InputOption::VALUE_NONE, 'Delete translations available on provider but not locally.'),
|
|
|
86 |
new InputOption('domains', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'Specify the domains to push.'),
|
|
|
87 |
new InputOption('locales', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'Specify the locales to push.', $this->enabledLocales),
|
|
|
88 |
])
|
|
|
89 |
->setHelp(<<<'EOF'
|
|
|
90 |
The <info>%command.name%</> command pushes translations to the given provider. Only new
|
|
|
91 |
translations are pushed, existing ones are not overwritten.
|
|
|
92 |
|
|
|
93 |
You can overwrite existing translations by using the <comment>--force</> flag:
|
|
|
94 |
|
|
|
95 |
<info>php %command.full_name% --force provider</>
|
|
|
96 |
|
|
|
97 |
You can delete provider translations which are not present locally by using the <comment>--delete-missing</> flag:
|
|
|
98 |
|
|
|
99 |
<info>php %command.full_name% --delete-missing provider</>
|
|
|
100 |
|
|
|
101 |
Full example:
|
|
|
102 |
|
|
|
103 |
<info>php %command.full_name% provider --force --delete-missing --domains=messages --domains=validators --locales=en</>
|
|
|
104 |
|
|
|
105 |
This command pushes all translations associated with the <comment>messages</> and <comment>validators</> domains for the <comment>en</> locale.
|
|
|
106 |
Provider translations for the specified domains and locale are deleted if they're not present locally and overwritten if it's the case.
|
|
|
107 |
Provider translations for others domains and locales are ignored.
|
|
|
108 |
EOF
|
|
|
109 |
)
|
|
|
110 |
;
|
|
|
111 |
}
|
|
|
112 |
|
|
|
113 |
protected function execute(InputInterface $input, OutputInterface $output): int
|
|
|
114 |
{
|
|
|
115 |
$provider = $this->providers->get($input->getArgument('provider'));
|
|
|
116 |
|
|
|
117 |
if (!$this->enabledLocales) {
|
| 688 |
lars |
118 |
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)));
|
| 148 |
lars |
119 |
}
|
|
|
120 |
|
|
|
121 |
$io = new SymfonyStyle($input, $output);
|
|
|
122 |
$domains = $input->getOption('domains');
|
|
|
123 |
$locales = $input->getOption('locales');
|
|
|
124 |
$force = $input->getOption('force');
|
|
|
125 |
$deleteMissing = $input->getOption('delete-missing');
|
|
|
126 |
|
|
|
127 |
if (!$domains && $provider instanceof FilteringProvider) {
|
|
|
128 |
$domains = $provider->getDomains();
|
|
|
129 |
}
|
|
|
130 |
|
|
|
131 |
// Reading local translations must be done after retrieving the domains from the provider
|
|
|
132 |
// in order to manage only translations from configured domains
|
|
|
133 |
$localTranslations = $this->readLocalTranslations($locales, $domains, $this->transPaths);
|
|
|
134 |
|
|
|
135 |
if (!$domains) {
|
|
|
136 |
$domains = $this->getDomainsFromTranslatorBag($localTranslations);
|
|
|
137 |
}
|
|
|
138 |
|
|
|
139 |
if (!$deleteMissing && $force) {
|
|
|
140 |
$provider->write($localTranslations);
|
|
|
141 |
|
|
|
142 |
$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)));
|
|
|
143 |
|
|
|
144 |
return 0;
|
|
|
145 |
}
|
|
|
146 |
|
|
|
147 |
$providerTranslations = $provider->read($domains, $locales);
|
|
|
148 |
|
|
|
149 |
if ($deleteMissing) {
|
|
|
150 |
$provider->delete($providerTranslations->diff($localTranslations));
|
|
|
151 |
|
|
|
152 |
$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)));
|
|
|
153 |
|
|
|
154 |
// Read provider translations again, after missing translations deletion,
|
|
|
155 |
// to avoid push freshly deleted translations.
|
|
|
156 |
$providerTranslations = $provider->read($domains, $locales);
|
|
|
157 |
}
|
|
|
158 |
|
|
|
159 |
$translationsToWrite = $localTranslations->diff($providerTranslations);
|
|
|
160 |
|
|
|
161 |
if ($force) {
|
|
|
162 |
$translationsToWrite->addBag($localTranslations->intersect($providerTranslations));
|
|
|
163 |
}
|
|
|
164 |
|
|
|
165 |
$provider->write($translationsToWrite);
|
|
|
166 |
|
|
|
167 |
$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)));
|
|
|
168 |
|
|
|
169 |
return 0;
|
|
|
170 |
}
|
|
|
171 |
|
|
|
172 |
private function getDomainsFromTranslatorBag(TranslatorBag $translatorBag): array
|
|
|
173 |
{
|
|
|
174 |
$domains = [];
|
|
|
175 |
|
|
|
176 |
foreach ($translatorBag->getCatalogues() as $catalogue) {
|
|
|
177 |
$domains += $catalogue->getDomains();
|
|
|
178 |
}
|
|
|
179 |
|
|
|
180 |
return array_unique($domains);
|
|
|
181 |
}
|
|
|
182 |
}
|