diff --git a/core/Translation/Transifex/API.php b/core/Translation/Transifex/API.php index 6df4dd3b791..9691e8eb8b5 100644 --- a/core/Translation/Transifex/API.php +++ b/core/Translation/Transifex/API.php @@ -13,7 +13,7 @@ class API { - protected $apiUrl = 'https://www.transifex.com/api/2/project/piwik/'; + protected $apiUrl = 'https://www.transifex.com/api/2/'; protected $username = ''; protected $password = ''; protected $projectSlug = ''; @@ -25,21 +25,48 @@ public function __construct($username, $password, $project='piwik') $this->projectSlug = $project; } - public function getAvailableLanguageCodes() + public function getAvailableResources() + { + static $resources; + + if (empty($resources)) { + $apiPath = 'project/' . $this->projectSlug . '/resources'; + $resources = $this->getApiResults($apiPath); + } + + return $resources; + } + + public function resourceExists($resource) { - $languageCodes = array(); - $apiData = $this->getApiResults('languages'); - foreach ($apiData as $languageData) { - $languageCodes[] = $languageData->language_code; + $resources = $this->getAvailableResources(); + foreach ($resources as $res) { + if ($res->slug == $resource) { + return true; + } } + return false; + } + public function getAvailableLanguageCodes() + { + static $languageCodes = array(); + if (empty($languageCodes)) { + $apiData = $this->getApiResults('project/' . $this->projectSlug . '/languages'); + foreach ($apiData as $languageData) { + $languageCodes[] = $languageData->language_code; + } + } return $languageCodes; } public function getTranslations($resource, $language, $raw=false) { - $apiPath = 'resource/'.$resource.'/translation/'.$language.'/?mode=onlytranslated&file'; - return $this->getApiResults($apiPath, $raw); + if ($this->resourceExists($resource)) { + $apiPath = 'project/' . $this->projectSlug . '/resource/' . $resource . '/translation/' . $language . '/?mode=onlytranslated&file'; + return $this->getApiResults($apiPath, $raw); + } + return null; } protected function getApiResults($apiPath, $raw=false) @@ -49,6 +76,7 @@ protected function getApiResults($apiPath, $raw=false) $curl = curl_init($apiUrl); curl_setopt($curl, CURLOPT_USERPWD, sprintf("%s:%s", $this->username, $this->password)); curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); + curl_setopt($curl, CURLOPT_ENCODING, ''); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($curl); $httpStatus = curl_getinfo($curl, CURLINFO_HTTP_CODE); @@ -57,7 +85,7 @@ protected function getApiResults($apiPath, $raw=false) if ($httpStatus == 401) { throw new AuthenticationFailedException(); } else if ($httpStatus != 200) { - throw new Exception(); + throw new Exception('Error while getting API results', $httpStatus); } return $raw ? $response : json_decode($response); diff --git a/plugins/LanguagesManager/Commands/FetchFromTransifex.php b/plugins/LanguagesManager/Commands/FetchFromTransifex.php index aac1d10b2b3..e6df2bfde0e 100644 --- a/plugins/LanguagesManager/Commands/FetchFromTransifex.php +++ b/plugins/LanguagesManager/Commands/FetchFromTransifex.php @@ -41,12 +41,27 @@ protected function execute(InputInterface $input, OutputInterface $output) $resource = 'piwik-'. ($plugin ? 'plugin-'.strtolower($plugin) : 'base'); - $output->writeln("Fetching translations from Transifex for resource $resource"); - $transifexApi = new API($username, $password); + // remove all existing translation files in download path + $files = glob($this->getDownloadPath() . DIRECTORY_SEPARATOR . '*.json'); + array_map('unlink', $files); + + if (!$transifexApi->resourceExists($resource)) { + $output->writeln("Skipping resource $resource as it doesn't exist on Transifex"); + return; + } + + $output->writeln("Fetching translations from Transifex for resource $resource"); + $languages = $transifexApi->getAvailableLanguageCodes(); + if (!empty($plugin)) { + $languages = array_filter($languages, function($language) { + return \Piwik\Plugins\LanguagesManager\API::getInstance()->isLanguageAvailable(str_replace('_', '-', strtolower($language))); + }); + } + /** @var ProgressHelper $progress */ $progress = $this->getHelperSet()->get('progress'); diff --git a/plugins/LanguagesManager/Commands/Update.php b/plugins/LanguagesManager/Commands/Update.php index f09188e83a0..883a214b146 100644 --- a/plugins/LanguagesManager/Commands/Update.php +++ b/plugins/LanguagesManager/Commands/Update.php @@ -33,6 +33,8 @@ protected function configure() protected function execute(InputInterface $input, OutputInterface $output) { + $start = microtime(true); + /** @var DialogHelper $dialog */ $dialog = $this->getHelperSet()->get('dialog'); @@ -109,7 +111,7 @@ protected function execute(InputInterface $input, OutputInterface $output) $progress->finish(); } - $output->writeln("Finished."); + $output->writeln("Finished in " . round(microtime(true)-$start, 3) . "s"); } /**