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

