diff options
author | Thomas Steur <thomas.steur@gmail.com> | 2013-11-11 04:22:54 +0400 |
---|---|---|
committer | Thomas Steur <thomas.steur@gmail.com> | 2013-11-11 04:22:54 +0400 |
commit | 3a2bfb0e865655f45a54fb83d05e0656b001043b (patch) | |
tree | 8f95884e3d3f5fcf88fe5587186002ab00acb338 /plugins/LanguagesManager/Commands/SetTranslations.php | |
parent | 1a3b0681ccee5db70cc9756be9f78307a5e3d2cb (diff) |
refs #4241 moving translation commands to LanguagesManager
Diffstat (limited to 'plugins/LanguagesManager/Commands/SetTranslations.php')
-rw-r--r-- | plugins/LanguagesManager/Commands/SetTranslations.php | 108 |
1 files changed, 108 insertions, 0 deletions
diff --git a/plugins/LanguagesManager/Commands/SetTranslations.php b/plugins/LanguagesManager/Commands/SetTranslations.php new file mode 100644 index 0000000000..62db2ee4e5 --- /dev/null +++ b/plugins/LanguagesManager/Commands/SetTranslations.php @@ -0,0 +1,108 @@ +<?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\LanguagesManager\Commands; + +use Piwik\Plugin\ConsoleCommand; +use Piwik\Plugins\LanguagesManager\API; +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\Translate\Validate\CoreTranslations; +use Piwik\Translate\Validate\NoScripts; +use Piwik\Translate\Writer; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Output\OutputInterface; + +/** + * @package CoreConsole + */ +class SetTranslations extends ConsoleCommand +{ + 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'); + + $languageCodes = API::getInstance()->getAvailableLanguages(); + + 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 |