tsmarty2c.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #!/usr/bin/env php
  2. <?php
  3. /**
  4. * tsmarty2c.php - rips gettext strings from smarty template
  5. *
  6. * ------------------------------------------------------------------------- *
  7. * This library is free software; you can redistribute it and/or *
  8. * modify it under the terms of the GNU Lesser General Public *
  9. * License as published by the Free Software Foundation; either *
  10. * version 2.1 of the License, or (at your option) any later version. *
  11. * *
  12. * This library 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 GNU *
  15. * Lesser General Public License for more details. *
  16. * *
  17. * You should have received a copy of the GNU Lesser General Public *
  18. * License along with this library; if not, write to the Free Software *
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
  20. * ------------------------------------------------------------------------- *
  21. *
  22. * This command line script rips gettext strings from smarty file,
  23. * and prints them to stdout in C format, that can later be used with the
  24. * standard gettext tools.
  25. *
  26. * Usage:
  27. * ./tsmarty2c.php <filename or directory> <file2> <..> > smarty.c
  28. *
  29. * If a parameter is a directory, the template files within will be parsed.
  30. *
  31. * @package smarty-gettext
  32. * @version $Id: tsmarty2c.php,v 1.3 2005/07/27 17:59:39 sagi Exp $
  33. * @link http://smarty-gettext.sf.net/
  34. * @author Sagi Bashari <sagi@boom.org.il>
  35. * @copyright 2004-2005 Sagi Bashari
  36. */
  37. // smarty open tag
  38. $ldq = preg_quote('{');
  39. // smarty close tag
  40. $rdq = preg_quote('}');
  41. // smarty command
  42. $cmd = preg_quote('t');
  43. // extensions of smarty files, used when going through a directory
  44. $extensions = array('tpl');
  45. // "fix" string - strip slashes, escape and convert new lines to \n
  46. function fs($str)
  47. {
  48. $str = stripslashes($str);
  49. $str = str_replace('"', '\"', $str);
  50. $str = str_replace("\n", '\n', $str);
  51. return $str;
  52. }
  53. // rips gettext strings from $file and prints them in C format
  54. function do_file($file)
  55. {
  56. $content = @file_get_contents($file);
  57. if (empty($content)) {
  58. return;
  59. }
  60. global $ldq, $rdq, $cmd;
  61. preg_match_all(
  62. "/{$ldq}\s*({$cmd})\s*([^{$rdq}]*){$rdq}([^{$ldq}]*){$ldq}\/\\1{$rdq}/",
  63. $content,
  64. $matches
  65. );
  66. for ($i=0; $i < count($matches[0]); $i++) {
  67. // TODO: add line number
  68. echo "/* $file */\n"; // credit: Mike van Lammeren 2005-02-14
  69. if (preg_match('/plural\s*=\s*["\']?\s*(.[^\"\']*)\s*["\']?/', $matches[2][$i], $match)) {
  70. echo 'ngettext("'.fs($matches[3][$i]).'","'.fs($match[1]).'",x);'."\n";
  71. } else {
  72. echo 'gettext("'.fs($matches[3][$i]).'");'."\n";
  73. }
  74. echo "\n";
  75. }
  76. }
  77. // go through a directory
  78. function do_dir($dir)
  79. {
  80. $d = dir($dir);
  81. while (false !== ($entry = $d->read())) {
  82. if ($entry == '.' || $entry == '..') {
  83. continue;
  84. }
  85. $entry = $dir.'/'.$entry;
  86. if (is_dir($entry)) { // if a directory, go through it
  87. do_dir($entry);
  88. } else { // if file, parse only if extension is matched
  89. $pi = pathinfo($entry);
  90. if (isset($pi['extension']) && in_array($pi['extension'], $GLOBALS['extensions'])) {
  91. do_file($entry);
  92. }
  93. }
  94. }
  95. $d->close();
  96. }
  97. for ($ac=1; $ac < $_SERVER['argc']; $ac++) {
  98. if (is_dir($_SERVER['argv'][$ac])) { // go through directory
  99. do_dir($_SERVER['argv'][$ac]);
  100. } else { // do file
  101. do_file($_SERVER['argv'][$ac]);
  102. }
  103. }
  104. ?>