]> git.sur5r.net Git - bacula/bacula/blob - gui/bacula-web/tsmarty2c.php
commit changes
[bacula/bacula] / gui / bacula-web / tsmarty2c.php
1 #!/usr/bin/php -qn
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, and prints them to stdout in C format, 
23  * that can later be used with the standard gettext tools.
24  *
25  * Usage:
26  * ./tsmarty2c.php <filename or directory> <file2> <..> > smarty.c
27  *
28  * If a parameter is a directory, the template files within will be parsed.
29  *
30  * @package     smarty_gettext
31  * @version     0.9
32  * @link        http://www.boom.org.il/smarty/gettext/
33  * @author      Sagi Bashari <sagi@boom.org.il>
34  * @copyright 2004 Sagi Bashari
35  */
36
37 // smarty open tag
38 $ldq = preg_quote('{');
39
40 // smarty close tag
41 $rdq = preg_quote('}');
42
43 // smarty command
44 $cmd = preg_quote('t');
45
46 // extensions of smarty files, used when going through a directory
47 $extensions = array('tpl');
48
49 // "fix" string - strip slashes, escape and convert new lines to \n
50 function fs($str)
51 {
52         $str = stripslashes($str);
53         $str = str_replace('"', '\"', $str);
54         $str = str_replace("\n", '\n', $str);
55         return $str;
56 }
57
58 // rips gettext strings from $file and prints them in C format
59 function do_file($file)
60 {
61         $content = @file_get_contents($file);
62
63         if (empty($content)) {
64                 return;
65         }
66
67         global $ldq, $rdq, $cmd;
68
69         preg_match_all("/{$ldq}\s*({$cmd})\s*([^{$rdq}]*){$rdq}([^{$ldq}]*){$ldq}\/\\1{$rdq}/", $content, $matches);
70         
71         for ($i=0; $i < count($matches[0]); $i++) {
72                 if (preg_match('/plural\s*=\s*["\']?\s*(.[^\"\']*)\s*["\']?/', $matches[2][$i], $match)) {
73                         print 'ngettext("'.fs($matches[3][$i]).'","'.fs($match[1]).'",x);'."\n";
74                 } else {
75                         print 'gettext("'.fs($matches[3][$i]).'");'."\n";
76                 }
77         }
78 }
79
80 // go through a directory
81 function do_dir($dir)
82 {
83         $d = dir($dir);
84
85         while (false !== ($entry = $d->read())) {
86                 if ($entry == '.' || $entry == '..') {
87                         continue;
88                 }
89
90                 $entry = $dir.'/'.$entry;
91
92                 if (is_dir($entry)) { // if a directory, go through it
93                         do_dir($entry);
94                 } else { // if file, parse only if extension is matched
95                         $pi = pathinfo($entry);
96                         
97                         if (in_array($pi['extension'], $GLOBALS['extensions'])) {
98                                 do_file($entry);
99                         }
100                 }
101         }
102
103         $d->close();
104 }
105
106 for ($ac=1; $ac < $_SERVER['argc']; $ac++) {
107         if (is_dir($_SERVER['argv'][$ac])) { // go through directory
108                 do_dir($_SERVER['argv'][$ac]);
109         } else { // do file
110                 do_file($_SERVER['argv'][$ac]);
111         }
112 }
113
114 ?>