CreateJs.php 5.0 KB

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