| 8 |
* @link http://phpgettext.web-sense.net |
* @link http://phpgettext.web-sense.net |
| 9 |
* |
* |
| 10 |
* |
* |
|
* class PHPGettext |
|
|
* php replacement for gettext where it is not available |
|
|
* |
|
|
* not all gettext functions are implemented yet |
|
|
* |
|
|
* |
|
|
* |
|
| 11 |
*/ |
*/ |
| 12 |
class PHPGettext |
class PHPGettext |
| 13 |
{ |
{ |
|
/** |
|
|
* Enter description here... |
|
|
* |
|
|
* @var bool |
|
|
*/ |
|
| 14 |
var $has_gettext; |
var $has_gettext; |
| 15 |
|
|
| 16 |
/** |
/** |
| 17 |
* The current locale. eg: en-GB |
* The current locale. eg: en-GB |
|
* |
|
|
* @var string |
|
| 18 |
*/ |
*/ |
| 19 |
var $locale; |
var $locale; |
| 20 |
|
|
| 21 |
/** |
/** |
| 22 |
* The current domain |
* The current domain |
|
* |
|
|
* @var string |
|
| 23 |
*/ |
*/ |
| 24 |
var $domain; |
var $domain; |
| 25 |
|
|
| 26 |
/** |
/** |
| 27 |
* The current character set |
* The current character set |
|
* |
|
|
* @var unknown_type |
|
| 28 |
*/ |
*/ |
| 29 |
var $charset; |
var $charset; |
| 30 |
|
|
| 31 |
/** |
/** |
| 32 |
* Container for the loaded domains |
* Container for the loaded domains |
|
* |
|
|
* @var array |
|
| 33 |
*/ |
*/ |
| 34 |
var $text_domains; |
var $text_domains = array(); |
| 35 |
|
|
| 36 |
/** |
/** |
| 37 |
* The asssociative array of messages for the current domain |
* The asssociative array of messages for the current domain |
|
* |
|
|
* @var unknown_type |
|
| 38 |
*/ |
*/ |
| 39 |
var $messages; |
var $messages = array(); |
| 40 |
|
|
| 41 |
/** |
/** |
| 42 |
* Enter description here... |
* The debugging flag |
|
* |
|
|
* @return PHPGettext |
|
| 43 |
*/ |
*/ |
| 44 |
|
var $debug; |
| 45 |
|
|
| 46 |
function PHPGettext() {} |
function PHPGettext() {} |
| 47 |
|
|
| 48 |
|
|
| 49 |
|
|
| 50 |
/** |
/** |
|
* Enter description here... |
|
| 51 |
* |
* |
| 52 |
* Lookup the locale from the environment variables. |
* Set and lookup the locale from the environment variables. |
| 53 |
* Priority order for gettext is: |
* Priority order for gettext is: |
| 54 |
* 1. LANGUAGE |
* 1. LANGUAGE |
| 55 |
* 2. LC_ALL |
* 2. LC_ALL |
| 58 |
* |
* |
| 59 |
* @return unknown |
* @return unknown |
| 60 |
*/ |
*/ |
| 61 |
|
|
| 62 |
|
function setlocale($lang) { |
| 63 |
|
#dump(setlocale(LC_ALL, 0)); |
| 64 |
|
$this->locale = $lang; |
| 65 |
|
putenv("LANGUAGE=$lang"); |
| 66 |
|
putenv("LC_ALL=$lang"); |
| 67 |
|
putenv("LC_MESSAGE=$lang"); |
| 68 |
|
putenv("LANG=$lang"); |
| 69 |
|
/*$_ENV['LANGUAGE'] = $lang; |
| 70 |
|
$_ENV['LC_ALL'] = $lang; |
| 71 |
|
$_ENV['LC_MESSAGE'] = $lang; |
| 72 |
|
$_ENV['LANG'] = $lang;*/ |
| 73 |
|
setlocale(LC_ALL, ''); |
| 74 |
|
return $this->locale; |
| 75 |
|
} |
| 76 |
|
|
| 77 |
function getlocale() { |
function getlocale() { |
| 78 |
if (empty($this->locale)) { |
if (empty($this->locale)) { |
| 79 |
$langs = array( getenv('LANGUAGE'), |
$langs = array( getenv('LANGUAGE'), |
| 81 |
getenv('LC_MESSAGE'), |
getenv('LC_MESSAGE'), |
| 82 |
getenv('LANG') |
getenv('LANG') |
| 83 |
); |
); |
| 84 |
foreach ($langs as $lang) { |
foreach ($langs as $lang) |
| 85 |
if ($lang){ |
if ($lang){ |
| 86 |
$this->locale = $lang; |
$this->locale = $lang; |
| 87 |
break; |
break; |
| 88 |
} |
} |
| 89 |
} |
} |
|
} |
|
| 90 |
return $this->locale; |
return $this->locale; |
| 91 |
} |
} |
| 92 |
|
|
| 93 |
/** |
/** |
| 94 |
* Set locale environment variables |
* debugging function |
| 95 |
|
* |
| 96 |
*/ |
*/ |
| 97 |
|
function output($message, $untranslated = false){ |
| 98 |
function setlocale($lang) { |
switch ($this->debug) |
| 99 |
$this->locale = $lang; |
{ |
| 100 |
putenv("LANGUAGE=$lang"); |
case 2: |
| 101 |
putenv("LC_ALL=$lang"); |
$trace = debug_backtrace(); |
| 102 |
putenv("LC_MESSAGE=$lang"); |
$html = '<span style="border-bottom: thin solid %s" title="%s(%d)">T_(%s)</span>'; |
| 103 |
putenv("LANG=$lang"); |
$str = sprintf($html, ($untranslated ? 'red' : 'green'), str_replace('\\', '/', $trace[2]['file']), $trace[2]['line'], $message); |
| 104 |
$ret = setlocale(LC_ALL, ''); |
break; |
| 105 |
return $ret; |
case 1: |
| 106 |
|
$str = sprintf('T_(%s)%s',$message, $untranslated ? ';' : ''); |
| 107 |
|
break; |
| 108 |
|
case 0: |
| 109 |
|
default: |
| 110 |
|
$str = $message; |
| 111 |
|
break; |
| 112 |
|
} |
| 113 |
|
return $str; |
| 114 |
} |
} |
| 115 |
|
|
| 116 |
/** |
/** |
| 118 |
* will also output the result if $output = true |
* will also output the result if $output = true |
| 119 |
*/ |
*/ |
| 120 |
function _($message, $output = false){ |
function _($message, $output = false){ |
| 121 |
$ret = $this->gettext($message); |
$return = $this->gettext($message); |
| 122 |
if ($output) { |
if ($output) { |
| 123 |
echo $ret; |
echo $return; |
| 124 |
return true; |
return true; |
| 125 |
} |
} |
| 126 |
return $ret; |
return $return; |
| 127 |
} |
} |
| 128 |
|
|
| 129 |
/** |
/** |
| 131 |
* returns translation if it exists or original message |
* returns translation if it exists or original message |
| 132 |
*/ |
*/ |
| 133 |
function gettext($message){ |
function gettext($message){ |
| 134 |
|
$translation = $message; |
| 135 |
if ($this->has_gettext){ |
if ($this->has_gettext){ |
| 136 |
return gettext($message); |
$translation = gettext($message); |
| 137 |
} |
} |
| 138 |
if (isset( $this->messages[$this->domain][$message])) { |
elseif (isset($this->messages[$this->domain][$message])) { |
| 139 |
$translation = $this->messages[$this->domain][$message]; |
$translation = $this->messages[$this->domain][$message]; |
| 140 |
|
} |
| 141 |
|
$untranslated = (strcmp($translation, $message) === 0) ? true : false; |
| 142 |
|
return $this->output($translation, $untranslated); |
| 143 |
|
} |
| 144 |
|
|
| 145 |
|
/** |
| 146 |
|
* Override the current domain |
| 147 |
|
* The dgettext() function allows you to override the current domain for a single message lookup. |
| 148 |
|
*/ |
| 149 |
|
function dgettext($domain, $message){ |
| 150 |
|
$translation = $message; |
| 151 |
|
if (array_key_exists($domain, $this->messages)){ |
| 152 |
|
$translation = $this->messages[$domain][$message]; |
| 153 |
|
} |
| 154 |
|
$untranslated = (strcmp($translation, $message) === 0) ? true : false; |
| 155 |
|
return $this->output($translation, $untranslated); |
| 156 |
|
} |
| 157 |
|
|
| 158 |
|
/** |
| 159 |
|
* Plural version of gettext * |
| 160 |
|
* not yet implemented |
| 161 |
|
*/ |
| 162 |
|
function ngettext($msg1, $msg2, $count){ |
| 163 |
|
$translation = $msg1; |
| 164 |
|
if ($this->has_gettext){ |
| 165 |
|
$translation = ngettext($msg1, $msg2, $count); |
| 166 |
|
} |
| 167 |
|
if (isset( $this->messages[$this->domain][$msg1])) { |
| 168 |
|
$translation = $this->messages[$this->domain][$msg1]; |
| 169 |
return $translation; |
return $translation; |
| 170 |
} |
} |
| 171 |
return $message; |
dump($this); |
| 172 |
|
$untranslated = (strcmp($translation, $message) === 0) ? true : false; |
| 173 |
|
return $this->output($translation, $untranslated); |
| 174 |
|
} |
| 175 |
|
/** |
| 176 |
|
* Plural version of dgettext . |
| 177 |
|
* not yet implemented |
| 178 |
|
*/ |
| 179 |
|
function dngettext($domain, $msg1, $msg2, $count){ |
| 180 |
|
return $msg1; |
| 181 |
} |
} |
| 182 |
|
|
| 183 |
/** |
/** |
| 189 |
if ($this->has_gettext){ |
if ($this->has_gettext){ |
| 190 |
bind_textdomain_codeset($domain, $charset); |
bind_textdomain_codeset($domain, $charset); |
| 191 |
} |
} |
| 192 |
return $this->test_domains[$domain]["charset"] = $charset; |
return $this->text_domains[$domain]["charset"] = $charset; |
| 193 |
} |
} |
| 194 |
|
|
| 195 |
/** |
/** |
| 203 |
} else { |
} else { |
| 204 |
$this->load($domain, $path); |
$this->load($domain, $path); |
| 205 |
} |
} |
| 206 |
return $this->test_domains[$domain]["path"] = $path; |
return $this->text_domains[$domain]["path"] = $path; |
| 207 |
} |
} |
| 208 |
|
|
| 209 |
/** |
/** |
| 219 |
return $this->domain = $domain; |
return $this->domain = $domain; |
| 220 |
} |
} |
| 221 |
|
|
|
/** |
|
|
* Override the current domain |
|
|
* The dgettext() function allows you to override the current domain for a single message lookup. |
|
|
*/ |
|
|
function dgettext($domain, $message){ |
|
|
if (array_key_exists($domain, $this->messages)) |
|
|
$translation = $this->messages[$domain][$message]; |
|
|
if ($translation) |
|
|
return $translation; |
|
|
|
|
|
return $message; |
|
|
} |
|
|
|
|
|
/** |
|
|
* Plural version of gettext * |
|
|
* not yet implemented |
|
|
*/ |
|
|
function ngettext($msg1, $msg2, $count){ |
|
|
if ($this->has_gettext){ |
|
|
return ngettext($msg1, $msg2, $count); |
|
|
} |
|
|
dump($this->domain); |
|
|
if (isset( $this->messages[$this->domain][$msg1])) { |
|
|
$translation = $this->messages[$this->domain][$msg1]; |
|
|
return $translation; |
|
|
} |
|
|
return $translation; |
|
|
} |
|
|
/** |
|
|
* Plural version of dgettext . |
|
|
* not yet implemented |
|
|
*/ |
|
|
function dngettext($domain, $msg1, $msg2, $count){ |
|
|
return $msg1; |
|
|
} |
|
|
|
|
|
|
|
| 222 |
function load($domain, $path) { |
function load($domain, $path) { |
| 223 |
require_once('phpgettext.catalog.php'); |
require_once('phpgettext.catalog.php'); |
| 224 |
$catalog = new PHPGettext_catalog($domain, $path); |
$catalog = new PHPGettext_catalog($domain, $path); |
| 301 |
* @return unknown |
* @return unknown |
| 302 |
*/ |
*/ |
| 303 |
function xgettext($args) { |
function xgettext($args) { |
| 304 |
$cmd = substr(strtoupper($_ENV['OS']), 0, 3) == 'WIN' ? "xgettext.exe " : "xgettext "; |
$cmd = substr(strtoupper(PHP_OS), 0, 3) == 'WIN' ? "xgettext.exe " : "xgettext "; |
| 305 |
return $this->execute($cmd.$args); |
return $this->execute($cmd.$args); |
| 306 |
} |
} |
| 307 |
|
|
| 314 |
*/ |
*/ |
| 315 |
function execute($cmd) { |
function execute($cmd) { |
| 316 |
|
|
| 317 |
if (substr(strtoupper(php_uname()), 0, 3) == 'WIN'){ |
$return = false; |
| 318 |
$cmd = "cmd /C $cmd"; |
if (substr(strtoupper(PHP_OS), 0, 3) == 'WIN'){ |
| 319 |
|
$outputfile = session_save_path() . DIRECTORY_SEPARATOR . md5(uniqid(rand(), true)) . ".txt"; |
| 320 |
$shell = new COM("WScript.Shell"); |
$shell = new COM("WScript.Shell"); |
| 321 |
$shell->Run($cmd, 0, false); |
$shell->Run("cmd /C $cmd > $outputfile", 0, true); |
| 322 |
|
$return = file_get_contents($outputfile); |
| 323 |
|
unlink($outputfile); |
| 324 |
} else { |
} else { |
| 325 |
$cmd = "$cmd"; |
$cmd = "$cmd"; |
| 326 |
$lastline = exec($cmd, $output, $ret); |
$lastline = exec($cmd, $output, $return); |
| 327 |
} |
} |
| 328 |
return $ret; |
return $return; |
| 329 |
} |
} |
| 330 |
} |
} |
| 331 |
|
|
| 332 |
|
|
| 333 |
|
|
| 334 |
|
|
| 335 |
/** |
/** |
| 336 |
* Enter description here... |
* Enter description here... |
| 337 |
* |
* |
| 342 |
if (is_null($gettext)) { |
if (is_null($gettext)) { |
| 343 |
require_once('phpgettext.class.php'); |
require_once('phpgettext.class.php'); |
| 344 |
$gettext = new PHPGettext(); |
$gettext = new PHPGettext(); |
| 345 |
|
$gettext->has_gettext = true; |
| 346 |
if (!function_exists("gettext") || !function_exists("_")) { |
if (!function_exists("gettext") || !function_exists("_")) { |
| 347 |
$gettext->has_gettext = false; |
$gettext->has_gettext = false; |
| 348 |
require_once('phpgettext.compat.php'); |
require_once('phpgettext.compat.php'); |
| 353 |
|
|
| 354 |
function T_($message) { |
function T_($message) { |
| 355 |
$gettext =& phpgettext(); |
$gettext =& phpgettext(); |
| 356 |
return $gettext->gettext($message); |
$trans = $gettext->gettext($message); |
| 357 |
|
return $trans; |
| 358 |
} |
} |
| 359 |
|
|
| 360 |
function Tn_($msg1, $msg2, $count) { |
function Tn_($msg1, $msg2, $count) { |