-
Notifications
You must be signed in to change notification settings - Fork 291
/
Copy pathExtractTranslationCommand.php
223 lines (189 loc) · 10.5 KB
/
ExtractTranslationCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<?php
/*
* Copyright 2011 Johannes M. Schmitt <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace JMS\TranslationBundle\Command;
use JMS\TranslationBundle\Translation\ConfigBuilder;
use JMS\TranslationBundle\Exception\RuntimeException;
use Symfony\Component\Console\Input\InputArgument;
use JMS\TranslationBundle\Translation\Config;
use JMS\TranslationBundle\Logger\OutputLogger;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
/**
* Command for extracting translations.
*
* @author Johannes M. Schmitt <[email protected]>
*/
class ExtractTranslationCommand extends ContainerAwareCommand
{
/**
* {@inheritdoc}
*/
protected function configure()
{
$this
->setName('translation:extract')
->setDescription('Extracts translation messages from your code.')
->addArgument('locales', InputArgument::IS_ARRAY, 'The locales for which to extract messages.')
->addOption('enable-extractor', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'The alias of an extractor which should be enabled.')
->addOption('disable-extractor', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'The alias of an extractor which should be disabled (only required for overriding config values).')
->addOption('config', 'c', InputOption::VALUE_REQUIRED, 'The config to use')
->addOption('bundle', 'b', InputOption::VALUE_REQUIRED, 'The bundle that you want to extract translations for.')
->addOption('exclude-name', null, InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, 'A pattern which should be ignored, e.g. *Test.php')
->addOption('exclude-dir', null, InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, 'A directory name which should be ignored, e.g. Tests')
->addOption('ignore-domain', 'i', InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, 'A domain to ignore.')
->addOption('domain', null, InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, 'Use only this domain.')
->addOption('dir', 'd', InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, 'A directory to scan for messages.')
->addOption('output-dir', null, InputOption::VALUE_REQUIRED, 'The directory where files should be written to.')
->addOption('dry-run', null, InputOption::VALUE_NONE, 'When specified, changes are _NOT_ persisted to disk.')
->addOption('output-format', null, InputOption::VALUE_REQUIRED, 'The output format that should be used (in most cases, it is better to change only the default-output-format).')
->addOption('default-output-format', null, InputOption::VALUE_REQUIRED, 'The default output format (defaults to xlf).')
->addOption('keep', null, InputOption::VALUE_NONE, 'Define if the updater service should keep the old translation (defaults to false).')
->addOption('external-translations-dir', null, InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, 'Load external translation resources')
->addOption('keeptm', null, InputOption::VALUE_NONE, 'Define if the updater service should keep the old translation messages (defaults to false).')
;
}
/**
* @param InputInterface $input
* @param OutputInterface $output
* @return void
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$builder = $input->getOption('config') ?
$this->getContainer()->get('jms_translation.config_factory')->getBuilder($input->getOption('config'))
: new ConfigBuilder();
$this->updateWithInput($input, $builder);
$locales = $input->getArgument('locales');
if (empty($locales)) {
$locales = $this->getContainer()->getParameter('jms_translation.locales');
}
if (empty($locales)) {
throw new \LogicException('No locales were given, and no locales are configured.');
}
foreach ($locales as $locale) {
$config = $builder->setLocale($locale)->getConfig();
$output->writeln(sprintf('Extracting Translations for locale <info>%s</info>', $locale));
$output->writeln(sprintf('Keep old translations: <info>%s</info>', $config->isKeepOldMessages() ? 'Yes' : 'No'));
$output->writeln(sprintf('Keep old translation messages: <info>%s</info>', $config->isKeepOldTranslationMessages() ? 'Yes' : 'No'));
$output->writeln(sprintf('Output-Path: <info>%s</info>', $config->getTranslationsDir()));
$output->writeln(sprintf('Directories: <info>%s</info>', implode(', ', $config->getScanDirs())));
$output->writeln(sprintf('Excluded Directories: <info>%s</info>', $config->getExcludedDirs() ? implode(', ', $config->getExcludedDirs()) : '# none #'));
$output->writeln(sprintf('Excluded Names: <info>%s</info>', $config->getExcludedNames() ? implode(', ', $config->getExcludedNames()) : '# none #'));
$output->writeln(sprintf('Output-Format: <info>%s</info>', $config->getOutputFormat() ? $config->getOutputFormat() : '# whatever is present, if nothing then '.$config->getDefaultOutputFormat().' #'));
$output->writeln(sprintf('Custom Extractors: <info>%s</info>', $config->getEnabledExtractors() ? implode(', ', array_keys($config->getEnabledExtractors())) : '# none #'));
$output->writeln('============================================================');
$updater = $this->getContainer()->get('jms_translation.updater');
$updater->setLogger($logger = new OutputLogger($output));
if (!$input->getOption('verbose')) {
$logger->setLevel(OutputLogger::ALL ^ OutputLogger::DEBUG);
}
if ($input->getOption('dry-run')) {
$changeSet = $updater->getChangeSet($config);
$output->writeln('Added Messages: '.count($changeSet->getAddedMessages()));
if ($input->hasParameterOption('--verbose')) {
foreach ($changeSet->getAddedMessages() as $message) {
$output->writeln($message->getId(). '-> '.$message->getDesc());
}
}
if ($config->isKeepOldMessages()) {
$output->writeln('Deleted Messages: # none as "Keep Old Translations" is true #');
} else {
$output->writeln('Deleted Messages: '.count($changeSet->getDeletedMessages()));
if ($input->hasParameterOption('--verbose')) {
foreach ($changeSet->getDeletedMessages() as $message) {
$output->writeln($message->getId(). '-> '.$message->getDesc());
}
}
}
if ($config->isKeepOldTranslationMessages()) {
$output->writeln('Not keeping old Translation Messages');
}
return;
}
$updater->process($config);
}
$output->writeln('done!');
}
/**
* @param InputInterface $input
* @param ConfigBuilder $builder
*/
private function updateWithInput(InputInterface $input, ConfigBuilder $builder)
{
if ($bundle = $input->getOption('bundle')) {
if ('@' === $bundle[0]) {
$bundle = substr($bundle, 1);
}
$bundle = $this->getApplication()->getKernel()->getBundle($bundle);
$builder->setTranslationsDir($bundle->getPath().'/Resources/translations');
$builder->setScanDirs(array($bundle->getPath()));
}
if ($dirs = $input->getOption('dir')) {
$builder->setScanDirs($dirs);
}
if ($outputDir = $input->getOption('output-dir')) {
$builder->setTranslationsDir($outputDir);
}
if ($outputFormat = $input->getOption('output-format')) {
$builder->setOutputFormat($outputFormat);
}
if ($input->getOption('ignore-domain')) {
foreach ($input->getOption('ignore-domain') as $domain) {
$builder->addIgnoredDomain($domain);
}
}
if ($input->getOption('domain')) {
foreach ($input->getOption('domain') as $domain) {
$builder->addDomain($domain);
}
}
if ($excludeDirs = $input->getOption('exclude-dir')) {
$builder->setExcludedDirs($excludeDirs);
}
if ($excludeNames = $input->getOption('exclude-name')) {
$builder->setExcludedNames($excludeNames);
}
if ($format = $input->getOption('default-output-format')) {
$builder->setDefaultOutputFormat($format);
}
if ($enabledExtractors = $input->getOption('enable-extractor')) {
foreach ($enabledExtractors as $alias) {
$builder->enableExtractor($alias);
}
}
if ($disabledExtractors = $input->getOption('disable-extractor')) {
foreach ($disabledExtractors as $alias) {
$builder->disableExtractor($alias);
}
}
if ($input->hasParameterOption('--keep') || $input->hasParameterOption('--keep=true')) {
$builder->setKeepOldTranslations(true);
} elseif ($input->hasParameterOption('--keep=false')) {
$builder->setKeepOldTranslations(false);
}
if ($input->hasParameterOption('--keeptm') || $input->hasParameterOption('--keeptm=true')) {
$builder->setKeepOldTranslationMessages(true);
} else if ($input->hasParameterOption('--keeptm=false')) {
$builder->setKeepOldTranslationMessages(false);
}
if ($loadResource = $input->getOption('external-translations-dir')) {
$builder->setLoadResources($loadResource);
}
}
}