CreateJs.php 4.3 KB

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