PageRenderTime 59ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/plugins/LanguagesManager/Commands/SetTranslations.php

https://github.com/CodeYellowBV/piwik
PHP | 105 lines | 75 code | 21 blank | 9 comment | 9 complexity | 96a5fa6fffbbad007de87db05272f505 MD5 | raw file
Possible License(s): LGPL-3.0, JSON, MIT, GPL-3.0, LGPL-2.1, GPL-2.0, AGPL-1.0, BSD-2-Clause, BSD-3-Clause
  1. <?php
  2. /**
  3. * Piwik - free/libre analytics platform
  4. *
  5. * @link http://piwik.org
  6. * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
  7. *
  8. */
  9. namespace Piwik\Plugins\LanguagesManager\Commands;
  10. use Piwik\Plugin\ConsoleCommand;
  11. use Piwik\Plugins\LanguagesManager\API;
  12. use Piwik\Translate\Filter\ByBaseTranslations;
  13. use Piwik\Translate\Filter\ByParameterCount;
  14. use Piwik\Translate\Filter\EmptyTranslations;
  15. use Piwik\Translate\Filter\EncodedEntities;
  16. use Piwik\Translate\Filter\UnnecassaryWhitespaces;
  17. use Piwik\Translate\Validate\CoreTranslations;
  18. use Piwik\Translate\Validate\NoScripts;
  19. use Piwik\Translate\Writer;
  20. use Symfony\Component\Console\Input\InputInterface;
  21. use Symfony\Component\Console\Input\InputOption;
  22. use Symfony\Component\Console\Output\OutputInterface;
  23. /**
  24. */
  25. class SetTranslations extends ConsoleCommand
  26. {
  27. protected function configure()
  28. {
  29. $this->setName('translations:set')
  30. ->setDescription('Sets new translations for a given language')
  31. ->addOption('code', 'c', InputOption::VALUE_REQUIRED, 'code of the language to set translations for')
  32. ->addOption('file', 'f', InputOption::VALUE_REQUIRED, 'json file to load new translations from')
  33. ->addOption('plugin', 'pl', InputOption::VALUE_OPTIONAL, 'optional name of plugin to set translations for');
  34. }
  35. protected function execute(InputInterface $input, OutputInterface $output)
  36. {
  37. $dialog = $this->getHelperSet()->get('dialog');
  38. $languageCode = $input->getOption('code');
  39. $filename = $input->getOption('file');
  40. $languageCodes = API::getInstance()->getAvailableLanguages();
  41. if (empty($languageCode) || !in_array($languageCode, $languageCodes)) {
  42. $languageCode = $dialog->askAndValidate($output, 'Please provide a valid language code: ', function ($code) use ($languageCodes) {
  43. if (!in_array($code, array_values($languageCodes))) {
  44. throw new \InvalidArgumentException(sprintf('Language code "%s" is invalid.', $code));
  45. }
  46. return $code;
  47. });
  48. }
  49. if (empty($filename) || !file_exists($filename)) {
  50. $filename = $dialog->askAndValidate($output, 'Please provide a file to load translations from: ', function ($file) {
  51. if (!file_exists($file)) {
  52. throw new \InvalidArgumentException(sprintf('File "%s" does not exist.', $file));
  53. }
  54. return $file;
  55. });
  56. }
  57. $output->writeln("Starting to import data from '$filename' to language '$languageCode'");
  58. $plugin = $input->getOption('plugin');
  59. $translationWriter = new Writer($languageCode, $plugin);
  60. $baseTranslations = $translationWriter->getTranslations("en");
  61. $translationWriter->addValidator(new NoScripts());
  62. if (empty($plugin)) {
  63. $translationWriter->addValidator(new CoreTranslations($baseTranslations));
  64. }
  65. $translationWriter->addFilter(new ByBaseTranslations($baseTranslations));
  66. $translationWriter->addFilter(new EmptyTranslations());
  67. $translationWriter->addFilter(new ByParameterCount($baseTranslations));
  68. $translationWriter->addFilter(new UnnecassaryWhitespaces($baseTranslations));
  69. $translationWriter->addFilter(new EncodedEntities());
  70. $translationData = file_get_contents($filename);
  71. $translations = json_decode($translationData, true);
  72. $translationWriter->setTranslations($translations);
  73. if (!$translationWriter->isValid()) {
  74. $output->writeln("Failed setting translations:" . $translationWriter->getValidationMessage());
  75. return;
  76. }
  77. if (!$translationWriter->hasTranslations()) {
  78. $output->writeln("No translations available");
  79. return;
  80. }
  81. $translationWriter->save();
  82. $output->writeln("Finished.");
  83. }
  84. }