*/ /** * patTemplate gettext / patI18n wrapper * * Utilize patI18n to translate template snippets * * @version 0.1.0 * @package Wombat * @subpackage patTemplate */ class patTemplate_Function_Gettext extends patTemplate_Function { /** * name of the function * @access private * @var string */ public $_name = 'Gettext'; /** * call the function * * Use patI18n to translate contetn. * * Attributes: * - domain: text domain * * @param array parameters of the function (= attributes of the tag) * @param string content of the tag * @return string content to insert into the template */ public function call($params, $content) { // nothing to do if (empty($content)) { return ''; } $params = array_merge(array( 'domain' => '', 'wxml' => 'auto', 'allowedtags' => '', 'bracemode' => 'keep' ), $params); $content = $this->filterBraces($params['bracemode'], $content); // translate with domain if (!empty($params['domain'])) { $msg = patI18n::dgettext($params['domain'], $content); } else { // use default domain $msg = patI18n::gettext($content); } $this->runWXml($msg, $params); if (empty($params['allowedtags'])) { return $this->filterBraces($params['bracemode'], $msg, false); } $allowed = explode(',', $params['allowedtags']); $allowed = '<' . implode('><', $allowed) . '>'; $msg = strip_tags($msg, $allowed); return $this->filterBraces($params['bracemode'], $msg, false); } /** * Convert Braces * * @param string brache mode (keep, HTML or TeX) * @param string content to filter * @param bool filter direction */ private function filterBraces($mode, $content, $dir = true) { $mode = strtolower($mode); if (in_array($mode, array('html', 'keep'))) { return $content; } $in = array('<{', '}>'); $out = array('{', '}'); if ($dir) { return str_replace($in, $out, $content); } return str_replace($out, $in, $content); } /** * Run WXML markup handler * * Check parameter "wxml" * * @param string $msg * @param array $params */ public function runWXml(&$msg, $params) { if (empty($params['wxml']) || '0' == $params['wxml']) { return; } // check for HTML tags if ('auto' == $params['wxml']) { $wLen = strlen($msg); $oLen = strlen(strip_tags($msg)); if ($wLen == $oLen) { return; } } $scan = WBClass::create( 'WBMarkup_Scanner' ); /* @var $scan WBMarkup_Scanner */ $hdl = WBClass::create( 'WBMarkup_Handler_Xml2Html' ); /* @var $hdl WBMarkup_Handler_Xml2Html */ if (isset($params['tmpldir']) && !empty($params['tmpldir'])) { $hdl->setTmplDir($params['tmpldir']); } $scan->setHandler($hdl); $scan->scan($msg); $msg = $hdl->getParsedContent(); } }