// require_once PHPTAL_DIR.'PHPTAL/TranslationService.php'; /** * PHPTAL_TranslationService gettext implementation. * * Because gettext is the most common translation library in use, this * implementation is shipped with the PHPTAL library. * * Please refer to the PHPTAL documentation for usage examples. * * @package phptal * @author Laurent Bedubourg */ class PHPTAL_GetTextTranslator implements PHPTAL_TranslationService { public function __construct() { if (!function_exists('gettext')) throw new PHPTAL_Exception("Gettext not installed"); $this->useDomain("messages"); // PHP bug #21965 } private $_vars = array(); private $_currentDomain; private $_encoding = 'UTF-8'; private $_canonicalize = false; public function setEncoding($enc) { $this->_encoding = $enc; } /** * if true, all non-ASCII characters in keys will be converted to C form. This impacts performance. * by default keys will be passed to gettext unmodified. */ public function setCanonicalize($bool) { $this->_canonicalize = $bool; } public function setLanguage() { $langs = func_get_args(); foreach ($langs as $langCode){ putenv("LANG=$langCode"); putenv("LC_ALL=$langCode"); putenv("LANGUAGE=$langCode"); if (setlocale(LC_ALL, $langCode)) { return; } } $err = sprintf('Language(s) code(s) "%s" not supported by your system', join(',', $langs)); throw new PHPTAL_Exception($err); } /** * encoding must be set before calling addDomain */ public function addDomain($domain, $path='./locale/') { bindtextdomain($domain, $path); if ($this->_encoding){ bind_textdomain_codeset($domain, $this->_encoding); } $this->useDomain($domain); } public function useDomain($domain) { $old = $this->_currentDomain; $this->_currentDomain = $domain; textdomain($domain); return $old; } public function setVar($key, $value) { $this->_vars[$key] = $value; } public function translate($key, $htmlencode=true) { if ($this->_canonicalize) $key = self::_canonicalizeKey($key); $value = gettext($key); if ($htmlencode){ $value = @htmlspecialchars($value, ENT_QUOTES, $this->_encoding); // silence unsupported encoding error for ISO-8859-x, which doesn't matter. } while (preg_match('/\${(.*?)\}/sm', $value, $m)){ list($src,$var) = $m; if (!array_key_exists($var, $this->_vars)){ $err = sprintf('Interpolation error, var "%s" not set', $var); throw new PHPTAL_Exception($err); } $value = str_replace($src, $this->_vars[$var], $value); } return $value; } static function _canonicalizeKey($key_) { $result = ""; $key_ = trim($key_); $key_ = str_replace("\n", "", $key_); $key_ = str_replace("\r", "", $key_); for ($i = 0; $i 127){ $result .= 'C<'.$o.'>'; } else { $result .= $c; } } return $result; } }