CreateJs.php 3.9 KB

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