Spaces:
No application file
No application file
File size: 6,490 Bytes
d2897cd |
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 |
<?php
declare(strict_types=1);
namespace Mautic\CoreBundle\Command;
use Mautic\CoreBundle\Factory\TransifexFactory;
use Mautic\CoreBundle\Helper\LanguageHelper;
use Mautic\CoreBundle\Helper\PathsHelper;
use Mautic\CoreBundle\Helper\UrlHelper;
use Mautic\Transifex\Connector\Statistics;
use Mautic\Transifex\Connector\Translations;
use Mautic\Transifex\Exception\InvalidConfigurationException;
use Mautic\Transifex\Exception\ResponseException;
use Mautic\Transifex\Promise;
use Psr\Http\Message\ResponseInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
/**
* CLI Command to pull language resources from Transifex.
*/
class PullTransifexCommand extends Command
{
public const NAME = 'mautic:transifex:pull';
public function __construct(
private TransifexFactory $transifexFactory,
private TranslatorInterface $translator,
private PathsHelper $pathsHelper,
private LanguageHelper $languageHelper
) {
parent::__construct();
}
protected function configure(): void
{
$this->setName(self::NAME)
->addOption('language', null, InputOption::VALUE_OPTIONAL, 'Optional language to pull', null)
->addOption('bundle', null, InputOption::VALUE_OPTIONAL, 'Optional bundle to pull. Example value: WebhookBundle', null)
->addOption('path', null, InputOption::VALUE_OPTIONAL, 'Optional path to a directory where to store the traslations.', null)
->setHelp(<<<'EOT'
The <info>%command.name%</info> command is used to retrieve updated Mautic translations from Transifex and writes them to the filesystem.
<info>php %command.full_name%</info>
The command can optionally only pull files for a specific language with the --language option
<info>php %command.full_name% --language=<language_code></info>
EOT
);
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$languageFilter = $input->getOption('language');
$bundleFilter = $input->getOption('bundle');
$path = $input->getOption('path');
$files = $this->languageHelper->getLanguageFiles();
$translationDir = ($path ?? $this->pathsHelper->getTranslationsPath()).'/';
try {
$transifex = $this->transifexFactory->getTransifex();
} catch (InvalidConfigurationException) {
$output->writeln($this->translator->trans('mautic.core.command.transifex_no_credentials'));
return Command::FAILURE;
}
$statistics = $transifex->getConnector(Statistics::class);
\assert($statistics instanceof Statistics);
$translations = $transifex->getConnector(Translations::class);
\assert($translations instanceof Translations);
/** @var \SplQueue<Promise> $queue */
$queue = new \SplQueue();
foreach ($files as $bundle => $stringFiles) {
if ($bundleFilter && $bundle !== $bundleFilter) {
continue;
}
foreach ($stringFiles as $file) {
$name = $bundle.' '.str_replace('.ini', '', basename($file));
$resource = UrlHelper::stringURLUnicodeSlug($name);
$output->writeln($this->translator->trans('mautic.core.command.transifex_processing_resource', ['%resource%' => $name]));
try {
$response = $statistics->getLanguageStats($resource);
$languageStats = json_decode((string) $response->getBody(), true);
foreach ($languageStats['data'] as $stats) {
$language = ltrim($stats['relationships']['language']['data']['id'], 'l:');
if ('en' === $language) {
continue;
}
// If we are filtering on a specific language, skip anything that doesn't match
if ($languageFilter && $languageFilter !== $language) {
continue;
}
$output->writeln($this->translator->trans('mautic.core.command.transifex_processing_language', ['%language%' => $language]));
$completed = $stats['attributes']['translated_strings'] / $stats['attributes']['total_strings'];
// We only want resources which are 80% completed
if ($completed >= 0.8) {
$path = $translationDir.$language.'/'.$bundle.'/'.basename($file);
try {
$promise = $transifex->getApiConnector()->createPromise($translations->download($resource, $language));
$promise->setFilePath($path);
$queue->enqueue($promise);
} catch (ResponseException $responseException) {
$output->writeln($this->translator->trans($responseException->getMessage()));
}
}
}
} catch (\Exception $exception) {
$output->writeln($this->translator->trans('mautic.core.command.transifex_error_pulling_data', ['%message%' => $exception->getMessage()]));
return Command::FAILURE;
}
}
}
$transifex->getApiConnector()->fulfillPromises(
$queue,
function (ResponseInterface $response, Promise $promise) use ($output): void {
try {
$this->languageHelper->createLanguageFile($promise->getFilePath(), $response->getBody()->__toString());
} catch (\Exception $exception) {
$output->writeln($exception->getMessage());
}
},
function (ResponseException $exception) use ($output): void {
$output->writeln($exception->getMessage());
}
);
$output->writeln($this->translator->trans('mautic.core.command.transifex_resource_downloaded'));
return Command::SUCCESS;
}
protected static $defaultDescription = 'Fetches translations for Mautic from Transifex';
}
|