diff --git a/lib/functions/smarty_plugins/gettext-prefilter.php b/lib/functions/smarty_plugins/gettext-prefilter.php new file mode 100644 index 00000000..dabd2c17 --- /dev/null +++ b/lib/functions/smarty_plugins/gettext-prefilter.php @@ -0,0 +1,117 @@ +registerFilter('pre', 'smarty_prefilter_t'); + function smarty_prefilter_t($tpl_source, &$smarty) { + /* find all {t} ... {/t} uses regex */ + $tpl_source = preg_replace_callback( + '|{t([^}]*)}(.*?){/t}|s', + 'smarty_helper_gettext_block', + $tpl_source + ); + + /* return our new tpl_source */ + return $tpl_source; + } + function smarty_helper_gettext_block($matches) + { + /* the actual text is the 2nd submatch */ + $text = stripslashes($matches[2]); + + /* if we do not have gettext, just return the text itself */ + if (!HAVE_GETTEXT) return $text; + + /* build our params via the 1st submatch */ + $params = Array(); + if(!isset($param_matches)) + { + $param_matches = ''; + } + $num = preg_match_all( + "|(\\w+)=([\"'])([^\\2]*)\\2|", + $param_matches, + $matches[2] + ); + if ($num) foreach($param_matches AS $param) $params[$param[1]] = $param[3]; + + // set domain if needed + if (isset($params['domain'])) + { + $domain = $params['domain']; + unset($params['domain']); + } + + // 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 = isset($domain) ? dngettext($domain, $text, $plural, $count) : ngettext($text, $plural, $count); + } + else + { // use normal + $text = isset($domain) ? dgettext($domain, $text) : gettext($text); + } + + // default to noescaping at all + if (isset($escape)) { + switch ($escape) { + case 'javascript': + case 'js': + // javascript escape + $text = str_replace('\'', '\\\'', stripslashes($text)); + break; + case 'url': + // url escape + $text = urlencode($text); + break; + case 'html': + $text = nl2br(htmlspecialchars($text)); + } + } + + return $text; + } +?> \ No newline at end of file