From 050cd9c0dcfedc02f7c26f73a08055ce144ed403 Mon Sep 17 00:00:00 2001 From: Deon George Date: Sat, 14 Nov 2009 14:45:29 +1100 Subject: [PATCH] Add gettext-smarty 1.0b1 --- includes/smarty/plugins/block.t.php | 126 ++++++++++++++++++++++++++++ includes/smarty/tsmarty2c.php | 124 +++++++++++++++++++++++++++ 2 files changed, 250 insertions(+) create mode 100644 includes/smarty/plugins/block.t.php create mode 100755 includes/smarty/tsmarty2c.php diff --git a/includes/smarty/plugins/block.t.php b/includes/smarty/plugins/block.t.php new file mode 100644 index 00000000..ca7382ae --- /dev/null +++ b/includes/smarty/plugins/block.t.php @@ -0,0 +1,126 @@ + + * @copyright 2004-2005 Sagi Bashari + */ + +/** + * Replaces arguments in a string with their values. + * Arguments are represented by % followed by their number. + * + * @param string Source string + * @param mixed Arguments, can be passed in an array or through single variables. + * @returns string Modified string + */ +function smarty_gettext_strarg($str) +{ + $tr = array(); + $p = 0; + + for ($i=1; $i < func_num_args(); $i++) { + $arg = func_get_arg($i); + + if (is_array($arg)) { + foreach ($arg as $aarg) { + $tr['%'.++$p] = $aarg; + } + } else { + $tr['%'.++$p] = $arg; + } + } + + return strtr($str, $tr); +} + +/** + * Smarty block function, provides gettext support for smarty. + * + * The block content is the text that should be translated. + * + * Any parameter that is sent to the function will be represented as %n in the translation text, + * where n is 1 for the first parameter. The following parameters are reserved: + * - escape - sets escape mode: + * - 'html' for HTML escaping, this is the default. + * - 'js' for javascript escaping. + * - 'url' for url escaping. + * - 'no'/'off'/0 - turns off escaping + * - plural - The plural version of the text (2nd parameter of ngettext()) + * - count - The item count for plural mode (3rd parameter of ngettext()) + */ +function smarty_block_t($params, $text, &$smarty) +{ + $text = stripslashes($text); + + // set escape mode + if (isset($params['escape'])) { + $escape = $params['escape']; + unset($params['escape']); + } + + // set plural version + if (isset($params['plural'])) { + $plural = $params['plural']; + unset($params['plural']); + + // set count + if (isset($params['count'])) { + $count = $params['count']; + unset($params['count']); + } + } + + // use plural if required parameters are set + if (isset($count) && isset($plural)) { + $text = ngettext($text, $plural, $count); + } else { // use normal + $text = gettext($text); + } + + // run strarg if there are parameters + if (count($params)) { + $text = smarty_gettext_strarg($text, $params); + } + + if (!isset($escape) || $escape == 'html') { // html escape, default + $text = nl2br(htmlspecialchars($text)); + } elseif (isset($escape)) { + switch ($escape) { + case 'javascript': + case 'js': + // javascript escape + $text = str_replace('\'', '\\\'', stripslashes($text)); + break; + case 'url': + // url escape + $text = urlencode($text); + break; + } + } + + return $text; +} + +?> diff --git a/includes/smarty/tsmarty2c.php b/includes/smarty/tsmarty2c.php new file mode 100755 index 00000000..6e0b9944 --- /dev/null +++ b/includes/smarty/tsmarty2c.php @@ -0,0 +1,124 @@ +#!/usr/bin/env php + <..> > smarty.c + * + * If a parameter is a directory, the template files within will be parsed. + * + * @package smarty-gettext + * @version $Id: tsmarty2c.php,v 1.3 2005/07/27 17:59:39 sagi Exp $ + * @link http://smarty-gettext.sf.net/ + * @author Sagi Bashari + * @copyright 2004-2005 Sagi Bashari + */ + +// smarty open tag +$ldq = preg_quote('{'); + +// smarty close tag +$rdq = preg_quote('}'); + +// smarty command +$cmd = preg_quote('t'); + +// extensions of smarty files, used when going through a directory +$extensions = array('tpl'); + +// "fix" string - strip slashes, escape and convert new lines to \n +function fs($str) +{ + $str = stripslashes($str); + $str = str_replace('"', '\"', $str); + $str = str_replace("\n", '\n', $str); + return $str; +} + +// rips gettext strings from $file and prints them in C format +function do_file($file) +{ + $content = @file_get_contents($file); + + if (empty($content)) { + return; + } + + global $ldq, $rdq, $cmd; + + preg_match_all( + "/{$ldq}\s*({$cmd})\s*([^{$rdq}]*){$rdq}([^{$ldq}]*){$ldq}\/\\1{$rdq}/", + $content, + $matches + ); + + for ($i=0; $i < count($matches[0]); $i++) { + // TODO: add line number + echo "/* $file */\n"; // credit: Mike van Lammeren 2005-02-14 + + if (preg_match('/plural\s*=\s*["\']?\s*(.[^\"\']*)\s*["\']?/', $matches[2][$i], $match)) { + echo 'ngettext("'.fs($matches[3][$i]).'","'.fs($match[1]).'",x);'."\n"; + } else { + echo 'gettext("'.fs($matches[3][$i]).'");'."\n"; + } + + echo "\n"; + } +} + +// go through a directory +function do_dir($dir) +{ + $d = dir($dir); + + while (false !== ($entry = $d->read())) { + if ($entry == '.' || $entry == '..') { + continue; + } + + $entry = $dir.'/'.$entry; + + if (is_dir($entry)) { // if a directory, go through it + do_dir($entry); + } else { // if file, parse only if extension is matched + $pi = pathinfo($entry); + + if (isset($pi['extension']) && in_array($pi['extension'], $GLOBALS['extensions'])) { + do_file($entry); + } + } + } + + $d->close(); +} + +for ($ac=1; $ac < $_SERVER['argc']; $ac++) { + if (is_dir($_SERVER['argv'][$ac])) { // go through directory + do_dir($_SERVER['argv'][$ac]); + } else { // do file + do_file($_SERVER['argv'][$ac]); + } +} + +?>