CreateJs.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  6. * SPDX-License-Identifier: AGPL-3.0-only
  7. */
  8. namespace OC\Core\Command\L10n;
  9. use DirectoryIterator;
  10. use OCP\App\AppPathNotFoundException;
  11. use OCP\App\IAppManager;
  12. use Stecman\Component\Symfony\Console\BashCompletion\Completion\CompletionAwareInterface;
  13. use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
  14. use Symfony\Component\Console\Command\Command;
  15. use Symfony\Component\Console\Input\InputInterface;
  16. use Symfony\Component\Console\Input\InputOption;
  17. use Symfony\Component\Console\Output\OutputInterface;
  18. use UnexpectedValueException;
  19. class CreateJs extends Command implements CompletionAwareInterface {
  20. public function __construct(
  21. protected IAppManager $appManager,
  22. ) {
  23. parent::__construct();
  24. }
  25. protected function configure() {
  26. $this
  27. ->setName('l10n:createjs')
  28. ->setDescription('Create javascript translation files for a given app')
  29. ->addArgument(
  30. 'app',
  31. InputOption::VALUE_REQUIRED,
  32. 'name of the app'
  33. )
  34. ->addArgument(
  35. 'lang',
  36. InputOption::VALUE_OPTIONAL,
  37. 'name of the language'
  38. );
  39. }
  40. protected function execute(InputInterface $input, OutputInterface $output): int {
  41. $app = $input->getArgument('app');
  42. $lang = $input->getArgument('lang');
  43. $path = $this->appManager->getAppPath($app);
  44. $languages = $lang;
  45. if (empty($lang)) {
  46. $languages = $this->getAllLanguages($path);
  47. }
  48. foreach ($languages as $lang) {
  49. $this->writeFiles($app, $path, $lang, $output);
  50. }
  51. return 0;
  52. }
  53. private function getAllLanguages($path) {
  54. $result = [];
  55. foreach (new DirectoryIterator("$path/l10n") as $fileInfo) {
  56. if ($fileInfo->isDot()) {
  57. continue;
  58. }
  59. if ($fileInfo->isDir()) {
  60. continue;
  61. }
  62. if ($fileInfo->getExtension() !== 'php') {
  63. continue;
  64. }
  65. $result[] = substr($fileInfo->getBasename(), 0, -4);
  66. }
  67. return $result;
  68. }
  69. private function writeFiles($app, $path, $lang, OutputInterface $output) {
  70. [$translations, $plurals] = $this->loadTranslations($path, $lang);
  71. $this->writeJsFile($app, $path, $lang, $output, $translations, $plurals);
  72. $this->writeJsonFile($path, $lang, $output, $translations, $plurals);
  73. }
  74. private function writeJsFile($app, $path, $lang, OutputInterface $output, $translations, $plurals) {
  75. $jsFile = "$path/l10n/$lang.js";
  76. if (file_exists($jsFile)) {
  77. $output->writeln("File already exists: $jsFile");
  78. return;
  79. }
  80. $content = "OC.L10N.register(\n \"$app\",\n {\n ";
  81. $jsTrans = [];
  82. foreach ($translations as $id => $val) {
  83. if (is_array($val)) {
  84. $val = '[ ' . implode(',', $val) . ']';
  85. }
  86. $jsTrans[] = "\"$id\" : \"$val\"";
  87. }
  88. $content .= implode(",\n ", $jsTrans);
  89. $content .= "\n},\n\"$plurals\");\n";
  90. file_put_contents($jsFile, $content);
  91. $output->writeln("Javascript translation file generated: $jsFile");
  92. }
  93. private function writeJsonFile($path, $lang, OutputInterface $output, $translations, $plurals) {
  94. $jsFile = "$path/l10n/$lang.json";
  95. if (file_exists($jsFile)) {
  96. $output->writeln("File already exists: $jsFile");
  97. return;
  98. }
  99. $content = ['translations' => $translations, 'pluralForm' => $plurals];
  100. file_put_contents($jsFile, json_encode($content));
  101. $output->writeln("Json translation file generated: $jsFile");
  102. }
  103. private function loadTranslations($path, $lang) {
  104. $phpFile = "$path/l10n/$lang.php";
  105. $TRANSLATIONS = [];
  106. $PLURAL_FORMS = '';
  107. if (!file_exists($phpFile)) {
  108. throw new UnexpectedValueException("PHP translation file <$phpFile> does not exist.");
  109. }
  110. require $phpFile;
  111. return [$TRANSLATIONS, $PLURAL_FORMS];
  112. }
  113. /**
  114. * Return possible values for the named option
  115. *
  116. * @param string $optionName
  117. * @param CompletionContext $context
  118. * @return string[]
  119. */
  120. public function completeOptionValues($optionName, CompletionContext $context) {
  121. return [];
  122. }
  123. /**
  124. * Return possible values for the named argument
  125. *
  126. * @param string $argumentName
  127. * @param CompletionContext $context
  128. * @return string[]
  129. */
  130. public function completeArgumentValues($argumentName, CompletionContext $context) {
  131. if ($argumentName === 'app') {
  132. return $this->appManager->getAllAppsInAppsFolders();
  133. } elseif ($argumentName === 'lang') {
  134. $appName = $context->getWordAtIndex($context->getWordIndex() - 1);
  135. try {
  136. return $this->getAllLanguages($this->appManager->getAppPath($appName));
  137. } catch (AppPathNotFoundException) {
  138. return [];
  139. }
  140. }
  141. return [];
  142. }
  143. }