Annotation of /mambo/branches/4.6/includes/phpgettext/phpgettext.class.php
Parent Directory
|
Revision Log
Revision 175 - (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 : | * class PHPGettext | ||
| 12 : | * php replacement for gettext where it is not available | ||
| 13 : | * | ||
| 14 : | * not all gettext functions are implemented yet | ||
| 15 : | * | ||
| 16 : | * | ||
| 17 : | * | ||
| 18 : | */ | ||
| 19 : | class PHPGettext | ||
| 20 : | { | ||
| 21 : | /** | ||
| 22 : | * Enter description here... | ||
| 23 : | * | ||
| 24 : | * @var bool | ||
| 25 : | */ | ||
| 26 : | var $has_gettext; | ||
| 27 : | csouza | 149 | |
| 28 : | csouza | 39 | /** |
| 29 : | * The current locale. eg: en-GB | ||
| 30 : | * | ||
| 31 : | * @var string | ||
| 32 : | */ | ||
| 33 : | var $locale; | ||
| 34 : | csouza | 149 | |
| 35 : | csouza | 39 | /** |
| 36 : | * The current domain | ||
| 37 : | * | ||
| 38 : | * @var string | ||
| 39 : | */ | ||
| 40 : | var $domain; | ||
| 41 : | csouza | 149 | |
| 42 : | csouza | 39 | /** |
| 43 : | * The current character set | ||
| 44 : | * | ||
| 45 : | * @var unknown_type | ||
| 46 : | */ | ||
| 47 : | var $charset; | ||
| 48 : | csouza | 149 | |
| 49 : | csouza | 39 | /** |
| 50 : | * Container for the loaded domains | ||
| 51 : | * | ||
| 52 : | * @var array | ||
| 53 : | */ | ||
| 54 : | var $text_domains; | ||
| 55 : | csouza | 149 | |
| 56 : | csouza | 39 | /** |
| 57 : | * The asssociative array of messages for the current domain | ||
| 58 : | * | ||
| 59 : | * @var unknown_type | ||
| 60 : | */ | ||
| 61 : | var $messages; | ||
| 62 : | |||
| 63 : | /** | ||
| 64 : | csouza | 153 | * The debugging flag |
| 65 : | * | ||
| 66 : | * @var unknown_type | ||
| 67 : | */ | ||
| 68 : | var $debug; | ||
| 69 : | |||
| 70 : | /** | ||
| 71 : | csouza | 39 | * Enter description here... |
| 72 : | * | ||
| 73 : | * @return PHPGettext | ||
| 74 : | */ | ||
| 75 : | function PHPGettext() {} | ||
| 76 : | |||
| 77 : | |||
| 78 : | csouza | 149 | |
| 79 : | csouza | 39 | /** |
| 80 : | * Enter description here... | ||
| 81 : | * | ||
| 82 : | * Lookup the locale from the environment variables. | ||
| 83 : | * Priority order for gettext is: | ||
| 84 : | * 1. LANGUAGE | ||
| 85 : | * 2. LC_ALL | ||
| 86 : | * 3. LC_MESSAGE | ||
| 87 : | * 4. LANG | ||
| 88 : | * | ||
| 89 : | * @return unknown | ||
| 90 : | */ | ||
| 91 : | function getlocale() { | ||
| 92 : | if (empty($this->locale)) { | ||
| 93 : | $langs = array( getenv('LANGUAGE'), | ||
| 94 : | csouza | 149 | getenv('LC_ALL'), |
| 95 : | getenv('LC_MESSAGE'), | ||
| 96 : | getenv('LANG') | ||
| 97 : | ); | ||
| 98 : | csouza | 39 | foreach ($langs as $lang) { |
| 99 : | if ($lang){ | ||
| 100 : | $this->locale = $lang; | ||
| 101 : | break; | ||
| 102 : | } | ||
| 103 : | } | ||
| 104 : | } | ||
| 105 : | return $this->locale; | ||
| 106 : | } | ||
| 107 : | /** | ||
| 108 : | * Set locale environment variables | ||
| 109 : | */ | ||
| 110 : | |||
| 111 : | function setlocale($lang) { | ||
| 112 : | $this->locale = $lang; | ||
| 113 : | putenv("LANGUAGE=$lang"); | ||
| 114 : | putenv("LC_ALL=$lang"); | ||
| 115 : | putenv("LC_MESSAGE=$lang"); | ||
| 116 : | putenv("LANG=$lang"); | ||
| 117 : | csouza | 175 | $ret = setlocale(LC_ALL, ''); |
| 118 : | csouza | 39 | return $ret; |
| 119 : | } | ||
| 120 : | |||
| 121 : | /** | ||
| 122 : | csouza | 153 | * debugging function |
| 123 : | * | ||
| 124 : | */ | ||
| 125 : | csouza | 154 | function debug($message, $untranslated = false){ |
| 126 : | $trace = debug_backtrace(); | ||
| 127 : | csouza | 175 | $str = sprintf('T_(%s)%s',$message, $untranslated ? ';' : ''); |
| 128 : | csouza | 154 | if ($this->debug > 1) { |
| 129 : | $html = '<span style="border-bottom: thin solid %s" title="%s(%d)">T_(%s)</span>'; | ||
| 130 : | $str = sprintf($html, ($untranslated ? 'red' : 'green'), str_replace('\\', '/', $trace[2]['file']), $trace[2]['line'], $message); | ||
| 131 : | } | ||
| 132 : | return $str; | ||
| 133 : | csouza | 153 | } |
| 134 : | |||
| 135 : | /** | ||
| 136 : | csouza | 39 | * Alias for gettext |
| 137 : | * will also output the result if $output = true | ||
| 138 : | */ | ||
| 139 : | csouza | 149 | function _($message, $output = false){ |
| 140 : | csouza | 153 | $return = $this->gettext($message); |
| 141 : | csouza | 39 | if ($output) { |
| 142 : | csouza | 153 | echo $return; |
| 143 : | csouza | 39 | return true; |
| 144 : | } | ||
| 145 : | csouza | 153 | return $return; |
| 146 : | csouza | 39 | } |
| 147 : | |||
| 148 : | /** | ||
| 149 : | * Lookup a message in the current domain | ||
| 150 : | * returns translation if it exists or original message | ||
| 151 : | */ | ||
| 152 : | function gettext($message){ | ||
| 153 : | csouza | 153 | $translation = $message; |
| 154 : | csouza | 39 | if ($this->has_gettext){ |
| 155 : | csouza | 153 | $translation = gettext($message); |
| 156 : | csouza | 39 | } |
| 157 : | csouza | 153 | elseif (isset($this->messages[$this->domain][$message])) { |
| 158 : | csouza | 39 | $translation = $this->messages[$this->domain][$message]; |
| 159 : | csouza | 149 | } |
| 160 : | csouza | 154 | $untranslated = (strcmp($translation, $message) === 0) ? true : false; |
| 161 : | $translation = ($this->debug) ? $this->debug($translation, $untranslated) : $translation; | ||
| 162 : | csouza | 153 | return $translation; |
| 163 : | csouza | 39 | } |
| 164 : | |||
| 165 : | /** | ||
| 166 : | * Specify the character encoding in which the messages | ||
| 167 : | * from the DOMAIN message catalog will be returned | ||
| 168 : | * | ||
| 169 : | */ | ||
| 170 : | function bind_textdomain_codeset($domain, $charset){ | ||
| 171 : | if ($this->has_gettext){ | ||
| 172 : | bind_textdomain_codeset($domain, $charset); | ||
| 173 : | } | ||
| 174 : | csouza | 175 | return $this->text_domains[$domain]["charset"] = $charset; |
| 175 : | csouza | 39 | } |
| 176 : | |||
| 177 : | /** | ||
| 178 : | * Sets the path for a domain | ||
| 179 : | * if gettext is unavailable, translation files will be loaded here | ||
| 180 : | * | ||
| 181 : | */ | ||
| 182 : | function bindtextdomain($domain, $path){ | ||
| 183 : | if ($this->has_gettext){ | ||
| 184 : | bindtextdomain($domain, $path); | ||
| 185 : | } else { | ||
| 186 : | $this->load($domain, $path); | ||
| 187 : | } | ||
| 188 : | csouza | 175 | return $this->text_domains[$domain]["path"] = $path; |
| 189 : | csouza | 39 | } |
| 190 : | |||
| 191 : | /** | ||
| 192 : | * Sets the default domain textdomain | ||
| 193 : | */ | ||
| 194 : | function textdomain($domain = null){ | ||
| 195 : | if ($this->has_gettext){ | ||
| 196 : | textdomain($domain); | ||
| 197 : | } | ||
| 198 : | if (is_null($domain)) | ||
| 199 : | csouza | 149 | return $this->domain; |
| 200 : | csouza | 39 | else |
| 201 : | csouza | 149 | return $this->domain = $domain; |
| 202 : | csouza | 39 | } |
| 203 : | |||
| 204 : | /** | ||
| 205 : | * Override the current domain | ||
| 206 : | * The dgettext() function allows you to override the current domain for a single message lookup. | ||
| 207 : | */ | ||
| 208 : | function dgettext($domain, $message){ | ||
| 209 : | if (array_key_exists($domain, $this->messages)) | ||
| 210 : | $translation = $this->messages[$domain][$message]; | ||
| 211 : | if ($translation) | ||
| 212 : | csouza | 149 | return $translation; |
| 213 : | csouza | 39 | |
| 214 : | return $message; | ||
| 215 : | } | ||
| 216 : | |||
| 217 : | /** | ||
| 218 : | * Plural version of gettext * | ||
| 219 : | * not yet implemented | ||
| 220 : | */ | ||
| 221 : | function ngettext($msg1, $msg2, $count){ | ||
| 222 : | if ($this->has_gettext){ | ||
| 223 : | csouza | 175 | $translation = ngettext($msg1, $msg2, $count); |
| 224 : | csouza | 39 | } |
| 225 : | if (isset( $this->messages[$this->domain][$msg1])) { | ||
| 226 : | $translation = $this->messages[$this->domain][$msg1]; | ||
| 227 : | return $translation; | ||
| 228 : | csouza | 149 | } |
| 229 : | csouza | 39 | return $translation; |
| 230 : | } | ||
| 231 : | /** | ||
| 232 : | * Plural version of dgettext . | ||
| 233 : | * not yet implemented | ||
| 234 : | */ | ||
| 235 : | function dngettext($domain, $msg1, $msg2, $count){ | ||
| 236 : | return $msg1; | ||
| 237 : | } | ||
| 238 : | |||
| 239 : | |||
| 240 : | function load($domain, $path) { | ||
| 241 : | require_once('phpgettext.catalog.php'); | ||
| 242 : | $catalog = new PHPGettext_catalog($domain, $path); | ||
| 243 : | $catalog->setproperty('mode', _MODE_MO_); | ||
| 244 : | $catalog->setproperty('lang', $this->getlocale()); | ||
| 245 : | $catalog->load(); | ||
| 246 : | foreach ($catalog->strings as $string) | ||
| 247 : | csouza | 149 | $this->messages[$domain][$string->msgid] = $string->msgstr; |
| 248 : | csouza | 39 | } |
| 249 : | csouza | 149 | |
| 250 : | csouza | 39 | /** |
| 251 : | * Overrides the domain for a single lookup string | ||
| 252 : | * This function allows you to override the current domain for a single message lookup. | ||
| 253 : | * It also allows you to specify a category. | ||
| 254 : | * Categories are folders within the languages directory . | ||
| 255 : | * currently, only LC_MESSAGES is implemented | ||
| 256 : | * | ||
| 257 : | * The values for categories are: | ||
| 258 : | * LC_CTYPE 0 | ||
| 259 : | * LC_NUMERIC 1 | ||
| 260 : | * LC_TIME 2 | ||
| 261 : | * LC_COLLATE 3 | ||
| 262 : | * LC_MONETARY 4 | ||
| 263 : | * LC_MESSAGES 5 | ||
| 264 : | * LC_ALL 6 | ||
| 265 : | * | ||
| 266 : | * not yet implemented | ||
| 267 : | */ | ||
| 268 : | function dcgettext($domain, $message, $category){ | ||
| 269 : | return $message; | ||
| 270 : | } | ||
| 271 : | |||
| 272 : | /** | ||
| 273 : | * Plural version of dcgettext | ||
| 274 : | * not yet implemented | ||
| 275 : | */ | ||
| 276 : | function dcngettext($domain, $msg1, $msg2, $count, $category){ | ||
| 277 : | return $msg1; | ||
| 278 : | csouza | 149 | } |
| 279 : | csouza | 39 | |
| 280 : | csouza | 149 | |
| 281 : | csouza | 39 | } |
| 282 : | |||
| 283 : | class PHPGettextAdmin | ||
| 284 : | { | ||
| 285 : | csouza | 149 | |
| 286 : | csouza | 39 | function PHPGettextAdmin(){} |
| 287 : | csouza | 149 | |
| 288 : | csouza | 39 | /** |
| 289 : | * Enter description here... | ||
| 290 : | * | ||
| 291 : | * @param unknown_type $domain | ||
| 292 : | * @param unknown_type $langdir | ||
| 293 : | */ | ||
| 294 : | function msgfmt ($domain, $langdir) { | ||
| 295 : | $cmd = "msgfmt"; | ||
| 296 : | $arg = "-o $langdir/LC_MESSAGES/$domain.mo $langdir/$domain.po"; | ||
| 297 : | //$exec = 'msgfmt --help'; | ||
| 298 : | return $this->execute($cmd, $arg); | ||
| 299 : | } | ||
| 300 : | /** | ||
| 301 : | * Enter description here... | ||
| 302 : | * | ||
| 303 : | * @param unknown_type $domain | ||
| 304 : | * @param unknown_type $langdir | ||
| 305 : | */ | ||
| 306 : | function msgmerge ($outdated, $recent, $langdir) { | ||
| 307 : | #msgmerge $langdir/$old.po $new.po | ||
| 308 : | $cmd = "msgmerge"; | ||
| 309 : | $arg = "$langdir/$outdated $recent"; | ||
| 310 : | //$exec = 'msgmerge --help'; | ||
| 311 : | return $this->execute($cmd, $arg); | ||
| 312 : | } | ||
| 313 : | csouza | 149 | |
| 314 : | csouza | 39 | /** |
| 315 : | * Invoke the xgettext utility with $args | ||
| 316 : | * the xgettext executable must be in PATH | ||
| 317 : | * | ||
| 318 : | * @param string the commandline arguments to gettext | ||
| 319 : | * @return unknown | ||
| 320 : | */ | ||
| 321 : | csouza | 149 | function xgettext($args) { |
| 322 : | $cmd = substr(strtoupper(PHP_OS), 0, 3) == 'WIN' ? "xgettext.exe " : "xgettext "; | ||
| 323 : | return $this->execute($cmd.$args); | ||
| 324 : | } | ||
| 325 : | |||
| 326 : | |||
| 327 : | csouza | 39 | /** |
| 328 : | * Enter description here... | ||
| 329 : | * | ||
| 330 : | * @param unknown_type $domain | ||
| 331 : | * @param unknown_type $langdir | ||
| 332 : | */ | ||
| 333 : | csouza | 149 | function execute($cmd) { |
| 334 : | csouza | 39 | |
| 335 : | csouza | 149 | $return = false; |
| 336 : | csouza | 153 | if (substr(strtoupper(PHP_OS), 0, 3) == 'WIN'){ |
| 337 : | csouza | 149 | $outputfile = session_save_path() . DIRECTORY_SEPARATOR . md5(uniqid(rand(), true)) . ".txt"; |
| 338 : | csouza | 39 | $shell = new COM("WScript.Shell"); |
| 339 : | csouza | 149 | $shell->Run("cmd /C $cmd > $outputfile", 0, true); |
| 340 : | $return = file_get_contents($outputfile); | ||
| 341 : | unlink($outputfile); | ||
| 342 : | csouza | 39 | } else { |
| 343 : | $cmd = "$cmd"; | ||
| 344 : | csouza | 149 | $lastline = exec($cmd, $output, $return); |
| 345 : | csouza | 39 | } |
| 346 : | csouza | 149 | return $return; |
| 347 : | csouza | 39 | } |
| 348 : | } | ||
| 349 : | |||
| 350 : | |||
| 351 : | csouza | 149 | |
| 352 : | |||
| 353 : | csouza | 39 | /** |
| 354 : | * Enter description here... | ||
| 355 : | * | ||
| 356 : | * @return unknown | ||
| 357 : | */ | ||
| 358 : | function &phpgettext(){ | ||
| 359 : | static $gettext; | ||
| 360 : | csouza | 149 | if (is_null($gettext)) { |
| 361 : | csouza | 39 | require_once('phpgettext.class.php'); |
| 362 : | csouza | 149 | $gettext = new PHPGettext(); |
| 363 : | csouza | 175 | $gettext->has_gettext = true; |
| 364 : | csouza | 39 | if (!function_exists("gettext") || !function_exists("_")) { |
| 365 : | $gettext->has_gettext = false; | ||
| 366 : | require_once('phpgettext.compat.php'); | ||
| 367 : | } | ||
| 368 : | } | ||
| 369 : | return $gettext; | ||
| 370 : | } | ||
| 371 : | |||
| 372 : | function T_($message) { | ||
| 373 : | $gettext =& phpgettext(); | ||
| 374 : | csouza | 154 | $trans = $gettext->gettext($message); |
| 375 : | csouza | 153 | return $trans; |
| 376 : | csouza | 39 | } |
| 377 : | |||
| 378 : | function Tn_($msg1, $msg2, $count) { | ||
| 379 : | $gettext =& phpgettext(); | ||
| 380 : | return $gettext->ngettext($msg1, $msg2, $count); | ||
| 381 : | } | ||
| 382 : | csouza | 149 | function Td_($domain, $message) { |
| 383 : | csouza | 39 | $gettext =& phpgettext(); |
| 384 : | return $gettext->dgettext($domain, $message); | ||
| 385 : | } | ||
| 386 : | csouza | 149 | function Tdn_($domain, $msg1, $msg2, $count) { |
| 387 : | csouza | 39 | $gettext =& phpgettext(); |
| 388 : | return $gettext->dngettext($domain, $msg1, $msg2, $count); | ||
| 389 : | } | ||
| 390 : | |||
| 391 : | ?> |
| ViewVC Help | |
| Powered by ViewVC 1.0.0 |
Web Hosting provided by Network Redux.

