Annotation of /mambo/branches/4.6/includes/phpgettext/phpgettext.class.php
Parent Directory
|
Revision Log
Revision 266 - (view) (download)
| 1 : | csouza | 39 | <?php |
| 2 : | /** | ||
| 3 : | * @version 0.9 | ||
| 4 : | * @author Carlos Souza | ||
| 5 : | * @copyright Copyright (c) 2005 Carlos Souza <csouza@web-sense.net> | ||
| 6 : | * @package PHPGettext | ||
| 7 : | * @license MIT License (http://www.opensource.org/licenses/mit-license.php) | ||
| 8 : | * @link http://phpgettext.web-sense.net | ||
| 9 : | * | ||
| 10 : | * | ||
| 11 : | */ | ||
| 12 : | class PHPGettext | ||
| 13 : | { | ||
| 14 : | var $has_gettext; | ||
| 15 : | csouza | 149 | |
| 16 : | csouza | 39 | /** |
| 17 : | * The current locale. eg: en-GB | ||
| 18 : | */ | ||
| 19 : | csouza | 266 | var $lang; |
| 20 : | /** | ||
| 21 : | * The current locale. eg: en-GB | ||
| 22 : | */ | ||
| 23 : | csouza | 39 | var $locale; |
| 24 : | csouza | 149 | |
| 25 : | csouza | 39 | /** |
| 26 : | * The current domain | ||
| 27 : | */ | ||
| 28 : | var $domain; | ||
| 29 : | csouza | 149 | |
| 30 : | csouza | 39 | /** |
| 31 : | * The current character set | ||
| 32 : | */ | ||
| 33 : | var $charset; | ||
| 34 : | csouza | 149 | |
| 35 : | csouza | 39 | /** |
| 36 : | * Container for the loaded domains | ||
| 37 : | */ | ||
| 38 : | csouza | 183 | var $text_domains = array(); |
| 39 : | csouza | 149 | |
| 40 : | csouza | 39 | /** |
| 41 : | csouza | 266 | * The asssociative array of headers for the current domain |
| 42 : | */ | ||
| 43 : | var $headers = array(); | ||
| 44 : | /** | ||
| 45 : | csouza | 39 | * The asssociative array of messages for the current domain |
| 46 : | */ | ||
| 47 : | csouza | 183 | var $messages = array(); |
| 48 : | csouza | 39 | |
| 49 : | /** | ||
| 50 : | csouza | 153 | * The debugging flag |
| 51 : | */ | ||
| 52 : | var $debug; | ||
| 53 : | |||
| 54 : | csouza | 39 | function PHPGettext() {} |
| 55 : | |||
| 56 : | |||
| 57 : | csouza | 149 | |
| 58 : | csouza | 39 | /** |
| 59 : | * | ||
| 60 : | csouza | 179 | * Set and lookup the locale from the environment variables. |
| 61 : | csouza | 39 | * Priority order for gettext is: |
| 62 : | * 1. LANGUAGE | ||
| 63 : | * 2. LC_ALL | ||
| 64 : | * 3. LC_MESSAGE | ||
| 65 : | * 4. LANG | ||
| 66 : | * | ||
| 67 : | * @return unknown | ||
| 68 : | */ | ||
| 69 : | csouza | 183 | |
| 70 : | csouza | 266 | function setlocale($lang, $locale) { |
| 71 : | csouza | 179 | #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 : | csouza | 266 | $this->lang = $lang; |
| 81 : | $this->locale = setlocale(LC_ALL, $locale); | ||
| 82 : | setlocale(LC_ALL, $locale); | ||
| 83 : | csouza | 179 | } |
| 84 : | csouza | 183 | |
| 85 : | csouza | 39 | function getlocale() { |
| 86 : | if (empty($this->locale)) { | ||
| 87 : | $langs = array( getenv('LANGUAGE'), | ||
| 88 : | csouza | 149 | getenv('LC_ALL'), |
| 89 : | getenv('LC_MESSAGE'), | ||
| 90 : | getenv('LANG') | ||
| 91 : | ); | ||
| 92 : | csouza | 179 | foreach ($langs as $lang) |
| 93 : | csouza | 183 | if ($lang){ |
| 94 : | $this->locale = $lang; | ||
| 95 : | break; | ||
| 96 : | } | ||
| 97 : | csouza | 39 | } |
| 98 : | return $this->locale; | ||
| 99 : | } | ||
| 100 : | |||
| 101 : | /** | ||
| 102 : | csouza | 153 | * debugging function |
| 103 : | * | ||
| 104 : | */ | ||
| 105 : | csouza | 183 | function output($message, $untranslated = false){ |
| 106 : | switch ($this->debug) | ||
| 107 : | { | ||
| 108 : | case 2: | ||
| 109 : | $trace = debug_backtrace(); | ||
| 110 : | csouza | 154 | $html = '<span style="border-bottom: thin solid %s" title="%s(%d)">T_(%s)</span>'; |
| 111 : | $str = sprintf($html, ($untranslated ? 'red' : 'green'), str_replace('\\', '/', $trace[2]['file']), $trace[2]['line'], $message); | ||
| 112 : | csouza | 183 | break; |
| 113 : | case 1: | ||
| 114 : | csouza | 266 | $str = sprintf('%sT_(%s)',$untranslated ? '!' : '', $message); |
| 115 : | csouza | 183 | break; |
| 116 : | case 0: | ||
| 117 : | default: | ||
| 118 : | csouza | 266 | $str = $message; |
| 119 : | csouza | 183 | break; |
| 120 : | csouza | 154 | } |
| 121 : | return $str; | ||
| 122 : | csouza | 153 | } |
| 123 : | |||
| 124 : | /** | ||
| 125 : | csouza | 39 | * Alias for gettext |
| 126 : | * will also output the result if $output = true | ||
| 127 : | */ | ||
| 128 : | csouza | 149 | function _($message, $output = false){ |
| 129 : | csouza | 153 | $return = $this->gettext($message); |
| 130 : | csouza | 39 | if ($output) { |
| 131 : | csouza | 153 | echo $return; |
| 132 : | csouza | 39 | return true; |
| 133 : | } | ||
| 134 : | csouza | 153 | return $return; |
| 135 : | csouza | 39 | } |
| 136 : | |||
| 137 : | /** | ||
| 138 : | * Lookup a message in the current domain | ||
| 139 : | * returns translation if it exists or original message | ||
| 140 : | */ | ||
| 141 : | function gettext($message){ | ||
| 142 : | csouza | 153 | $translation = $message; |
| 143 : | csouza | 39 | if ($this->has_gettext){ |
| 144 : | csouza | 153 | $translation = gettext($message); |
| 145 : | csouza | 39 | } |
| 146 : | csouza | 153 | elseif (isset($this->messages[$this->domain][$message])) { |
| 147 : | csouza | 39 | $translation = $this->messages[$this->domain][$message]; |
| 148 : | csouza | 149 | } |
| 149 : | csouza | 154 | $untranslated = (strcmp($translation, $message) === 0) ? true : false; |
| 150 : | csouza | 183 | return $this->output($translation, $untranslated); |
| 151 : | csouza | 39 | } |
| 152 : | |||
| 153 : | /** | ||
| 154 : | csouza | 183 | * 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 : | csouza | 266 | if (isset($this->messages[$domain][$message])) |
| 161 : | $translation = $this->messages[$domain][$message]; | ||
| 162 : | csouza | 183 | } |
| 163 : | $untranslated = (strcmp($translation, $message) === 0) ? true : false; | ||
| 164 : | return $this->output($translation, $untranslated); | ||
| 165 : | } | ||
| 166 : | |||
| 167 : | /** | ||
| 168 : | csouza | 266 | * Plural version of gettext |
| 169 : | csouza | 183 | */ |
| 170 : | csouza | 266 | function ngettext($msgid, $msgid_plural, $count){ |
| 171 : | csouza | 183 | if ($this->has_gettext){ |
| 172 : | csouza | 266 | $translation = ngettext($msgid, $msgid_plural, $count); |
| 173 : | csouza | 183 | } |
| 174 : | csouza | 266 | $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 : | csouza | 183 | } |
| 181 : | csouza | 266 | $untranslated = (strcmp($translation, $original[$plural]) === 0) ? true : false; |
| 182 : | csouza | 183 | return $this->output($translation, $untranslated); |
| 183 : | } | ||
| 184 : | /** | ||
| 185 : | csouza | 266 | * Plural version of dgettext |
| 186 : | csouza | 183 | */ |
| 187 : | csouza | 266 | 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 : | csouza | 183 | } |
| 202 : | |||
| 203 : | /** | ||
| 204 : | csouza | 39 | * Specify the character encoding in which the messages |
| 205 : | * from the DOMAIN message catalog will be returned | ||
| 206 : | * | ||
| 207 : | */ | ||
| 208 : | function bind_textdomain_codeset($domain, $charset){ | ||
| 209 : | if ($this->has_gettext){ | ||
| 210 : | bind_textdomain_codeset($domain, $charset); | ||
| 211 : | } | ||
| 212 : | csouza | 175 | return $this->text_domains[$domain]["charset"] = $charset; |
| 213 : | csouza | 39 | } |
| 214 : | |||
| 215 : | /** | ||
| 216 : | * Sets the path for a domain | ||
| 217 : | * if gettext is unavailable, translation files will be loaded here | ||
| 218 : | * | ||
| 219 : | */ | ||
| 220 : | function bindtextdomain($domain, $path){ | ||
| 221 : | if ($this->has_gettext){ | ||
| 222 : | bindtextdomain($domain, $path); | ||
| 223 : | } else { | ||
| 224 : | $this->load($domain, $path); | ||
| 225 : | } | ||
| 226 : | csouza | 175 | return $this->text_domains[$domain]["path"] = $path; |
| 227 : | csouza | 39 | } |
| 228 : | |||
| 229 : | /** | ||
| 230 : | * Sets the default domain textdomain | ||
| 231 : | */ | ||
| 232 : | function textdomain($domain = null){ | ||
| 233 : | csouza | 266 | if ($this->has_gettext) { |
| 234 : | $domain = textdomain($domain); | ||
| 235 : | csouza | 39 | } |
| 236 : | csouza | 266 | elseif (!is_null($domain)) { |
| 237 : | $this->domain = $domain; | ||
| 238 : | $this->load($domain, $this->text_domains[$this->domain]['path']); | ||
| 239 : | } | ||
| 240 : | csouza | 149 | return $this->domain; |
| 241 : | csouza | 39 | } |
| 242 : | csouza | 266 | |
| 243 : | csouza | 39 | /** |
| 244 : | csouza | 266 | * Overrides the domain for a single lookup |
| 245 : | csouza | 39 | * This function allows you to override the current domain for a single message lookup. |
| 246 : | * It also allows you to specify a category. | ||
| 247 : | * Categories are folders within the languages directory . | ||
| 248 : | * currently, only LC_MESSAGES is implemented | ||
| 249 : | * | ||
| 250 : | * The values for categories are: | ||
| 251 : | * LC_CTYPE 0 | ||
| 252 : | * LC_NUMERIC 1 | ||
| 253 : | * LC_TIME 2 | ||
| 254 : | * LC_COLLATE 3 | ||
| 255 : | * LC_MONETARY 4 | ||
| 256 : | * LC_MESSAGES 5 | ||
| 257 : | * LC_ALL 6 | ||
| 258 : | * | ||
| 259 : | * not yet implemented | ||
| 260 : | */ | ||
| 261 : | function dcgettext($domain, $message, $category){ | ||
| 262 : | return $message; | ||
| 263 : | } | ||
| 264 : | |||
| 265 : | /** | ||
| 266 : | csouza | 266 | * dcngettext -- Plural version of dcgettext |
| 267 : | csouza | 39 | * not yet implemented |
| 268 : | */ | ||
| 269 : | function dcngettext($domain, $msg1, $msg2, $count, $category){ | ||
| 270 : | return $msg1; | ||
| 271 : | csouza | 149 | } |
| 272 : | csouza | 39 | |
| 273 : | csouza | 149 | |
| 274 : | csouza | 266 | /** |
| 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 : | csouza | 39 | } |
| 327 : | |||
| 328 : | class PHPGettextAdmin | ||
| 329 : | { | ||
| 330 : | csouza | 149 | |
| 331 : | csouza | 39 | function PHPGettextAdmin(){} |
| 332 : | csouza | 149 | |
| 333 : | csouza | 39 | /** |
| 334 : | * Enter description here... | ||
| 335 : | * | ||
| 336 : | * @param unknown_type $domain | ||
| 337 : | * @param unknown_type $langdir | ||
| 338 : | */ | ||
| 339 : | function msgfmt ($domain, $langdir) { | ||
| 340 : | $cmd = "msgfmt"; | ||
| 341 : | $arg = "-o $langdir/LC_MESSAGES/$domain.mo $langdir/$domain.po"; | ||
| 342 : | //$exec = 'msgfmt --help'; | ||
| 343 : | return $this->execute($cmd, $arg); | ||
| 344 : | } | ||
| 345 : | /** | ||
| 346 : | * Enter description here... | ||
| 347 : | * | ||
| 348 : | * @param unknown_type $domain | ||
| 349 : | * @param unknown_type $langdir | ||
| 350 : | */ | ||
| 351 : | csouza | 266 | function msgmerge ($args) { |
| 352 : | $cmd = "msgmerge "; | ||
| 353 : | return $this->execute($cmd.$args); | ||
| 354 : | csouza | 39 | } |
| 355 : | csouza | 149 | |
| 356 : | csouza | 39 | /** |
| 357 : | * Invoke the xgettext utility with $args | ||
| 358 : | * the xgettext executable must be in PATH | ||
| 359 : | * | ||
| 360 : | * @param string the commandline arguments to gettext | ||
| 361 : | * @return unknown | ||
| 362 : | */ | ||
| 363 : | csouza | 149 | function xgettext($args) { |
| 364 : | csouza | 266 | $cmd = "xgettext "; |
| 365 : | csouza | 149 | return $this->execute($cmd.$args); |
| 366 : | } | ||
| 367 : | |||
| 368 : | |||
| 369 : | csouza | 39 | /** |
| 370 : | * Enter description here... | ||
| 371 : | * | ||
| 372 : | * @param unknown_type $domain | ||
| 373 : | * @param unknown_type $langdir | ||
| 374 : | */ | ||
| 375 : | csouza | 149 | function execute($cmd) { |
| 376 : | csouza | 39 | |
| 377 : | csouza | 149 | $return = false; |
| 378 : | csouza | 153 | if (substr(strtoupper(PHP_OS), 0, 3) == 'WIN'){ |
| 379 : | csouza | 149 | $outputfile = session_save_path() . DIRECTORY_SEPARATOR . md5(uniqid(rand(), true)) . ".txt"; |
| 380 : | csouza | 266 | $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 : | csouza | 39 | } else { |
| 386 : | $cmd = "$cmd"; | ||
| 387 : | csouza | 149 | $lastline = exec($cmd, $output, $return); |
| 388 : | csouza | 39 | } |
| 389 : | csouza | 149 | return $return; |
| 390 : | csouza | 39 | } |
| 391 : | } | ||
| 392 : | |||
| 393 : | /** | ||
| 394 : | * Enter description here... | ||
| 395 : | * | ||
| 396 : | * @return unknown | ||
| 397 : | */ | ||
| 398 : | function &phpgettext(){ | ||
| 399 : | static $gettext; | ||
| 400 : | csouza | 149 | if (is_null($gettext)) { |
| 401 : | csouza | 39 | require_once('phpgettext.class.php'); |
| 402 : | csouza | 149 | $gettext = new PHPGettext(); |
| 403 : | csouza | 175 | $gettext->has_gettext = true; |
| 404 : | csouza | 39 | if (!function_exists("gettext") || !function_exists("_")) { |
| 405 : | $gettext->has_gettext = false; | ||
| 406 : | require_once('phpgettext.compat.php'); | ||
| 407 : | } | ||
| 408 : | } | ||
| 409 : | return $gettext; | ||
| 410 : | } | ||
| 411 : | function T_($message) { | ||
| 412 : | $gettext =& phpgettext(); | ||
| 413 : | csouza | 154 | $trans = $gettext->gettext($message); |
| 414 : | csouza | 153 | return $trans; |
| 415 : | csouza | 39 | } |
| 416 : | function Tn_($msg1, $msg2, $count) { | ||
| 417 : | $gettext =& phpgettext(); | ||
| 418 : | return $gettext->ngettext($msg1, $msg2, $count); | ||
| 419 : | } | ||
| 420 : | csouza | 149 | function Td_($domain, $message) { |
| 421 : | csouza | 39 | $gettext =& phpgettext(); |
| 422 : | return $gettext->dgettext($domain, $message); | ||
| 423 : | } | ||
| 424 : | csouza | 149 | function Tdn_($domain, $msg1, $msg2, $count) { |
| 425 : | csouza | 39 | $gettext =& phpgettext(); |
| 426 : | return $gettext->dngettext($domain, $msg1, $msg2, $count); | ||
| 427 : | } | ||
| 428 : | ?> |
| ViewVC Help | |
| Powered by ViewVC 1.0.0 |
Web Hosting provided by Network Redux.

