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

