update_po_templates.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #!/usr/bin/env php
  2. <?php
  3. /*
  4. * StatusNet - the distributed open-source microblogging tool
  5. * Copyright (C) 2008, 2009, StatusNet, Inc.
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. // Abort if called from a web server
  21. if (isset($_SERVER) && array_key_exists('REQUEST_METHOD', $_SERVER)) {
  22. print "This script must be run from the command line\n";
  23. exit();
  24. }
  25. define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
  26. function update_core($dir, $domain)
  27. {
  28. $old = getcwd();
  29. chdir($dir);
  30. passthru(<<<END
  31. xgettext \
  32. --from-code=UTF-8 \
  33. --default-domain=$domain \
  34. --output=locale/$domain.pot \
  35. --language=PHP \
  36. --add-comments=TRANS \
  37. --keyword="_m:1,1t" \
  38. --keyword="_m:1c,2,2t" \
  39. --keyword="_m:1,2,3t" \
  40. --keyword="_m:1c,2,3,4t" \
  41. --keyword="pgettext:1c,2" \
  42. --keyword="npgettext:1c,2,3" \
  43. index.php \
  44. actions/*.php \
  45. classes/*.php \
  46. lib/*.php \
  47. scripts/*.php
  48. END
  49. );
  50. chdir($old);
  51. }
  52. function do_update_plugin($dir, $domain)
  53. {
  54. $old = getcwd();
  55. chdir($dir);
  56. if (!file_exists('locale')) {
  57. mkdir('locale');
  58. }
  59. $files = get_plugin_sources(".");
  60. $cmd = <<<END
  61. xgettext \
  62. --from-code=UTF-8 \
  63. --default-domain=$domain \
  64. --output=locale/$domain.pot \
  65. --language=PHP \
  66. --add-comments=TRANS \
  67. --keyword='' \
  68. --keyword="_m:1,1t" \
  69. --keyword="_m:1c,2,2t" \
  70. --keyword="_m:1,2,3t" \
  71. --keyword="_m:1c,2,3,4t" \
  72. END;
  73. foreach ($files as $file) {
  74. $cmd .= ' ' . escapeshellarg($file);
  75. }
  76. passthru($cmd);
  77. chdir($old);
  78. }
  79. function get_plugins($dir)
  80. {
  81. $plugins = array();
  82. $dirs = new DirectoryIterator("$dir/plugins");
  83. foreach ($dirs as $item) {
  84. if ($item->isDir() && !$item->isDot()) {
  85. $name = $item->getBasename();
  86. if (file_exists("$dir/plugins/$name/{$name}Plugin.php")) {
  87. $plugins[] = $name;
  88. }
  89. }
  90. }
  91. return $plugins;
  92. }
  93. function get_plugin_sources($dir)
  94. {
  95. $files = array();
  96. $dirs = new RecursiveDirectoryIterator($dir);
  97. $iter = new RecursiveIteratorIterator($dirs);
  98. foreach ($iter as $pathname => $item) {
  99. if ($item->isFile() && preg_match('/\.php$/', $item->getBaseName())) {
  100. $files[] = $pathname;
  101. }
  102. }
  103. return $files;
  104. }
  105. function plugin_using_gettext($dir)
  106. {
  107. $files = get_plugin_sources($dir);
  108. foreach ($files as $pathname) {
  109. // Check if the file is using our _m gettext wrapper
  110. $code = file_get_contents($pathname);
  111. if (preg_match('/\b_m\(/', $code)) {
  112. return true;
  113. }
  114. }
  115. return false;
  116. }
  117. function update_plugin($basedir, $name)
  118. {
  119. $dir = "$basedir/plugins/$name";
  120. if (plugin_using_gettext($dir)) {
  121. do_update_plugin($dir, $name);
  122. return true;
  123. } else {
  124. return false;
  125. }
  126. }
  127. $args = $_SERVER['argv'];
  128. array_shift($args);
  129. $all = false;
  130. $core = false;
  131. $allplugins = false;
  132. $plugins = array();
  133. if (count($args) == 0) {
  134. $all = true;
  135. }
  136. foreach ($args as $arg) {
  137. if ($arg == '--all') {
  138. $all = true;
  139. } elseif ($arg == "--core") {
  140. $core = true;
  141. } elseif ($arg == "--plugins") {
  142. $allplugins = true;
  143. } elseif (substr($arg, 0, 9) == "--plugin=") {
  144. $plugins[] = substr($arg, 9);
  145. } elseif ($arg == '--help') {
  146. echo "options: --all --core --plugins --plugin=Foo\n\n";
  147. exit(0);
  148. }
  149. }
  150. if ($all || $core) {
  151. echo "core...";
  152. update_core(INSTALLDIR, 'statusnet');
  153. echo " ok\n";
  154. }
  155. if ($all || $allplugins) {
  156. $plugins = get_plugins(INSTALLDIR);
  157. }
  158. if ($plugins) {
  159. foreach ($plugins as $plugin) {
  160. echo "$plugin...";
  161. if (update_plugin(INSTALLDIR, $plugin)) {
  162. echo " ok\n";
  163. } else {
  164. echo " not localized\n";
  165. }
  166. }
  167. }