CreateJs.php 4.9 KB

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