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
|
<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Plugins\LanguagesManager\Commands;
use Piwik\Container\StaticContainer;
use Piwik\Exception\AuthenticationFailedException;
use Piwik\Plugins\LanguagesManager\API as LanguagesManagerApi;
use Piwik\Translation\Weblate\API;
/**
*/
class FetchTranslations extends TranslationBase
{
public const DOWNLOAD_PATH = '/weblate';
protected function configure()
{
$path = StaticContainer::get('path.tmp') . self::DOWNLOAD_PATH;
$this->setName('translations:fetch')
->setDescription('Fetches translations files from Weblate to ' . $path)
->addOptionalValueOption('token', 't', 'Weblate API token')
->addOptionalValueOption('slug', 's', 'project slug on weblate', 'matomo')
->addOptionalValueOption('plugin', 'r', 'Plugin to update');
}
protected function doExecute(): int
{
$input = $this->getInput();
$output = $this->getOutput();
$output->setDecorated(true);
$apiToken = $input->getOption('token');
$plugin = $input->getOption('plugin');
$slug = $input->getOption('slug');
$resource = $plugin ? 'plugin-' . strtolower($plugin) : 'matomo-base';
$weblateApi = new API($apiToken, $slug);
// remove all existing translation files in download path
$files = glob($this->getDownloadPath() . DIRECTORY_SEPARATOR . '*.json');
array_map('unlink', $files);
if (!$weblateApi->resourceExists($resource)) {
$output->writeln("Skipping resource $resource as it doesn't exist on Weblate");
return self::SUCCESS;
}
$output->writeln("Fetching translations from Weblate for resource $resource");
try {
$languages = $weblateApi->getAvailableLanguageCodes();
if (!empty($plugin)) {
$languages = array_filter($languages, function ($language) {
return LanguagesManagerApi::getInstance()->isLanguageAvailable(str_replace('_', '-', strtolower($language)), true);
});
}
} catch (AuthenticationFailedException $e) {
$availableLanguages = LanguagesManagerApi::getInstance()->getAvailableLanguageNames(true);
$languageCodes = array();
foreach ($availableLanguages as $languageInfo) {
$codeParts = explode('-', $languageInfo['code']);
if (!empty($codeParts[1])) {
$codeParts[1] = strtoupper($codeParts[1]);
}
$languageCodes[] = implode('_', $codeParts);
}
$languageCodes = array_filter($languageCodes, function ($code) {
return !in_array($code, array('en', 'dev'));
});
$languages = $languageCodes;
}
$this->initProgressBar(count($languages));
$this->startProgressBar();
foreach ($languages as $language) {
try {
$translations = $weblateApi->getTranslations($resource, $language, true);
file_put_contents($this->getDownloadPath() . DIRECTORY_SEPARATOR . str_replace('_', '-', strtolower($language)) . '.json', $translations);
} catch (\Exception $e) {
$output->writeln("Error fetching language file $language: " . $e->getMessage());
}
$this->advanceProgressBar();
}
$this->finishProgressBar();
$output->writeln('');
return self::SUCCESS;
}
public static function getDownloadPath()
{
$path = StaticContainer::get('path.tmp') . self::DOWNLOAD_PATH;
if (!is_dir($path)) {
mkdir($path);
}
return $path;
}
}
|