Commit 5443c1bacd1be4e9036dd3ad32c65760439304ed
1 parent
cdc74456
Add tsmarty2c.php as smarty_to_gettext.php, a LGPL'd part of the
smarty-gettext project (http://smarty-gettext.sf.net/), written by Sagi Bashari <sagi@boom.org.il> git-svn-id: https://kt-dms.svn.sourceforge.net/svnroot/kt-dms/trunk@4646 c91229c3-7414-0410-bfa2-8a42b809f60b
Showing
1 changed file
with
132 additions
and
0 deletions
bin/smarty_to_gettext.php
0 → 100755
| 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$ | ||
| 33 | + * @link http://smarty-gettext.sf.net/ | ||
| 34 | + * @author Sagi Bashari <sagi@boom.org.il> | ||
| 35 | + * @copyright 2004-2005 Sagi Bashari | ||
| 36 | + */ | ||
| 37 | + | ||
| 38 | +// smarty open tag | ||
| 39 | +$ldq = preg_quote('{'); | ||
| 40 | + | ||
| 41 | +// smarty close tag | ||
| 42 | +$rdq = preg_quote('}'); | ||
| 43 | + | ||
| 44 | +// smarty command | ||
| 45 | +$cmd = preg_quote('i18n'); | ||
| 46 | + | ||
| 47 | +// extensions of smarty files, used when going through a directory | ||
| 48 | +$extensions = array('smarty'); | ||
| 49 | + | ||
| 50 | +// "fix" string - strip slashes, escape and convert new lines to \n | ||
| 51 | +function fs($str) | ||
| 52 | +{ | ||
| 53 | + $str = stripslashes($str); | ||
| 54 | + $str = trim($str); | ||
| 55 | + $str = str_replace('"', '\"', $str); | ||
| 56 | + $str = str_replace("\n", '\n', $str); | ||
| 57 | + return $str; | ||
| 58 | +} | ||
| 59 | + | ||
| 60 | +// rips gettext strings from $file and prints them in C format | ||
| 61 | +function do_file($file) | ||
| 62 | +{ | ||
| 63 | + $content = @file_get_contents($file); | ||
| 64 | + | ||
| 65 | + if (empty($content)) { | ||
| 66 | + return; | ||
| 67 | + } | ||
| 68 | + | ||
| 69 | + global $ldq, $rdq, $cmd; | ||
| 70 | + | ||
| 71 | + preg_match_all( | ||
| 72 | + "/{$ldq}\s*({$cmd})\s*([^{$rdq}]*){$rdq}([^{$ldq}]*){$ldq}\/\\1{$rdq}/", | ||
| 73 | + $content, | ||
| 74 | + $matches | ||
| 75 | + ); | ||
| 76 | + | ||
| 77 | + for ($i=0; $i < count($matches[0]); $i++) { | ||
| 78 | + // TODO: add line number | ||
| 79 | + echo "/* $file */\n"; // credit: Mike van Lammeren 2005-02-14 | ||
| 80 | + | ||
| 81 | + $content = $matches[3][$i]; | ||
| 82 | + if (!preg_match('/formatmatters\s*=\s*["\']?\s*(.[^\"\']*)\s*["\']?/', $matches[2][$i], $match)) { | ||
| 83 | + $replace = array( | ||
| 84 | + '@[\n\r]@' => ' ', | ||
| 85 | + ); | ||
| 86 | + $content = preg_replace(array_keys($replace), array_values($replace), $content); | ||
| 87 | + } | ||
| 88 | + if (preg_match('/plural\s*=\s*["\']?\s*(.[^\"\']*)\s*["\']?/', $matches[2][$i], $match)) { | ||
| 89 | + echo 'ngettext("'.fs($content).'","'.fs($match[1]).'",x);'."\n"; | ||
| 90 | + } else { | ||
| 91 | + echo 'gettext("'.fs($content).'");'."\n"; | ||
| 92 | + } | ||
| 93 | + | ||
| 94 | + echo "\n"; | ||
| 95 | + } | ||
| 96 | +} | ||
| 97 | + | ||
| 98 | +// go through a directory | ||
| 99 | +function do_dir($dir) | ||
| 100 | +{ | ||
| 101 | + $d = dir($dir); | ||
| 102 | + | ||
| 103 | + while (false !== ($entry = $d->read())) { | ||
| 104 | + if ($entry == '.' || $entry == '..') { | ||
| 105 | + continue; | ||
| 106 | + } | ||
| 107 | + | ||
| 108 | + $entry = $dir.'/'.$entry; | ||
| 109 | + | ||
| 110 | + if (is_dir($entry)) { // if a directory, go through it | ||
| 111 | + do_dir($entry); | ||
| 112 | + } else { // if file, parse only if extension is matched | ||
| 113 | + $pi = pathinfo($entry); | ||
| 114 | + | ||
| 115 | + if (isset($pi['extension']) && in_array($pi['extension'], $GLOBALS['extensions'])) { | ||
| 116 | + do_file($entry); | ||
| 117 | + } | ||
| 118 | + } | ||
| 119 | + } | ||
| 120 | + | ||
| 121 | + $d->close(); | ||
| 122 | +} | ||
| 123 | + | ||
| 124 | +for ($ac=1; $ac < $_SERVER['argc']; $ac++) { | ||
| 125 | + if (is_dir($_SERVER['argv'][$ac])) { // go through directory | ||
| 126 | + do_dir($_SERVER['argv'][$ac]); | ||
| 127 | + } else { // do file | ||
| 128 | + do_file($_SERVER['argv'][$ac]); | ||
| 129 | + } | ||
| 130 | +} | ||
| 131 | + | ||
| 132 | +?> |