CreateJs.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Thomas Müller <thomas.mueller@tmit.eu>
  8. *
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. namespace OC\Core\Command\L10n;
  25. use DirectoryIterator;
  26. use Stecman\Component\Symfony\Console\BashCompletion\Completion\CompletionAwareInterface;
  27. use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
  28. use Symfony\Component\Console\Command\Command;
  29. use Symfony\Component\Console\Input\InputInterface;
  30. use Symfony\Component\Console\Input\InputOption;
  31. use Symfony\Component\Console\Output\OutputInterface;
  32. use UnexpectedValueException;
  33. class CreateJs extends Command implements CompletionAwareInterface {
  34. protected function configure() {
  35. $this
  36. ->setName('l10n:createjs')
  37. ->setDescription('Create javascript translation files for a given app')
  38. ->addArgument(
  39. 'app',
  40. InputOption::VALUE_REQUIRED,
  41. 'name of the app'
  42. )
  43. ->addArgument(
  44. 'lang',
  45. InputOption::VALUE_OPTIONAL,
  46. 'name of the language'
  47. );
  48. }
  49. protected function execute(InputInterface $input, OutputInterface $output) {
  50. $app = $input->getArgument('app');
  51. $lang = $input->getArgument('lang');
  52. $path = \OC_App::getAppPath($app);
  53. if ($path === false) {
  54. $output->writeln("The app <$app> is unknown.");
  55. return;
  56. }
  57. $languages = $lang;
  58. if (empty($lang)) {
  59. $languages= $this->getAllLanguages($path);
  60. }
  61. foreach($languages as $lang) {
  62. $this->writeFiles($app, $path, $lang, $output);
  63. }
  64. }
  65. private function getAllLanguages($path) {
  66. $result = array();
  67. foreach (new DirectoryIterator("$path/l10n") as $fileInfo) {
  68. if($fileInfo->isDot()) {
  69. continue;
  70. }
  71. if($fileInfo->isDir()) {
  72. continue;
  73. }
  74. if($fileInfo->getExtension() !== 'php') {
  75. continue;
  76. }
  77. $result[]= substr($fileInfo->getBasename(), 0, -4);
  78. }
  79. return $result;
  80. }
  81. private function writeFiles($app, $path, $lang, OutputInterface $output) {
  82. list($translations, $plurals) = $this->loadTranslations($path, $lang);
  83. $this->writeJsFile($app, $path, $lang, $output, $translations, $plurals);
  84. $this->writeJsonFile($path, $lang, $output, $translations, $plurals);
  85. }
  86. private function writeJsFile($app, $path, $lang, OutputInterface $output, $translations, $plurals) {
  87. $jsFile = "$path/l10n/$lang.js";
  88. if (file_exists($jsFile)) {
  89. $output->writeln("File already exists: $jsFile");
  90. return;
  91. }
  92. $content = "OC.L10N.register(\n \"$app\",\n {\n ";
  93. $jsTrans = array();
  94. foreach ($translations as $id => $val) {
  95. if (is_array($val)) {
  96. $val = '[ ' . implode(',', $val) . ']';
  97. }
  98. $jsTrans[] = "\"$id\" : \"$val\"";
  99. }
  100. $content .= implode(",\n ", $jsTrans);
  101. $content .= "\n},\n\"$plurals\");\n";
  102. file_put_contents($jsFile, $content);
  103. $output->writeln("Javascript translation file generated: $jsFile");
  104. }
  105. private function writeJsonFile($path, $lang, OutputInterface $output, $translations, $plurals) {
  106. $jsFile = "$path/l10n/$lang.json";
  107. if (file_exists($jsFile)) {
  108. $output->writeln("File already exists: $jsFile");
  109. return;
  110. }
  111. $content = array('translations' => $translations, 'pluralForm' => $plurals);
  112. file_put_contents($jsFile, json_encode($content));
  113. $output->writeln("Json translation file generated: $jsFile");
  114. }
  115. private function loadTranslations($path, $lang) {
  116. $phpFile = "$path/l10n/$lang.php";
  117. $TRANSLATIONS = array();
  118. $PLURAL_FORMS = '';
  119. if (!file_exists($phpFile)) {
  120. throw new UnexpectedValueException("PHP translation file <$phpFile> does not exist.");
  121. }
  122. require $phpFile;
  123. return array($TRANSLATIONS, $PLURAL_FORMS);
  124. }
  125. /**
  126. * Return possible values for the named option
  127. *
  128. * @param string $optionName
  129. * @param CompletionContext $context
  130. * @return string[]
  131. */
  132. public function completeOptionValues($optionName, CompletionContext $context) {
  133. return [];
  134. }
  135. /**
  136. * Return possible values for the named argument
  137. *
  138. * @param string $argumentName
  139. * @param CompletionContext $context
  140. * @return string[]
  141. */
  142. public function completeArgumentValues($argumentName, CompletionContext $context) {
  143. if ($argumentName === 'app') {
  144. return \OC_App::getAllApps();
  145. } else if ($argumentName === 'lang') {
  146. $appName = $context->getWordAtIndex($context->getWordIndex() - 1);
  147. return $this->getAllLanguages(\OC_App::getAppPath($appName));
  148. }
  149. return [];
  150. }
  151. }