From 61de82ed892fc532ee1940ee763efe8f28298347 Mon Sep 17 00:00:00 2001 From: cajus Date: Fri, 9 Jun 2006 05:43:01 +0000 Subject: [PATCH] Updated smarty block.t git-svn-id: https://oss.gonicus.de/repositories/gosa/trunk@3729 594d385d-05f5-0310-b6e9-bd551577e9d8 --- include/smarty/plugins/block.t.php | 246 +++++++++++++---------------- 1 file changed, 113 insertions(+), 133 deletions(-) diff --git a/include/smarty/plugins/block.t.php b/include/smarty/plugins/block.t.php index dc5cf4298..ca7382aeb 100644 --- a/include/smarty/plugins/block.t.php +++ b/include/smarty/plugins/block.t.php @@ -1,146 +1,126 @@ - - * @param string $language override global t_language variable - * @version: 1.3 + * @package smarty-gettext + * @version $Id: block.t.php,v 1.1 2005/07/27 17:58:56 sagi Exp $ + * @link http://smarty-gettext.sourceforge.net/ + * @author Sagi Bashari + * @copyright 2004-2005 Sagi Bashari */ -function smarty_block_t($params, $content, &$this) + +/** + * 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) { - if (!$content) return $content; - // Setup local development variables. - // If $development = false then use_babelfish is ignored. - $development = false; - // If use_babelfish is false, placeholders will be put in .po files - // but no translation will be attempted using babelfish. - $use_babelfish = false; - - global $t_language, $t_language_from, $t_msgfmt_location, $t_gettext_message_dir; - - // Check for new language - if (isset($params['language']) && $params['language']) { - $t_language = $params['language']; - putenv ("LC_ALL={$GLOBALS[t_language]}"); - } - - // If under development, check for necessary files - if ($development) { - // Translate string once for every language supported - foreach ($GLOBALS['t_language_all'] as $t_language) { - // Check for language directory; create if necessary - if (!is_dir($t_gettext_message_dir . $t_language)) { - mkdir ($t_gettext_message_dir . $t_language); - mkdir ("$t_gettext_message_dir$t_language/LC_MESSAGES"); - } - // Check for .po file - $po = "$t_gettext_message_dir$t_language/LC_MESSAGES/messages.po"; - if (!file_exists($po)) _create_po_file($po); - - // Load the .po file and check for the current translation string - $text = file($po); - $cst_content = addcslashes(trim($content), '\\"'); - if (!_in_array_trim('msgid "' . $cst_content . '"', $text)) { - // String is not in translation file; add it - // Get translated string from babelfish if set to true - if ($use_babelfish) { - $translated = ''; - $url ="http://babelfish.altavista.com/babelfish/tr?lp={$t_language_from}_{$t_language}&intl=1&tt=urltext&doit=done&urltext=" . urlencode(htmlspecialchars($content, ENT_NOQUOTES, 'UTF-8')); - $fp = fopen($url, 'r'); - // Find the translated string - //foreach ($babel as $line) { - while ($line = fgets($fp)) { - if (strstr($line, "lang=$t_language")) { - // multibytes do not work yet with - // html_entity_decode but that is ok, - // since altavista does not seem to send entities. - $translated = html_entity_decode(trim(addcslashes(strip_tags($line), '\\"')), ENT_NOQUOTES); - - break; - } - } - fclose($fp); - unset($url); - } + $tr = array(); + $p = 0; - // Add string to .po - // mark translation as babelfish, so we know - // that we need to look after it. - if ($use_babelfish) { - error_log("#: babelfish\n", 3, $po); - } - error_log("#: " . $this->_plugins['block']['t'][1] . "\nmsgid \"" . $cst_content . "\"\nmsgstr \"$translated\"\n\n", 3, $po); - - // Recompile translation file - `$t_msgfmt_location -o $t_gettext_message_dir$t_language/LC_MESSAGES/messages.mo $po`; - } - } - } - // Do the actual translation through gettext. - bindtextdomain ('messages', $t_gettext_message_dir); - return gettext(trim($content)); + 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); } -// Helper functions -function _in_array_trim($needle, $haystack) { - $needle = trim($needle); - foreach ($haystack as $ck) { - if ($needle == trim($ck)) return true; - } - return false; -} +/** + * 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); + } -function _create_po_file($po) { - touch ($po); - // put header info for .po file - $year = date('Y'); - $date = date('r'); - $body = << -- 2.30.2