| 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\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 |
* @author Mathieu Santostefano <msantostefano@protonmail.com>
|
|
|
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 |
private TranslationProviderCollection $providerCollection;
|
|
|
38 |
private TranslationWriterInterface $writer;
|
|
|
39 |
private TranslationReaderInterface $reader;
|
|
|
40 |
private string $defaultLocale;
|
|
|
41 |
private array $transPaths;
|
|
|
42 |
private array $enabledLocales;
|
|
|
43 |
|
|
|
44 |
public function __construct(TranslationProviderCollection $providerCollection, TranslationWriterInterface $writer, TranslationReaderInterface $reader, string $defaultLocale, array $transPaths = [], array $enabledLocales = [])
|
|
|
45 |
{
|
|
|
46 |
$this->providerCollection = $providerCollection;
|
|
|
47 |
$this->writer = $writer;
|
|
|
48 |
$this->reader = $reader;
|
|
|
49 |
$this->defaultLocale = $defaultLocale;
|
|
|
50 |
$this->transPaths = $transPaths;
|
|
|
51 |
$this->enabledLocales = $enabledLocales;
|
|
|
52 |
|
|
|
53 |
parent::__construct();
|
|
|
54 |
}
|
|
|
55 |
|
|
|
56 |
public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void
|
|
|
57 |
{
|
|
|
58 |
if ($input->mustSuggestArgumentValuesFor('provider')) {
|
|
|
59 |
$suggestions->suggestValues($this->providerCollection->keys());
|
|
|
60 |
|
|
|
61 |
return;
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
if ($input->mustSuggestOptionValuesFor('domains')) {
|
|
|
65 |
$provider = $this->providerCollection->get($input->getArgument('provider'));
|
|
|
66 |
|
|
|
67 |
if (method_exists($provider, 'getDomains')) {
|
|
|
68 |
$suggestions->suggestValues($provider->getDomains());
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
return;
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
if ($input->mustSuggestOptionValuesFor('locales')) {
|
|
|
75 |
$suggestions->suggestValues($this->enabledLocales);
|
|
|
76 |
|
|
|
77 |
return;
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
if ($input->mustSuggestOptionValuesFor('format')) {
|
|
|
81 |
$suggestions->suggestValues(['php', 'xlf', 'xlf12', 'xlf20', 'po', 'mo', 'yml', 'yaml', 'ts', 'csv', 'json', 'ini', 'res']);
|
|
|
82 |
}
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
protected function configure()
|
|
|
86 |
{
|
|
|
87 |
$keys = $this->providerCollection->keys();
|
|
|
88 |
$defaultProvider = 1 === \count($keys) ? $keys[0] : null;
|
|
|
89 |
|
|
|
90 |
$this
|
|
|
91 |
->setDefinition([
|
|
|
92 |
new InputArgument('provider', null !== $defaultProvider ? InputArgument::OPTIONAL : InputArgument::REQUIRED, 'The provider to pull translations from.', $defaultProvider),
|
|
|
93 |
new InputOption('force', null, InputOption::VALUE_NONE, 'Override existing translations with provider ones (it will delete not synchronized messages).'),
|
|
|
94 |
new InputOption('intl-icu', null, InputOption::VALUE_NONE, 'Associated to --force option, it will write messages in "%domain%+intl-icu.%locale%.xlf" files.'),
|
|
|
95 |
new InputOption('domains', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'Specify the domains to pull.'),
|
|
|
96 |
new InputOption('locales', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'Specify the locales to pull.'),
|
|
|
97 |
new InputOption('format', null, InputOption::VALUE_OPTIONAL, 'Override the default output format.', 'xlf12'),
|
|
|
98 |
])
|
|
|
99 |
->setHelp(<<<'EOF'
|
|
|
100 |
The <info>%command.name%</> command pulls translations from the given provider. Only
|
|
|
101 |
new translations are pulled, existing ones are not overwritten.
|
|
|
102 |
|
|
|
103 |
You can overwrite existing translations (and remove the missing ones on local side) by using the <comment>--force</> flag:
|
|
|
104 |
|
|
|
105 |
<info>php %command.full_name% --force provider</>
|
|
|
106 |
|
|
|
107 |
Full example:
|
|
|
108 |
|
|
|
109 |
<info>php %command.full_name% provider --force --domains=messages --domains=validators --locales=en</>
|
|
|
110 |
|
|
|
111 |
This command pulls all translations associated with the <comment>messages</> and <comment>validators</> domains for the <comment>en</> locale.
|
|
|
112 |
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.
|
|
|
113 |
Local translations for others domains and locales are ignored.
|
|
|
114 |
EOF
|
|
|
115 |
)
|
|
|
116 |
;
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
protected function execute(InputInterface $input, OutputInterface $output): int
|
|
|
120 |
{
|
|
|
121 |
$io = new SymfonyStyle($input, $output);
|
|
|
122 |
|
|
|
123 |
$provider = $this->providerCollection->get($input->getArgument('provider'));
|
|
|
124 |
$force = $input->getOption('force');
|
|
|
125 |
$intlIcu = $input->getOption('intl-icu');
|
|
|
126 |
$locales = $input->getOption('locales') ?: $this->enabledLocales;
|
|
|
127 |
$domains = $input->getOption('domains');
|
|
|
128 |
$format = $input->getOption('format');
|
|
|
129 |
$xliffVersion = '1.2';
|
|
|
130 |
|
|
|
131 |
if ($intlIcu && !$force) {
|
|
|
132 |
$io->note('--intl-icu option only has an effect when used with --force. Here, it will be ignored.');
|
|
|
133 |
}
|
|
|
134 |
|
|
|
135 |
switch ($format) {
|
|
|
136 |
case 'xlf20': $xliffVersion = '2.0';
|
|
|
137 |
// no break
|
|
|
138 |
case 'xlf12': $format = 'xlf';
|
|
|
139 |
}
|
|
|
140 |
|
|
|
141 |
$writeOptions = [
|
|
|
142 |
'path' => end($this->transPaths),
|
|
|
143 |
'xliff_version' => $xliffVersion,
|
|
|
144 |
'default_locale' => $this->defaultLocale,
|
|
|
145 |
];
|
|
|
146 |
|
|
|
147 |
if (!$domains) {
|
|
|
148 |
$domains = $provider->getDomains();
|
|
|
149 |
}
|
|
|
150 |
|
|
|
151 |
$providerTranslations = $provider->read($domains, $locales);
|
|
|
152 |
|
|
|
153 |
if ($force) {
|
|
|
154 |
foreach ($providerTranslations->getCatalogues() as $catalogue) {
|
|
|
155 |
$operation = new TargetOperation(new MessageCatalogue($catalogue->getLocale()), $catalogue);
|
|
|
156 |
if ($intlIcu) {
|
|
|
157 |
$operation->moveMessagesToIntlDomainsIfPossible();
|
|
|
158 |
}
|
|
|
159 |
$this->writer->write($operation->getResult(), $format, $writeOptions);
|
|
|
160 |
}
|
|
|
161 |
|
|
|
162 |
$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)));
|
|
|
163 |
|
|
|
164 |
return 0;
|
|
|
165 |
}
|
|
|
166 |
|
|
|
167 |
$localTranslations = $this->readLocalTranslations($locales, $domains, $this->transPaths);
|
|
|
168 |
|
|
|
169 |
// Append pulled translations to local ones.
|
|
|
170 |
$localTranslations->addBag($providerTranslations->diff($localTranslations));
|
|
|
171 |
|
|
|
172 |
foreach ($localTranslations->getCatalogues() as $catalogue) {
|
|
|
173 |
$this->writer->write($catalogue, $format, $writeOptions);
|
|
|
174 |
}
|
|
|
175 |
|
|
|
176 |
$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)));
|
|
|
177 |
|
|
|
178 |
return 0;
|
|
|
179 |
}
|
|
|
180 |
}
|