| 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 |
| 18 |
* |
*/ |
| 19 |
* @var string |
var $lang; |
| 20 |
|
/** |
| 21 |
|
* The current locale. eg: en-GB |
| 22 |
*/ |
*/ |
| 23 |
var $locale; |
var $locale; |
| 24 |
|
|
| 25 |
/** |
/** |
| 26 |
* The current domain |
* The current domain |
|
* |
|
|
* @var string |
|
| 27 |
*/ |
*/ |
| 28 |
var $domain; |
var $domain; |
| 29 |
|
|
| 30 |
/** |
/** |
| 31 |
* The current character set |
* The current character set |
|
* |
|
|
* @var unknown_type |
|
| 32 |
*/ |
*/ |
| 33 |
var $charset; |
var $charset; |
| 34 |
|
|
| 35 |
/** |
/** |
| 36 |
* Container for the loaded domains |
* Container for the loaded domains |
|
* |
|
|
* @var array |
|
| 37 |
*/ |
*/ |
| 38 |
var $text_domains; |
var $text_domains = array(); |
| 39 |
|
|
| 40 |
/** |
/** |
| 41 |
|
* The asssociative array of headers for the current domain |
| 42 |
|
*/ |
| 43 |
|
var $headers = array(); |
| 44 |
|
/** |
| 45 |
* The asssociative array of messages for the current domain |
* The asssociative array of messages for the current domain |
|
* |
|
|
* @var unknown_type |
|
| 46 |
*/ |
*/ |
| 47 |
var $messages; |
var $messages = array(); |
| 48 |
|
|
| 49 |
/** |
/** |
| 50 |
* Enter description here... |
* The debugging flag |
|
* |
|
|
* @return PHPGettext |
|
| 51 |
*/ |
*/ |
| 52 |
|
var $debug; |
| 53 |
|
|
| 54 |
function PHPGettext() {} |
function PHPGettext() {} |
| 55 |
|
|
| 56 |
|
|
| 57 |
|
|
| 58 |
/** |
/** |
|
* Enter description here... |
|
| 59 |
* |
* |
| 60 |
* Lookup the locale from the environment variables. |
* Set and lookup the locale from the environment variables. |
| 61 |
* Priority order for gettext is: |
* Priority order for gettext is: |
| 62 |
* 1. LANGUAGE |
* 1. LANGUAGE |
| 63 |
* 2. LC_ALL |
* 2. LC_ALL |
| 66 |
* |
* |
| 67 |
* @return unknown |
* @return unknown |
| 68 |
*/ |
*/ |
| 69 |
|
|
| 70 |
|
function setlocale($lang, $locale) { |
| 71 |
|
#dump(setlocale(LC_ALL, 0)); |
| 72 |
|
putenv("LANGUAGE=$lang"); |
| 73 |
|
putenv("LC_ALL=$lang"); |
| 74 |
|
putenv("LC_MESSAGE=$lang"); |
| 75 |
|
putenv("LANG=$lang"); |
| 76 |
|
/*$_ENV['LANGUAGE'] = $lang; |
| 77 |
|
$_ENV['LC_ALL'] = $lang; |
| 78 |
|
$_ENV['LC_MESSAGE'] = $lang; |
| 79 |
|
$_ENV['LANG'] = $lang;*/ |
| 80 |
|
$this->lang = $lang; |
| 81 |
|
$this->locale = setlocale(LC_ALL, $locale); |
| 82 |
|
setlocale(LC_ALL, $locale); |
| 83 |
|
} |
| 84 |
|
|
| 85 |
function getlocale() { |
function getlocale() { |
| 86 |
if (empty($this->locale)) { |
if (empty($this->locale)) { |
| 87 |
$langs = array( getenv('LANGUAGE'), |
$langs = array( getenv('LANGUAGE'), |
| 89 |
getenv('LC_MESSAGE'), |
getenv('LC_MESSAGE'), |
| 90 |
getenv('LANG') |
getenv('LANG') |
| 91 |
); |
); |
| 92 |
foreach ($langs as $lang) { |
foreach ($langs as $lang) |
| 93 |
if ($lang){ |
if ($lang){ |
| 94 |
$this->locale = $lang; |
$this->locale = $lang; |
| 95 |
break; |
break; |
| 96 |
} |
} |
| 97 |
} |
} |
|
} |
|
| 98 |
return $this->locale; |
return $this->locale; |
| 99 |
} |
} |
| 100 |
|
|
| 101 |
/** |
/** |
| 102 |
* Set locale environment variables |
* debugging function |
| 103 |
|
* |
| 104 |
*/ |
*/ |
| 105 |
|
function output($message, $untranslated = false){ |
| 106 |
function setlocale($lang) { |
switch ($this->debug) |
| 107 |
$this->locale = $lang; |
{ |
| 108 |
putenv("LANGUAGE=$lang"); |
case 2: |
| 109 |
putenv("LC_ALL=$lang"); |
$trace = debug_backtrace(); |
| 110 |
putenv("LC_MESSAGE=$lang"); |
$html = '<span style="border-bottom: thin solid %s" title="%s(%d)">T_(%s)</span>'; |
| 111 |
putenv("LANG=$lang"); |
$str = sprintf($html, ($untranslated ? 'red' : 'green'), str_replace('\\', '/', $trace[2]['file']), $trace[2]['line'], $message); |
| 112 |
$ret = setlocale(LC_ALL, ''); |
break; |
| 113 |
return $ret; |
case 1: |
| 114 |
|
$str = sprintf('%sT_(%s)',$untranslated ? '!' : '', $message); |
| 115 |
|
break; |
| 116 |
|
case 0: |
| 117 |
|
default: |
| 118 |
|
$str = $message; |
| 119 |
|
break; |
| 120 |
|
} |
| 121 |
|
return $str; |
| 122 |
} |
} |
| 123 |
|
|
| 124 |
/** |
/** |
| 126 |
* will also output the result if $output = true |
* will also output the result if $output = true |
| 127 |
*/ |
*/ |
| 128 |
function _($message, $output = false){ |
function _($message, $output = false){ |
| 129 |
$ret = $this->gettext($message); |
$return = $this->gettext($message); |
| 130 |
if ($output) { |
if ($output) { |
| 131 |
echo $ret; |
echo $return; |
| 132 |
return true; |
return true; |
| 133 |
} |
} |
| 134 |
return $ret; |
return $return; |
| 135 |
} |
} |
| 136 |
|
|
| 137 |
/** |
/** |
| 139 |
* returns translation if it exists or original message |
* returns translation if it exists or original message |
| 140 |
*/ |
*/ |
| 141 |
function gettext($message){ |
function gettext($message){ |
| 142 |
|
$translation = $message; |
| 143 |
if ($this->has_gettext){ |
if ($this->has_gettext){ |
| 144 |
return gettext($message); |
$translation = gettext($message); |
| 145 |
} |
} |
| 146 |
if (isset( $this->messages[$this->domain][$message])) { |
elseif (isset($this->messages[$this->domain][$message])) { |
| 147 |
$translation = $this->messages[$this->domain][$message]; |
$translation = $this->messages[$this->domain][$message]; |
|
return $translation; |
|
| 148 |
} |
} |
| 149 |
return $message; |
$untranslated = (strcmp($translation, $message) === 0) ? true : false; |
| 150 |
|
return $this->output($translation, $untranslated); |
| 151 |
|
} |
| 152 |
|
|
| 153 |
|
/** |
| 154 |
|
* Override the current domain |
| 155 |
|
* The dgettext() function allows you to override the current domain for a single message lookup. |
| 156 |
|
*/ |
| 157 |
|
function dgettext($domain, $message){ |
| 158 |
|
$translation = $message; |
| 159 |
|
if (array_key_exists($domain, $this->messages)){ |
| 160 |
|
if (isset($this->messages[$domain][$message])) |
| 161 |
|
$translation = $this->messages[$domain][$message]; |
| 162 |
|
} |
| 163 |
|
$untranslated = (strcmp($translation, $message) === 0) ? true : false; |
| 164 |
|
return $this->output($translation, $untranslated); |
| 165 |
|
} |
| 166 |
|
|
| 167 |
|
/** |
| 168 |
|
* Plural version of gettext |
| 169 |
|
*/ |
| 170 |
|
function ngettext($msgid, $msgid_plural, $count){ |
| 171 |
|
if ($this->has_gettext){ |
| 172 |
|
$translation = ngettext($msgid, $msgid_plural, $count); |
| 173 |
|
} |
| 174 |
|
$plural = $this->getplural($count, $this->domain); |
| 175 |
|
if (isset($this->messages[$this->domain][$msgid][$plural])) { |
| 176 |
|
$translation = $this->messages[$this->domain][$msgid][$plural]; |
| 177 |
|
} else { |
| 178 |
|
$original = array($msgid, $msgid_plural); |
| 179 |
|
$translation = $original[$plural]; |
| 180 |
|
} |
| 181 |
|
$untranslated = (strcmp($translation, $original[$plural]) === 0) ? true : false; |
| 182 |
|
return $this->output($translation, $untranslated); |
| 183 |
|
} |
| 184 |
|
/** |
| 185 |
|
* Plural version of dgettext |
| 186 |
|
*/ |
| 187 |
|
function dngettext($domain, $msgid, $msgid_plural, $count){ |
| 188 |
|
$original = array($msgid, $msgid_plural); |
| 189 |
|
$plural = $this->getplural($count, $domain); |
| 190 |
|
if ($this->has_gettext){ |
| 191 |
|
$translation = dngettext($domain, $msgid, $msgid_plural, $count); |
| 192 |
|
} else { |
| 193 |
|
if (isset($this->messages[$domain][$msgid][$plural])) { |
| 194 |
|
$translation = $this->messages[$domain][$msgid][$plural]; |
| 195 |
|
} else { |
| 196 |
|
$translation = $original[$plural]; |
| 197 |
|
} |
| 198 |
|
} |
| 199 |
|
$untranslated = (strcmp($translation, $original[$plural]) === 0) ? true : false; |
| 200 |
|
return $this->output($translation, $untranslated); |
| 201 |
} |
} |
| 202 |
|
|
| 203 |
/** |
/** |
| 209 |
if ($this->has_gettext){ |
if ($this->has_gettext){ |
| 210 |
bind_textdomain_codeset($domain, $charset); |
bind_textdomain_codeset($domain, $charset); |
| 211 |
} |
} |
| 212 |
return $this->test_domains[$domain]["charset"] = $charset; |
return $this->text_domains[$domain]["charset"] = $charset; |
| 213 |
} |
} |
| 214 |
|
|
| 215 |
/** |
/** |
| 223 |
} else { |
} else { |
| 224 |
$this->load($domain, $path); |
$this->load($domain, $path); |
| 225 |
} |
} |
| 226 |
return $this->test_domains[$domain]["path"] = $path; |
return $this->text_domains[$domain]["path"] = $path; |
| 227 |
} |
} |
| 228 |
|
|
| 229 |
/** |
/** |
| 231 |
*/ |
*/ |
| 232 |
function textdomain($domain = null){ |
function textdomain($domain = null){ |
| 233 |
if ($this->has_gettext){ |
if ($this->has_gettext){ |
| 234 |
textdomain($domain); |
$domain = textdomain($domain); |
|
} |
|
|
if (is_null($domain)) |
|
|
return $this->domain; |
|
|
else |
|
|
return $this->domain = $domain; |
|
|
} |
|
|
|
|
|
/** |
|
|
* 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; |
|
| 235 |
} |
} |
| 236 |
return $translation; |
elseif (!is_null($domain)) { |
| 237 |
|
$this->domain = $domain; |
| 238 |
|
$this->load($domain, $this->text_domains[$this->domain]['path']); |
| 239 |
} |
} |
| 240 |
/** |
return $this->domain; |
|
* Plural version of dgettext . |
|
|
* not yet implemented |
|
|
*/ |
|
|
function dngettext($domain, $msg1, $msg2, $count){ |
|
|
return $msg1; |
|
|
} |
|
|
|
|
|
|
|
|
function load($domain, $path) { |
|
|
require_once('phpgettext.catalog.php'); |
|
|
$catalog = new PHPGettext_catalog($domain, $path); |
|
|
$catalog->setproperty('mode', _MODE_MO_); |
|
|
$catalog->setproperty('lang', $this->getlocale()); |
|
|
$catalog->load(); |
|
|
foreach ($catalog->strings as $string) |
|
|
$this->messages[$domain][$string->msgid] = $string->msgstr; |
|
| 241 |
} |
} |
| 242 |
|
|
| 243 |
/** |
/** |
| 244 |
* Overrides the domain for a single lookup string |
* Overrides the domain for a single lookup |
| 245 |
* This function allows you to override the current domain for a single message lookup. |
* This function allows you to override the current domain for a single message lookup. |
| 246 |
* It also allows you to specify a category. |
* It also allows you to specify a category. |
| 247 |
* Categories are folders within the languages directory . |
* Categories are folders within the languages directory . |
| 263 |
} |
} |
| 264 |
|
|
| 265 |
/** |
/** |
| 266 |
* Plural version of dcgettext |
* dcngettext -- Plural version of dcgettext |
| 267 |
* not yet implemented |
* not yet implemented |
| 268 |
*/ |
*/ |
| 269 |
function dcngettext($domain, $msg1, $msg2, $count, $category){ |
function dcngettext($domain, $msg1, $msg2, $count, $category){ |
| 271 |
} |
} |
| 272 |
|
|
| 273 |
|
|
| 274 |
|
/** |
| 275 |
|
* Plural-Forms: nplurals=2; plural=n == 1 ? 0 : 1; |
| 276 |
|
* |
| 277 |
|
* nplurals - total number of plurals |
| 278 |
|
* plural - the plural index |
| 279 |
|
* |
| 280 |
|
* Plural-Forms: nplurals=1; plural=0; |
| 281 |
|
* 1 form only |
| 282 |
|
* |
| 283 |
|
* Plural-Forms: nplurals=2; plural=n == 1 ? 0 : 1; |
| 284 |
|
* Plural-Forms: nplurals=2; plural=n != 1; |
| 285 |
|
* 2 forms, singular used for one only |
| 286 |
|
* |
| 287 |
|
* Plural-Forms: nplurals=2; plural=n>1; |
| 288 |
|
* 2 forms, singular used for zero and one |
| 289 |
|
* |
| 290 |
|
* Plural-Forms: nplurals=3; plural=n%10==1 && n%100!=11 ? 0 : n != 0 ? 1 : 2; |
| 291 |
|
* 3 forms, special case for zero |
| 292 |
|
* |
| 293 |
|
* Plural-Forms: nplurals=3; plural=n==1 ? 0 : n==2 ? 1 : 2; |
| 294 |
|
* 3 forms, special cases for one and two |
| 295 |
|
* |
| 296 |
|
* Plural-Forms: nplurals=4; plural=n%100==1 ? 0 : n%100==2 ? 1 : n%100==3 || n%100==4 ? 2 : 3; |
| 297 |
|
* 4 forms, special case for one and all numbers ending in 02, 03, or 04 |
| 298 |
|
*/ |
| 299 |
|
function getplural($count, $domain) { |
| 300 |
|
if (isset($this->headers[$domain]['Plural-Forms'])) { |
| 301 |
|
$plural_forms = $this->headers[$domain]['Plural-Forms']; |
| 302 |
|
preg_match('/nplurals[\s]*[=]{1}[\s]*([\d]+);[\s]*plural[\s]*[=]{1}[\s]*(.*);$/', $plural_forms, $matches); |
| 303 |
|
$nplurals = $matches[1]; |
| 304 |
|
$plural_exp = $matches[2]; |
| 305 |
|
if ($nplurals > 1 && strpos($plural_exp, ':') === false) { |
| 306 |
|
$plural = 'return ('.preg_replace('/n/', $count, $plural_exp).') ? 1 : 0;'; |
| 307 |
|
} else { |
| 308 |
|
$plural = 'return '.preg_replace('/n/', $count, $plural_exp); |
| 309 |
|
} |
| 310 |
|
} else { |
| 311 |
|
$plural = 'return '.preg_replace('/n/', $count, 'n != 1 ? 1 : 0;'); |
| 312 |
|
} |
| 313 |
|
return eval($plural); |
| 314 |
|
} |
| 315 |
|
|
| 316 |
|
function load($domain, $path) { |
| 317 |
|
require_once('phpgettext.catalog.php'); |
| 318 |
|
$catalog = new PHPGettext_catalog($domain, $path); |
| 319 |
|
$catalog->setproperty('mode', _MODE_MO_); |
| 320 |
|
$catalog->setproperty('lang', $this->lang); |
| 321 |
|
$catalog->load(); |
| 322 |
|
$this->headers[$domain] = $catalog->headers; |
| 323 |
|
foreach ($catalog->strings as $string) |
| 324 |
|
$this->messages[$domain][$string->msgid] = $string->msgstr; |
| 325 |
|
} |
| 326 |
} |
} |
| 327 |
|
|
| 328 |
class PHPGettextAdmin |
class PHPGettextAdmin |
| 348 |
* @param unknown_type $domain |
* @param unknown_type $domain |
| 349 |
* @param unknown_type $langdir |
* @param unknown_type $langdir |
| 350 |
*/ |
*/ |
| 351 |
function msgmerge ($outdated, $recent, $langdir) { |
function msgmerge ($args) { |
|
#msgmerge $langdir/$old.po $new.po |
|
| 352 |
$cmd = "msgmerge"; |
$cmd = "msgmerge"; |
| 353 |
$arg = "$langdir/$outdated $recent"; |
return $this->execute($cmd.$args); |
|
//$exec = 'msgmerge --help'; |
|
|
return $this->execute($cmd, $arg); |
|
| 354 |
} |
} |
| 355 |
|
|
| 356 |
/** |
/** |
| 361 |
* @return unknown |
* @return unknown |
| 362 |
*/ |
*/ |
| 363 |
function xgettext($args) { |
function xgettext($args) { |
| 364 |
$cmd = substr(strtoupper($_ENV['OS']), 0, 3) == 'WIN' ? "xgettext.exe " : "xgettext "; |
$cmd = "xgettext "; |
| 365 |
return $this->execute($cmd.$args); |
return $this->execute($cmd.$args); |
| 366 |
} |
} |
| 367 |
|
|
| 374 |
*/ |
*/ |
| 375 |
function execute($cmd) { |
function execute($cmd) { |
| 376 |
|
|
| 377 |
if (substr(strtoupper(php_uname()), 0, 3) == 'WIN'){ |
$return = false; |
| 378 |
$cmd = "cmd /C $cmd"; |
if (substr(strtoupper(PHP_OS), 0, 3) == 'WIN'){ |
| 379 |
$shell = new COM("WScript.Shell"); |
$outputfile = session_save_path() . DIRECTORY_SEPARATOR . md5(uniqid(rand(), true)) . ".txt"; |
| 380 |
$shell->Run($cmd, 0, false); |
$fp = fopen("execute.bat", 'w+'); |
| 381 |
|
fwrite($fp, "@echo on\r\n $cmd"); |
| 382 |
|
fclose($fp); |
| 383 |
|
exec("execute.bat", $output, $return); |
| 384 |
|
unlink("execute.bat"); |
| 385 |
} else { |
} else { |
| 386 |
$cmd = "$cmd"; |
$cmd = "$cmd"; |
| 387 |
$lastline = exec($cmd, $output, $ret); |
$lastline = exec($cmd, $output, $return); |
| 388 |
} |
} |
| 389 |
return $ret; |
return $return; |
| 390 |
} |
} |
| 391 |
} |
} |
| 392 |
|
|
|
|
|
| 393 |
/** |
/** |
| 394 |
* Enter description here... |
* Enter description here... |
| 395 |
* |
* |
| 400 |
if (is_null($gettext)) { |
if (is_null($gettext)) { |
| 401 |
require_once('phpgettext.class.php'); |
require_once('phpgettext.class.php'); |
| 402 |
$gettext = new PHPGettext(); |
$gettext = new PHPGettext(); |
| 403 |
|
$gettext->has_gettext = true; |
| 404 |
if (!function_exists("gettext") || !function_exists("_")) { |
if (!function_exists("gettext") || !function_exists("_")) { |
| 405 |
$gettext->has_gettext = false; |
$gettext->has_gettext = false; |
| 406 |
require_once('phpgettext.compat.php'); |
require_once('phpgettext.compat.php'); |
| 408 |
} |
} |
| 409 |
return $gettext; |
return $gettext; |
| 410 |
} |
} |
|
|
|
| 411 |
function T_($message) { |
function T_($message) { |
| 412 |
$gettext =& phpgettext(); |
$gettext =& phpgettext(); |
| 413 |
return $gettext->gettext($message); |
$trans = $gettext->gettext($message); |
| 414 |
|
return $trans; |
| 415 |
} |
} |
|
|
|
| 416 |
function Tn_($msg1, $msg2, $count) { |
function Tn_($msg1, $msg2, $count) { |
| 417 |
$gettext =& phpgettext(); |
$gettext =& phpgettext(); |
| 418 |
return $gettext->ngettext($msg1, $msg2, $count); |
return $gettext->ngettext($msg1, $msg2, $count); |
| 425 |
$gettext =& phpgettext(); |
$gettext =& phpgettext(); |
| 426 |
return $gettext->dngettext($domain, $msg1, $msg2, $count); |
return $gettext->dngettext($domain, $msg1, $msg2, $count); |
| 427 |
} |
} |
|
|
|
| 428 |
?> |
?> |