/
TranslatePackageCommand.php
282 lines (264 loc) · 12.7 KB
/
TranslatePackageCommand.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
<?php
namespace Concrete\Core\Console\Command;
use Concrete\Core\Console\Command;
use Concrete\Core\Localization\Localization;
use Concrete\Core\Localization\Translation\Remote\ProviderInterface as RemoteTranslationProvider;
use Concrete\Core\Support\Facade\Application;
use Exception;
use Gettext\Translations;
use Package;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
class TranslatePackageCommand extends Command
{
protected function configure()
{
$okExitCode = static::SUCCESS;
$errExitCode = static::FAILURE;
$this
->setName('c5:package:translate')
->setAliases([
'c5:package-translate',
'c5:translate-package',
])
->addArgument('package', InputArgument::REQUIRED, 'The handle of the package to be translated (or the path to a directory containing a Concrete package)')
->addEnvOption()
->setCanRunAsRoot(false)
->addOption('locale', 'l', InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'List of locale codes to handle')
->addOption('contact', 'c', InputOption::VALUE_REQUIRED, 'Contact information to be put in the language files to report bugs to (eg the "Report-Msgid-Bugs-To" gettext header)', '')
->addOption('translator', 't', InputOption::VALUE_REQUIRED, 'Translator to be put in the language files (eg the "Last-Translator" gettext header)', '')
->addOption('exclude-3rdparty', 'x', InputOption::VALUE_NONE, 'Specify this option to avoid parsing 3rd party folders')
->addOption('fill', 'f', InputOption::VALUE_NONE, 'Fill-in already known translations using an online service')
->setDescription('Creates or updates translations of a Concrete package')
->setHelp(<<<EOT
If the locale option(s) is not specified, we'll generate/update translations for the currently defined locales for the package.
If currently no locale is defined, we'll generate/update translations for all the currently installed locales of the core of Concrete.
In order to don't generate the locale files but only the master translations file (.pot), specify "--locale=-" (or "-l-")
Examples:
concrete c5:package-translate package_handle
concrete c5:package-translate package_handle --locale=it_IT --locale=de_DE --fill
concrete c5:package-translate package_handle -l it_IT -l de_DE
concrete c5:package-translate package_handle --locale=-
concrete c5:package-translate path/to/a/package/directory -l-
Please remark that this command can also parse legacy (pre-5.7) packages.
Returns codes:
$okExitCode operation completed successfully
$errExitCode errors occurred
More info at http://documentation.concrete5.org/developers/appendix/cli-commands#c5-package-translate
EOT
)
;
}
/**
* @var \Concrete\Core\Application\Application
*/
protected $app;
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->app = Application::getFacadeApplication();
$vsh = $this->app->make('helper/validation/strings');
/* @var \Concrete\Core\Utility\Service\Validation\Strings $vsh */
$fh = $this->app->make('helper/file');
/* @var \Concrete\Core\File\Service\File $fh */
// Let's determine the package handle, directory and version
$packageHandle = null;
$packageDirectory = null;
$packageVersion = null;
$p = $input->getArgument('package');
if (is_dir($p) || !$vsh->handle($p)) {
$packageDirectory = @realpath($p);
if ($packageDirectory === false) {
throw new Exception("Unable to find the directory '$p'");
}
$packageDirectory = str_replace(DIRECTORY_SEPARATOR, '/', $packageDirectory);
list($packageHandle, $packageVersion) = $this->guessPackageDetails($packageDirectory);
if ($packageHandle === null) {
$packageHandle = basename($packageDirectory);
}
} else {
foreach (Package::getAvailablePackages(false) as $pkg) {
if (strcasecmp($p, $pkg->getPackageHandle()) === 0) {
$packageHandle = $pkg->getPackageHandle();
$packageDirectory = $pkg->getPackagePath();
$packageVersion = $pkg->getPackageVersion();
break;
}
}
if ($packageHandle === null) {
throw new Exception("Unable to find a package with handle '$p'");
}
}
$packageLanguagesDirectory = $packageDirectory . '/' . DIRNAME_LANGUAGES;
// Determine the locales to translate
$locales = [];
$localeOption = $input->getOption('locale');
if (in_array('-', $localeOption, true)) {
// We don't want any locale
} elseif (empty($localeOption)) {
// List the currently package locales
foreach ($fh->getDirectoryContents($packageLanguagesDirectory) as $item) {
if (is_file("$packageLanguagesDirectory/$item/LC_MESSAGES/messages.mo") || is_file("$packageLanguagesDirectory/$item/LC_MESSAGES/messages.po")) {
$locales[] = $item;
}
}
if (empty($locales)) {
// Let's use the core locales
$locales = Localization::getAvailableInterfaceLanguages();
}
} else {
// Normalize the locales (eg from it-it to it_IT)
foreach ($localeOption as $lo) {
$chunks = [];
foreach (explode('_', str_replace('-', '_', $lo)) as $index => $chunk) {
if ($index === 0) {
// Language (eg zh)
$chunks[] = strtolower($chunk);
} elseif (strlen($chunk) === 4) {
// Script (eg Hans)
$chunks[] = ucfirst(strtolower($chunk));
} else {
// Territory (eg CN)
$chunks[] = strtoupper($chunk);
}
}
$locales[] = implode('_', $chunks);
}
}
// Initialize the master translations file (.pot)
$pot = new Translations();
$pot->setHeader('Project-Id-Version', "$packageHandle $packageVersion");
$pot->setLanguage(Localization::BASE_LOCALE);
$pot->setHeader('Report-Msgid-Bugs-To', $input->getOption('contact'));
$pot->setHeader('Last-Translator', $input->getOption('translator'));
// Parse the package directory
$output->writeln('Parsing package contents');
foreach (\C5TL\Parser::getAllParsers() as $parser) {
if ($parser->canParseDirectory()) {
$output->write('- running parser "' . $parser->getParserName() . '"... ');
$parser->parseDirectory(
$packageDirectory,
"packages/$packageHandle",
$pot,
false,
$input->getOption('exclude-3rdparty')
);
$output->writeln('<info>done.</info>');
}
}
// Save the pot file
$output->write('Saving .pot file... ');
if (!is_dir($packageLanguagesDirectory)) {
@mkdir($packageLanguagesDirectory, 0775, true);
if (!is_dir($packageLanguagesDirectory)) {
throw new Exception("Unable to create the directory $packageLanguagesDirectory");
}
}
$potFilename = "$packageLanguagesDirectory/messages.pot";
if ($pot->toPoFile($potFilename) === false) {
throw new Exception("Unable to save the .pot file to $potFilename");
}
$output->writeln('<info>done.</info>');
$remoteTranslationsProvider = $this->app->make(RemoteTranslationProvider::class);
/* @var RemoteTranslationProvider $remoteTranslationsProvider */
// Creating/updating the locale files
foreach ($locales as $locale) {
$output->writeln("Working on locale $locale");
$poDirectory = "$packageLanguagesDirectory/$locale/LC_MESSAGES";
$po = clone $pot;
$po->setLanguage($locale);
if ($input->getOption('fill')) {
$output->write('- fetching remote translations... ');
$po = $remoteTranslationsProvider->fillTranslations($po);
$output->writeln('<info>done.</info>');
}
$poFile = "$poDirectory/messages.po";
$moFile = "$poDirectory/messages.mo";
if (is_file($poFile)) {
$output->write('- reading current .po file... ');
$oldPo = Translations::fromPoFile($poFile);
$output->writeln('<info>done.</info>');
} elseif (is_file($moFile)) {
$output->write('- decompiling current .mo file... ');
$oldPo = Translations::fromMoFile($poFile);
$output->writeln('<info>done.</info>');
} else {
$oldPo = null;
}
if ($oldPo !== null) {
$output->write('- merging translations... ');
$po->mergeWith($oldPo, 0);
$output->writeln('<info>done.</info>');
}
$output->write('- saving .po file... ');
if (!is_dir($poDirectory)) {
@mkdir($poDirectory, 0775, true);
if (!is_dir($poDirectory)) {
throw new Exception("Unable to create the directory $poDirectory");
}
}
$po->toPoFile($poFile);
$output->writeln('<info>done.</info>');
$output->write('- saving .mo file... ');
$po->toMoFile($moFile);
$output->writeln('<info>done.</info>');
}
return static::SUCCESS;
}
private function guessPackageDetails($packageDirectory)
{
$packageHandle = null;
$packageVersion = null;
$controllerFile = $packageDirectory . '/' . FILENAME_CONTROLLER;
if (!is_file($controllerFile)) {
throw new Exception("The directory '$packageDirectory' does not seems to contain a valid Concrete package");
}
$fh = $this->app->make('helper/file');
/* @var \Concrete\Core\File\Service\File $fh */
$controllerContents = $fh->getContents($controllerFile);
if ($controllerContents) {
$allTokens = @token_get_all($controllerContents);
if ($allTokens) {
$tokens = array_values(
array_filter(
$allTokens,
function ($token) {
$keep = true;
if (is_array($token)) {
switch ($token[0]) {
case T_DOC_COMMENT:
case T_WHITESPACE:
case T_COMMENT:
$keep = false;
break;
}
}
return $keep;
}
)
);
// Look for package version
for ($i = 0; $i < count($tokens) - 2; ++$i) {
if (
$packageHandle === null
&& is_array($tokens[$i + 0]) && $tokens[$i + 0][0] === T_VARIABLE && $tokens[$i + 0][1] === '$pkgHandle'
&& is_string($tokens[$i + 1]) && $tokens[$i + 1] === '='
&& is_array($tokens[$i + 2]) && $tokens[$i + 2][0] === T_CONSTANT_ENCAPSED_STRING
) {
$packageHandle = @eval('return ' . $tokens[$i + 2][1] . ';');
}
if (
$packageVersion === null
&& is_array($tokens[$i + 0]) && $tokens[$i + 0][0] === T_VARIABLE && $tokens[$i + 0][1] === '$pkgVersion'
&& is_string($tokens[$i + 1]) && $tokens[$i + 1] === '='
&& is_array($tokens[$i + 2]) && $tokens[$i + 2][0] === T_CONSTANT_ENCAPSED_STRING
) {
$packageVersion = @eval('return ' . $tokens[$i + 2][1] . ';');
}
}
}
}
return [$packageHandle, $packageVersion];
}
}