createjs.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /**
  3. * @author Morris Jobke <hey@morrisjobke.de>
  4. * @author Thomas Müller <thomas.mueller@tmit.eu>
  5. *
  6. * @copyright Copyright (c) 2015, 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. class CreateJs extends Command {
  29. protected function configure() {
  30. $this
  31. ->setName('l10n:createjs')
  32. ->setDescription('Create javascript translation files for a given app')
  33. ->addArgument(
  34. 'app',
  35. InputOption::VALUE_REQUIRED,
  36. 'name of the app'
  37. )
  38. ->addArgument(
  39. 'lang',
  40. InputOption::VALUE_OPTIONAL,
  41. 'name of the language'
  42. );
  43. }
  44. protected function execute(InputInterface $input, OutputInterface $output) {
  45. $app = $input->getArgument('app');
  46. $lang = $input->getArgument('lang');
  47. $path = \OC_App::getAppPath($app);
  48. if ($path === false) {
  49. $output->writeln("The app <$app> is unknown.");
  50. return;
  51. }
  52. $languages = $lang;
  53. if (empty($lang)) {
  54. $languages= $this->getAllLanguages($path);
  55. }
  56. foreach($languages as $lang) {
  57. $this->writeFiles($app, $path, $lang, $output);
  58. }
  59. }
  60. private function getAllLanguages($path) {
  61. $result = array();
  62. foreach (new DirectoryIterator("$path/l10n") as $fileInfo) {
  63. if($fileInfo->isDot()) {
  64. continue;
  65. }
  66. if($fileInfo->isDir()) {
  67. continue;
  68. }
  69. if($fileInfo->getExtension() !== 'php') {
  70. continue;
  71. }
  72. $result[]= substr($fileInfo->getBasename(), 0, -4);
  73. }
  74. return $result;
  75. }
  76. private function writeFiles($app, $path, $lang, OutputInterface $output) {
  77. list($translations, $plurals) = $this->loadTranslations($path, $lang);
  78. $this->writeJsFile($app, $path, $lang, $output, $translations, $plurals);
  79. $this->writeJsonFile($path, $lang, $output, $translations, $plurals);
  80. }
  81. private function writeJsFile($app, $path, $lang, OutputInterface $output, $translations, $plurals) {
  82. $jsFile = "$path/l10n/$lang.js";
  83. if (file_exists($jsFile)) {
  84. $output->writeln("File already exists: $jsFile");
  85. return;
  86. }
  87. $content = "OC.L10N.register(\n \"$app\",\n {\n ";
  88. $jsTrans = array();
  89. foreach ($translations as $id => $val) {
  90. if (is_array($val)) {
  91. $val = '[ ' . join(',', $val) . ']';
  92. }
  93. $jsTrans[] = "\"$id\" : \"$val\"";
  94. }
  95. $content .= join(",\n ", $jsTrans);
  96. $content .= "\n},\n\"$plurals\");\n";
  97. file_put_contents($jsFile, $content);
  98. $output->writeln("Javascript translation file generated: $jsFile");
  99. }
  100. private function writeJsonFile($path, $lang, OutputInterface $output, $translations, $plurals) {
  101. $jsFile = "$path/l10n/$lang.json";
  102. if (file_exists($jsFile)) {
  103. $output->writeln("File already exists: $jsFile");
  104. return;
  105. }
  106. $content = array('translations' => $translations, 'pluralForm' => $plurals);
  107. file_put_contents($jsFile, json_encode($content));
  108. $output->writeln("Json translation file generated: $jsFile");
  109. }
  110. private function loadTranslations($path, $lang) {
  111. $phpFile = "$path/l10n/$lang.php";
  112. $TRANSLATIONS = array();
  113. $PLURAL_FORMS = '';
  114. require $phpFile;
  115. return array($TRANSLATIONS, $PLURAL_FORMS);
  116. }
  117. }