4 * tsmarty2c.php - rips gettext strings from smarty template
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. *
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. *
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 * ------------------------------------------------------------------------- *
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.
26 * ./tsmarty2c.php <filename or directory> <file2> <..> > smarty.c
28 * If a parameter is a directory, the template files within will be parsed.
30 * @package smarty_gettext
32 * @link http://www.boom.org.il/smarty/gettext/
33 * @author Sagi Bashari <sagi@boom.org.il>
34 * @copyright 2004 Sagi Bashari
38 $ldq = preg_quote('{');
41 $rdq = preg_quote('}');
44 $cmd = preg_quote('t');
46 // extensions of smarty files, used when going through a directory
47 $extensions = array('tpl');
49 // "fix" string - strip slashes, escape and convert new lines to \n
52 $str = stripslashes($str);
53 $str = str_replace('"', '\"', $str);
54 $str = str_replace("\n", '\n', $str);
58 // rips gettext strings from $file and prints them in C format
59 function do_file($file)
61 $content = @file_get_contents($file);
63 if (empty($content)) {
67 global $ldq, $rdq, $cmd;
69 preg_match_all("/{$ldq}\s*({$cmd})\s*([^{$rdq}]*){$rdq}([^{$ldq}]*){$ldq}\/\\1{$rdq}/", $content, $matches);
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";
75 print 'gettext("'.fs($matches[3][$i]).'");'."\n";
80 // go through a directory
85 while (false !== ($entry = $d->read())) {
86 if ($entry == '.' || $entry == '..') {
90 $entry = $dir.'/'.$entry;
92 if (is_dir($entry)) { // if a directory, go through it
94 } else { // if file, parse only if extension is matched
95 $pi = pathinfo($entry);
97 if (in_array($pi['extension'], $GLOBALS['extensions'])) {
106 for ($ac=1; $ac < $_SERVER['argc']; $ac++) {
107 if (is_dir($_SERVER['argv'][$ac])) { // go through directory
108 do_dir($_SERVER['argv'][$ac]);
110 do_file($_SERVER['argv'][$ac]);