CreateJs.php 5.0 KB

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