update_po_templates.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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', dirname(__DIR__));
  26. define('PUBLICDIR', INSTALLDIR);
  27. function update_core($dir, $domain)
  28. {
  29. $old = getcwd();
  30. chdir($dir);
  31. passthru(<<<END
  32. xgettext \
  33. --from-code=UTF-8 \
  34. --default-domain=$domain \
  35. --output=locale/$domain.pot \
  36. --language=PHP \
  37. --add-comments=TRANS \
  38. --keyword="_m:1,1t" \
  39. --keyword="_m:1c,2,2t" \
  40. --keyword="_m:1,2,3t" \
  41. --keyword="_m:1c,2,3,4t" \
  42. --keyword="pgettext:1c,2" \
  43. --keyword="npgettext:1c,2,3" \
  44. public/index.php \
  45. actions/*.php \
  46. classes/*.php \
  47. lib/*.php \
  48. scripts/*.php
  49. END
  50. );
  51. chdir($old);
  52. }
  53. function do_update_module($dir, $domain)
  54. {
  55. $old = getcwd();
  56. chdir($dir);
  57. if (!file_exists('locale')) {
  58. mkdir('locale');
  59. }
  60. $files = get_module_sources(".");
  61. $cmd = <<<END
  62. xgettext \
  63. --from-code=UTF-8 \
  64. --default-domain=$domain \
  65. --output=locale/$domain.pot \
  66. --language=PHP \
  67. --add-comments=TRANS \
  68. --keyword='' \
  69. --keyword="_m:1,1t" \
  70. --keyword="_m:1c,2,2t" \
  71. --keyword="_m:1,2,3t" \
  72. --keyword="_m:1c,2,3,4t" \
  73. END;
  74. foreach ($files as $file) {
  75. $cmd .= ' ' . escapeshellarg($file);
  76. }
  77. passthru($cmd);
  78. chdir($old);
  79. }
  80. function do_update_plugin($dir, $domain)
  81. {
  82. $old = getcwd();
  83. chdir($dir);
  84. if (!file_exists('locale')) {
  85. mkdir('locale');
  86. }
  87. $files = get_plugin_sources(".");
  88. $cmd = <<<END
  89. xgettext \
  90. --from-code=UTF-8 \
  91. --default-domain=$domain \
  92. --output=locale/$domain.pot \
  93. --language=PHP \
  94. --add-comments=TRANS \
  95. --keyword='' \
  96. --keyword="_m:1,1t" \
  97. --keyword="_m:1c,2,2t" \
  98. --keyword="_m:1,2,3t" \
  99. --keyword="_m:1c,2,3,4t" \
  100. END;
  101. foreach ($files as $file) {
  102. $cmd .= ' ' . escapeshellarg($file);
  103. }
  104. passthru($cmd);
  105. chdir($old);
  106. }
  107. function get_modules($dir)
  108. {
  109. $plugins = array();
  110. $dirs = new DirectoryIterator("$dir/modules");
  111. foreach ($dirs as $item) {
  112. if ($item->isDir() && !$item->isDot()) {
  113. $name = $item->getBasename();
  114. if (file_exists("$dir/modules/$name/{$name}Module.php")) {
  115. $plugins[] = $name;
  116. }
  117. }
  118. }
  119. return $plugins;
  120. }
  121. function get_plugins($dir)
  122. {
  123. $plugins = array();
  124. $dirs = new DirectoryIterator("$dir/plugins");
  125. foreach ($dirs as $item) {
  126. if ($item->isDir() && !$item->isDot()) {
  127. $name = $item->getBasename();
  128. if (file_exists("$dir/plugins/$name/{$name}Plugin.php")) {
  129. $plugins[] = $name;
  130. }
  131. }
  132. }
  133. return $plugins;
  134. }
  135. function get_module_sources($dir)
  136. {
  137. $files = array();
  138. $dirs = new RecursiveDirectoryIterator($dir);
  139. $iter = new RecursiveIteratorIterator($dirs);
  140. foreach ($iter as $pathname => $item) {
  141. if ($item->isFile() && preg_match('/\.php$/', $item->getBaseName())) {
  142. $files[] = $pathname;
  143. }
  144. }
  145. return $files;
  146. }
  147. function get_plugin_sources($dir)
  148. {
  149. $files = array();
  150. $dirs = new RecursiveDirectoryIterator($dir);
  151. $iter = new RecursiveIteratorIterator($dirs);
  152. foreach ($iter as $pathname => $item) {
  153. if ($item->isFile() && preg_match('/\.php$/', $item->getBaseName())) {
  154. $files[] = $pathname;
  155. }
  156. }
  157. return $files;
  158. }
  159. function module_using_gettext($dir)
  160. {
  161. $files = get_module_sources($dir);
  162. foreach ($files as $pathname) {
  163. // Check if the file is using our _m gettext wrapper
  164. $code = file_get_contents($pathname);
  165. if (preg_match('/\b_m\(/', $code)) {
  166. return true;
  167. }
  168. }
  169. return false;
  170. }
  171. function plugin_using_gettext($dir)
  172. {
  173. $files = get_plugin_sources($dir);
  174. foreach ($files as $pathname) {
  175. // Check if the file is using our _m gettext wrapper
  176. $code = file_get_contents($pathname);
  177. if (preg_match('/\b_m\(/', $code)) {
  178. return true;
  179. }
  180. }
  181. return false;
  182. }
  183. function update_module($basedir, $name)
  184. {
  185. $dir = "$basedir/modules/$name";
  186. if (module_using_gettext($dir)) {
  187. do_update_module($dir, $name);
  188. return true;
  189. } else {
  190. return false;
  191. }
  192. }
  193. function update_plugin($basedir, $name)
  194. {
  195. $dir = "$basedir/plugins/$name";
  196. if (plugin_using_gettext($dir)) {
  197. do_update_plugin($dir, $name);
  198. return true;
  199. } else {
  200. return false;
  201. }
  202. }
  203. $args = $_SERVER['argv'];
  204. array_shift($args);
  205. $all = false;
  206. $core = false;
  207. $allmodules = false;
  208. $modules = array();
  209. $allplugins = false;
  210. $plugins = array();
  211. if (count($args) == 0) {
  212. $all = true;
  213. }
  214. foreach ($args as $arg) {
  215. if ($arg == '--all') {
  216. $all = true;
  217. } elseif ($arg == "--core") {
  218. $core = true;
  219. } elseif ($arg == "--modules") {
  220. $allmodules = true;
  221. } elseif ($arg == "--plugins") {
  222. $allplugins = true;
  223. } elseif (substr($arg, 0, 9) == "--module=") {
  224. $modules[] = substr($arg, 9);
  225. } elseif (substr($arg, 0, 9) == "--plugin=") {
  226. $plugins[] = substr($arg, 9);
  227. } elseif ($arg == '--help') {
  228. echo "options: --all --core --plugins --plugin=Foo --modules --module=Foo\n\n";
  229. exit(0);
  230. }
  231. }
  232. if ($all || $core) {
  233. echo "core...";
  234. update_core(INSTALLDIR, 'statusnet');
  235. echo " ok\n";
  236. }
  237. if ($all || $allmodules) {
  238. $plugins = get_modules(INSTALLDIR);
  239. }
  240. if ($all || $allplugins) {
  241. $plugins = get_plugins(INSTALLDIR);
  242. }
  243. if ($modules) {
  244. foreach ($modules as $module) {
  245. echo "$module...";
  246. if (update_module(INSTALLDIR, $plugin)) {
  247. echo " ok\n";
  248. } else {
  249. echo " not localized\n";
  250. }
  251. }
  252. }
  253. if ($plugins) {
  254. foreach ($plugins as $plugin) {
  255. echo "$plugin...";
  256. if (update_plugin(INSTALLDIR, $plugin)) {
  257. echo " ok\n";
  258. } else {
  259. echo " not localized\n";
  260. }
  261. }
  262. }