Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsgiehl <stefan@piwik.org>2013-10-12 02:42:56 +0400
committersgiehl <stefan@piwik.org>2013-10-12 12:24:51 +0400
commita544cbf19c22e58eabd43fc63b34bc361badc63b (patch)
tree825fc030d0c36f69e0f45d28e477287a87308314 /plugins
parent00ce3a830d487ee31b9cc90afd4e4ba7d111d370 (diff)
added console method to update translation files / set translations from a file
Diffstat (limited to 'plugins')
-rw-r--r--plugins/CoreConsole/Translations/FetchFromOTrance.php26
-rw-r--r--plugins/CoreConsole/Translations/LanguageCodes.php2
-rw-r--r--plugins/CoreConsole/Translations/LanguageNames.php2
-rw-r--r--plugins/CoreConsole/Translations/SetTranslations.php113
-rw-r--r--plugins/CoreConsole/Translations/Update.php71
5 files changed, 196 insertions, 18 deletions
diff --git a/plugins/CoreConsole/Translations/FetchFromOTrance.php b/plugins/CoreConsole/Translations/FetchFromOTrance.php
index c2adffc45c..851f568045 100644
--- a/plugins/CoreConsole/Translations/FetchFromOTrance.php
+++ b/plugins/CoreConsole/Translations/FetchFromOTrance.php
@@ -12,9 +12,7 @@
namespace Piwik\Plugins\CoreConsole\Translations;
use Piwik\Console\Command;
-use Piwik\Plugins\LanguagesManager\API;
use Piwik\Unzip;
-use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
@@ -30,8 +28,8 @@ class FetchFromOTrance extends Command
{
$this->setName('translations:fetch')
->setDescription('Fetches translations files from oTrance to '.self::DOWNLOADPATH)
- ->addOption('username', 'u', InputOption::VALUE_REQUIRED, 'oTrance username')
- ->addOption('password', 'p', InputOption::VALUE_REQUIRED, 'oTrance password');
+ ->addOption('username', 'u', InputOption::VALUE_OPTIONAL, 'oTrance username')
+ ->addOption('password', 'p', InputOption::VALUE_OPTIONAL, 'oTrance password');
}
protected function execute(InputInterface $input, OutputInterface $output)
@@ -40,7 +38,7 @@ class FetchFromOTrance extends Command
$dialog = $this->getHelperSet()->get('dialog');
- $cookieFile = $this->getDownloadPath() . DIRECTORY_SEPARATOR . 'cookie.txt';
+ $cookieFile = self::getDownloadPath() . DIRECTORY_SEPARATOR . 'cookie.txt';
@unlink($cookieFile);
$username = $input->getOption('username');
@@ -110,9 +108,9 @@ class FetchFromOTrance extends Command
}
// download language pack
- $packageHandle = fopen($this->getDownloadPath() . DIRECTORY_SEPARATOR . 'language_pack.tar.gz', 'w');
+ $packageHandle = fopen(self::getDownloadPath() . DIRECTORY_SEPARATOR . 'language_pack.tar.gz', 'w');
$curl = curl_init('http://translations.piwik.org/public/downloads/download/file/'.$downloadPackage);
- curl_setopt($curl, CURLOPT_COOKIEFILE, $this->getDownloadPath() . DIRECTORY_SEPARATOR . 'cookie.txt');
+ curl_setopt($curl, CURLOPT_COOKIEFILE, self::getDownloadPath() . DIRECTORY_SEPARATOR . 'cookie.txt');
curl_setopt($curl, CURLOPT_FILE, $packageHandle);
curl_exec($curl);
curl_close($curl);
@@ -121,13 +119,13 @@ class FetchFromOTrance extends Command
$output->writeln("Extracting package...");
- $unzipper = Unzip::factory('tar.gz', $this->getDownloadPath() . DIRECTORY_SEPARATOR . 'language_pack.tar.gz');
- $unzipper->extract($this->getDownloadPath());
+ $unzipper = Unzip::factory('tar.gz', self::getDownloadPath() . DIRECTORY_SEPARATOR . 'language_pack.tar.gz');
+ $unzipper->extract(self::getDownloadPath());
- @unlink($this->getDownloadPath() . DIRECTORY_SEPARATOR . 'en.php');
- @unlink($this->getDownloadPath() . DIRECTORY_SEPARATOR . 'language_pack.tar.gz');
+ @unlink(self::getDownloadPath() . DIRECTORY_SEPARATOR . 'en.php');
+ @unlink(self::getDownloadPath() . DIRECTORY_SEPARATOR . 'language_pack.tar.gz');
- $filesToConvert = _glob($this->getDownloadPath() . DIRECTORY_SEPARATOR . '*.php');
+ $filesToConvert = _glob(self::getDownloadPath() . DIRECTORY_SEPARATOR . '*.php');
$output->writeln("Converting downloaded php files to json");
@@ -145,7 +143,7 @@ class FetchFromOTrance extends Command
}
$translations = $nested;
$data = json_encode($translations, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
- $newFile = sprintf("%s/%s.json", $this->getDownloadPath(), $basename[0]);
+ $newFile = sprintf("%s/%s.json", self::getDownloadPath(), $basename[0]);
file_put_contents($newFile, $data);
@unlink($filename);
@@ -157,7 +155,7 @@ class FetchFromOTrance extends Command
$output->writeln("Finished fetching new language files from oTrance");
}
- protected function getDownloadPath() {
+ public static function getDownloadPath() {
return PIWIK_DOCUMENT_ROOT . DIRECTORY_SEPARATOR . self::DOWNLOADPATH;
}
diff --git a/plugins/CoreConsole/Translations/LanguageCodes.php b/plugins/CoreConsole/Translations/LanguageCodes.php
index 233ae8de67..14b2052b8e 100644
--- a/plugins/CoreConsole/Translations/LanguageCodes.php
+++ b/plugins/CoreConsole/Translations/LanguageCodes.php
@@ -13,9 +13,7 @@ namespace Piwik\Plugins\CoreConsole\Translations;
use Piwik\Console\Command;
use Piwik\Plugins\LanguagesManager\API;
-use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
-use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
/**
diff --git a/plugins/CoreConsole/Translations/LanguageNames.php b/plugins/CoreConsole/Translations/LanguageNames.php
index 0fa97ac249..d3208995cd 100644
--- a/plugins/CoreConsole/Translations/LanguageNames.php
+++ b/plugins/CoreConsole/Translations/LanguageNames.php
@@ -13,9 +13,7 @@ namespace Piwik\Plugins\CoreConsole\Translations;
use Piwik\Console\Command;
use Piwik\Plugins\LanguagesManager\API;
-use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
-use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
/**
diff --git a/plugins/CoreConsole/Translations/SetTranslations.php b/plugins/CoreConsole/Translations/SetTranslations.php
new file mode 100644
index 0000000000..9a95c7fbd7
--- /dev/null
+++ b/plugins/CoreConsole/Translations/SetTranslations.php
@@ -0,0 +1,113 @@
+<?php
+/**
+ * Piwik - Open source web analytics
+ *
+ * @link http://piwik.org
+ * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
+ *
+ * @category Piwik_Plugins
+ * @package CoreConsole
+ */
+
+namespace Piwik\Plugins\CoreConsole\Translations;
+
+use Piwik\Console\Command;
+use Piwik\Translate\Writer;
+use Piwik\Translate\Validate\NoScripts;
+use Piwik\Translate\Validate\CoreTranslations;
+use Piwik\Translate\Filter\ByBaseTranslations;
+use Piwik\Translate\Filter\ByParameterCount;
+use Piwik\Translate\Filter\EmptyTranslations;
+use Piwik\Translate\Filter\EncodedEntities;
+use Piwik\Translate\Filter\UnnecassaryWhitespaces;
+use Piwik\Plugins\LanguagesManager\API;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Input\InputOption;
+use Symfony\Component\Console\Output\OutputInterface;
+
+/**
+ * @package CoreConsole
+ */
+class SetTranslations extends Command
+{
+ protected function configure()
+ {
+ $this->setName('translations:set')
+ ->setDescription('Sets new translations for a given language')
+ ->addOption('code', 'c', InputOption::VALUE_REQUIRED, 'code of the language to set translations for')
+ ->addOption('file', 'f', InputOption::VALUE_REQUIRED, 'json file to load new translations from')
+ ->addOption('plugin', 'pl', InputOption::VALUE_OPTIONAL, 'optional name of plugin to set translations for');
+ }
+
+ protected function execute(InputInterface $input, OutputInterface $output)
+ {
+ $dialog = $this->getHelperSet()->get('dialog');
+
+ $languageCode = $input->getOption('code');
+ $filename = $input->getOption('file');
+
+ $languages = API::getInstance()->getAvailableLanguageNames();
+
+ $languageCodes = array();
+ foreach ($languages AS $languageInfo) {
+ $languageCodes[] = $languageInfo['code'];
+ }
+
+ if (empty($languageCode) || !in_array($languageCode, $languageCodes)) {
+ $languageCode = $dialog->askAndValidate($output, 'Please provide a valid language code: ', function ($code) use ($languageCodes) {
+ if (!in_array($code, array_values($languageCodes))) {
+ throw new \InvalidArgumentException(sprintf('Language code "%s" is invalid.', $code));
+ }
+
+ return $code;
+ });
+ }
+
+ if (empty($filename) || !file_exists($filename)) {
+ $filename = $dialog->askAndValidate($output, 'Please provide a file to load translations from: ', function ($file) {
+ if (!file_exists($file)) {
+ throw new \InvalidArgumentException(sprintf('File "%s" does not exist.', $file));
+ }
+
+ return $file;
+ });
+ }
+
+ $output->writeln("Starting to import data from '$filename' to language '$languageCode'");
+
+ $plugin = $input->getOption('plugin');
+ $translationWriter = new Writer($languageCode, $plugin);
+
+ $baseTranslations = $translationWriter->getTranslations("en");
+
+ $translationWriter->addValidator(new NoScripts());
+ if (empty($plugin)) {
+ $translationWriter->addValidator(new CoreTranslations($baseTranslations));
+ }
+
+ $translationWriter->addFilter(new ByBaseTranslations($baseTranslations));
+ $translationWriter->addFilter(new EmptyTranslations());
+ $translationWriter->addFilter(new ByParameterCount($baseTranslations));
+ $translationWriter->addFilter(new UnnecassaryWhitespaces($baseTranslations));
+ $translationWriter->addFilter(new EncodedEntities());
+
+ $translationData = file_get_contents($filename);
+ $translations = json_decode($translationData, true);
+
+ $translationWriter->setTranslations($translations);
+
+ if (!$translationWriter->isValid()) {
+ $output->writeln("Failed setting translations:" . $translationWriter->getValidationMessage());
+ return;
+ }
+
+ if (!$translationWriter->hasTranslations()) {
+ $output->writeln("No translations available");
+ return;
+ }
+
+ $translationWriter->save();
+
+ $output->writeln("Finished.");
+ }
+} \ No newline at end of file
diff --git a/plugins/CoreConsole/Translations/Update.php b/plugins/CoreConsole/Translations/Update.php
new file mode 100644
index 0000000000..8c8274c133
--- /dev/null
+++ b/plugins/CoreConsole/Translations/Update.php
@@ -0,0 +1,71 @@
+<?php
+/**
+ * Piwik - Open source web analytics
+ *
+ * @link http://piwik.org
+ * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
+ *
+ * @category Piwik_Plugins
+ * @package CoreConsole
+ */
+
+namespace Piwik\Plugins\CoreConsole\Translations;
+
+use Piwik\Console\Command;
+use Piwik\Plugins\LanguagesManager\API;
+use Symfony\Component\Console\Input\ArrayInput;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Input\InputOption;
+use Symfony\Component\Console\Output\OutputInterface;
+
+/**
+ * @package CoreConsole
+ */
+class Update extends Command
+{
+ protected function configure()
+ {
+ $this->setName('translations:update')
+ ->setDescription('Updates translation files')
+ ->addOption('username', 'u', InputOption::VALUE_OPTIONAL, 'oTrance username')
+ ->addOption('password', 'p', InputOption::VALUE_OPTIONAL, 'oTrance password')
+ ->addOption('plugin', 'l', InputOption::VALUE_OPTIONAL, 'optional name of plugin to update translations for');
+ }
+
+ protected function execute(InputInterface $input, OutputInterface $output)
+ {
+ $command = $this->getApplication()->find('translations:fetch');
+ $arguments = array(
+ 'command' => 'translations:fetch',
+ '--username' => $input->getOption('username'),
+ '--password' => $input->getOption('password'),
+ );
+ $command->run(new ArrayInput($arguments), $output);
+
+ $languages = API::getInstance()->getAvailableLanguageNames();
+
+ $languageCodes = array();
+ foreach ($languages AS $languageInfo) {
+ $languageCodes[] = $languageInfo['code'];
+ }
+
+ foreach ($languageCodes AS $code) {
+
+ $filename = FetchFromOTrance::getDownloadPath() . DIRECTORY_SEPARATOR . $code . '.json';
+
+ if (file_exists($filename)) {
+
+ $command = $this->getApplication()->find('translations:set');
+ $arguments = array(
+ 'command' => 'translations:set',
+ '--code' => $code,
+ '--file' => $filename,
+ '--plugin' => $input->getOption('plugin')
+ );
+ $command->run(new ArrayInput($arguments), $output);
+ }
+ }
+
+ $output->writeln("Finished.");
+ }
+} \ No newline at end of file