Annotation of /mambo/branches/4.6/includes/phpgettext/phpgettext.class.php
Parent Directory
|
Revision Log
Revision 913 - (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 : | neilt | 686 | * |
| 10 : | * | ||
| 11 : | csouza | 39 | */ |
| 12 : | andphe | 896 | defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' ); |
| 13 : | csouza | 39 | class PHPGettext |
| 14 : | { | ||
| 15 : | var $has_gettext; | ||
| 16 : | csouza | 149 | |
| 17 : | csouza | 39 | /** |
| 18 : | * The current locale. eg: en-GB | ||
| 19 : | */ | ||
| 20 : | csouza | 266 | var $lang; |
| 21 : | /** | ||
| 22 : | * The current locale. eg: en-GB | ||
| 23 : | */ | ||
| 24 : | csouza | 39 | var $locale; |
| 25 : | csouza | 149 | |
| 26 : | csouza | 39 | /** |
| 27 : | * The current domain | ||
| 28 : | */ | ||
| 29 : | var $domain; | ||
| 30 : | csouza | 149 | |
| 31 : | csouza | 39 | /** |
| 32 : | * The current character set | ||
| 33 : | */ | ||
| 34 : | var $charset; | ||
| 35 : | csouza | 149 | |
| 36 : | csouza | 39 | /** |
| 37 : | * Container for the loaded domains | ||
| 38 : | */ | ||
| 39 : | csouza | 183 | var $text_domains = array(); |
| 40 : | csouza | 149 | |
| 41 : | csouza | 39 | /** |
| 42 : | csouza | 266 | * The asssociative array of headers for the current domain |
| 43 : | */ | ||
| 44 : | var $headers = array(); | ||
| 45 : | /** | ||
| 46 : | csouza | 39 | * The asssociative array of messages for the current domain |
| 47 : | */ | ||
| 48 : | csouza | 183 | var $messages = array(); |
| 49 : | csouza | 39 | |
| 50 : | /** | ||
| 51 : | csouza | 153 | * The debugging flag |
| 52 : | */ | ||
| 53 : | var $debug; | ||
| 54 : | |||
| 55 : | csouza | 39 | function PHPGettext() {} |
| 56 : | |||
| 57 : | /** | ||
| 58 : | neilt | 686 | * |
| 59 : | csouza | 179 | * Set and lookup the locale from the environment variables. |
| 60 : | csouza | 39 | * Priority order for gettext is: |
| 61 : | * 1. LANGUAGE | ||
| 62 : | * 2. LC_ALL | ||
| 63 : | * 3. LC_MESSAGE | ||
| 64 : | neilt | 686 | * 4. LANG |
| 65 : | csouza | 39 | * |
| 66 : | * @return unknown | ||
| 67 : | */ | ||
| 68 : | csouza | 183 | |
| 69 : | neilt | 686 | function setlocale($lang, $locale=false) { |
| 70 : | //supress error messages | ||
| 71 : | ini_set('display_errors', '0'); | ||
| 72 : | #dump(setlocale(LC_ALL, 0)); | ||
| 73 : | putenv("LANGUAGE=$lang"); | ||
| 74 : | putenv("LC_ALL=$lang"); | ||
| 75 : | putenv("LC_MESSAGE=$lang"); | ||
| 76 : | putenv("LANG=$lang"); | ||
| 77 : | $this->lang = $lang; | ||
| 78 : | if ($locale) { | ||
| 79 : | andphe | 908 | $this->locale = setlocale(LC_ALL,$locale); |
| 80 : | neilt | 686 | } |
| 81 : | //allow error messages | ||
| 82 : | ini_set('display_errors', '1'); | ||
| 83 : | } | ||
| 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 : | neilt | 686 | * |
| 104 : | csouza | 153 | */ |
| 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 | 409 | $translation = !empty($this->messages[$this->domain][$message]) ? $this->messages[$this->domain][$message] : $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 | 556 | if (isset($this->messages[$domain][$message])) |
| 161 : | csouza | 409 | $translation = !empty($this->messages[$domain][$message]) ? $this->messages[$domain][$message] : $message; |
| 162 : | csouza | 183 | } |
| 163 : | $untranslated = (strcmp($translation, $message) === 0) ? true : false; | ||
| 164 : | return $this->output($translation, $untranslated); | ||
| 165 : | } | ||
| 166 : | |||
| 167 : | /** | ||
| 168 : | neilt | 686 | * 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 : | csouza | 556 | $original = array($msgid, $msgid_plural); |
| 176 : | csouza | 266 | if (isset($this->messages[$this->domain][$msgid][$plural])) { |
| 177 : | $translation = $this->messages[$this->domain][$msgid][$plural]; | ||
| 178 : | } else { | ||
| 179 : | $original = array($msgid, $msgid_plural); | ||
| 180 : | $translation = $original[$plural]; | ||
| 181 : | csouza | 183 | } |
| 182 : | csouza | 556 | $untranslated = isset($original[$plural]) && (strcmp($translation, $original[$plural]) === 0) ? true : false; |
| 183 : | csouza | 183 | return $this->output($translation, $untranslated); |
| 184 : | } | ||
| 185 : | /** | ||
| 186 : | neilt | 686 | * Plural version of dgettext |
| 187 : | csouza | 183 | */ |
| 188 : | csouza | 266 | function dngettext($domain, $msgid, $msgid_plural, $count){ |
| 189 : | $original = array($msgid, $msgid_plural); | ||
| 190 : | $plural = $this->getplural($count, $domain); | ||
| 191 : | if ($this->has_gettext){ | ||
| 192 : | $translation = dngettext($domain, $msgid, $msgid_plural, $count); | ||
| 193 : | } else { | ||
| 194 : | if (isset($this->messages[$domain][$msgid][$plural])) { | ||
| 195 : | $translation = $this->messages[$domain][$msgid][$plural]; | ||
| 196 : | } else { | ||
| 197 : | $translation = $original[$plural]; | ||
| 198 : | } | ||
| 199 : | } | ||
| 200 : | $untranslated = (strcmp($translation, $original[$plural]) === 0) ? true : false; | ||
| 201 : | return $this->output($translation, $untranslated); | ||
| 202 : | csouza | 183 | } |
| 203 : | |||
| 204 : | /** | ||
| 205 : | neilt | 686 | * Specify the character encoding in which the messages |
| 206 : | * from the DOMAIN message catalog will be returned | ||
| 207 : | csouza | 39 | * |
| 208 : | */ | ||
| 209 : | function bind_textdomain_codeset($domain, $charset){ | ||
| 210 : | if ($this->has_gettext){ | ||
| 211 : | bind_textdomain_codeset($domain, $charset); | ||
| 212 : | } | ||
| 213 : | csouza | 175 | return $this->text_domains[$domain]["charset"] = $charset; |
| 214 : | csouza | 39 | } |
| 215 : | |||
| 216 : | /** | ||
| 217 : | * Sets the path for a domain | ||
| 218 : | * if gettext is unavailable, translation files will be loaded here | ||
| 219 : | neilt | 686 | * |
| 220 : | csouza | 39 | */ |
| 221 : | function bindtextdomain($domain, $path){ | ||
| 222 : | if ($this->has_gettext){ | ||
| 223 : | bindtextdomain($domain, $path); | ||
| 224 : | } else { | ||
| 225 : | $this->load($domain, $path); | ||
| 226 : | } | ||
| 227 : | csouza | 175 | return $this->text_domains[$domain]["path"] = $path; |
| 228 : | csouza | 39 | } |
| 229 : | |||
| 230 : | /** | ||
| 231 : | * Sets the default domain textdomain | ||
| 232 : | */ | ||
| 233 : | function textdomain($domain = null){ | ||
| 234 : | csouza | 266 | if ($this->has_gettext) { |
| 235 : | andphe | 908 | $this->domain = textdomain($domain); |
| 236 : | csouza | 39 | } |
| 237 : | csouza | 266 | elseif (!is_null($domain)) { |
| 238 : | $this->domain = $domain; | ||
| 239 : | $this->load($domain, $this->text_domains[$this->domain]['path']); | ||
| 240 : | } | ||
| 241 : | csouza | 149 | return $this->domain; |
| 242 : | csouza | 39 | } |
| 243 : | csouza | 403 | |
| 244 : | csouza | 39 | /** |
| 245 : | csouza | 266 | * Overrides the domain for a single lookup |
| 246 : | neilt | 686 | * This function allows you to override the current domain for a single message lookup. |
| 247 : | * It also allows you to specify a category. | ||
| 248 : | csouza | 39 | * Categories are folders within the languages directory . |
| 249 : | neilt | 686 | * currently, only LC_MESSAGES is implemented |
| 250 : | csouza | 39 | * |
| 251 : | * The values for categories are: | ||
| 252 : | * LC_CTYPE 0 | ||
| 253 : | * LC_NUMERIC 1 | ||
| 254 : | * LC_TIME 2 | ||
| 255 : | * LC_COLLATE 3 | ||
| 256 : | * LC_MONETARY 4 | ||
| 257 : | * LC_MESSAGES 5 | ||
| 258 : | * LC_ALL 6 | ||
| 259 : | * | ||
| 260 : | * not yet implemented | ||
| 261 : | */ | ||
| 262 : | function dcgettext($domain, $message, $category){ | ||
| 263 : | return $message; | ||
| 264 : | } | ||
| 265 : | |||
| 266 : | /** | ||
| 267 : | csouza | 266 | * dcngettext -- Plural version of dcgettext |
| 268 : | csouza | 39 | * not yet implemented |
| 269 : | */ | ||
| 270 : | function dcngettext($domain, $msg1, $msg2, $count, $category){ | ||
| 271 : | return $msg1; | ||
| 272 : | csouza | 149 | } |
| 273 : | csouza | 39 | |
| 274 : | csouza | 149 | |
| 275 : | csouza | 266 | /** |
| 276 : | neilt | 686 | * Plural-Forms: nplurals=2; plural=n == 1 ? 0 : 1; |
| 277 : | * | ||
| 278 : | csouza | 266 | * nplurals - total number of plurals |
| 279 : | * plural - the plural index | ||
| 280 : | neilt | 686 | * |
| 281 : | csouza | 266 | * Plural-Forms: nplurals=1; plural=0; |
| 282 : | neilt | 686 | * 1 form only |
| 283 : | * | ||
| 284 : | * Plural-Forms: nplurals=2; plural=n == 1 ? 0 : 1; | ||
| 285 : | csouza | 266 | * Plural-Forms: nplurals=2; plural=n != 1; |
| 286 : | * 2 forms, singular used for one only | ||
| 287 : | neilt | 686 | * |
| 288 : | csouza | 266 | * Plural-Forms: nplurals=2; plural=n>1; |
| 289 : | * 2 forms, singular used for zero and one | ||
| 290 : | neilt | 686 | * |
| 291 : | csouza | 266 | * Plural-Forms: nplurals=3; plural=n%10==1 && n%100!=11 ? 0 : n != 0 ? 1 : 2; |
| 292 : | * 3 forms, special case for zero | ||
| 293 : | neilt | 686 | * |
| 294 : | csouza | 266 | * Plural-Forms: nplurals=3; plural=n==1 ? 0 : n==2 ? 1 : 2; |
| 295 : | * 3 forms, special cases for one and two | ||
| 296 : | neilt | 686 | * |
| 297 : | csouza | 266 | * Plural-Forms: nplurals=4; plural=n%100==1 ? 0 : n%100==2 ? 1 : n%100==3 || n%100==4 ? 2 : 3; |
| 298 : | * 4 forms, special case for one and all numbers ending in 02, 03, or 04 | ||
| 299 : | */ | ||
| 300 : | function getplural($count, $domain) { | ||
| 301 : | if (isset($this->headers[$domain]['Plural-Forms'])) { | ||
| 302 : | $plural_forms = $this->headers[$domain]['Plural-Forms']; | ||
| 303 : | preg_match('/nplurals[\s]*[=]{1}[\s]*([\d]+);[\s]*plural[\s]*[=]{1}[\s]*(.*);$/', $plural_forms, $matches); | ||
| 304 : | $nplurals = $matches[1]; | ||
| 305 : | $plural_exp = $matches[2]; | ||
| 306 : | if ($nplurals > 1 && strpos($plural_exp, ':') === false) { | ||
| 307 : | $plural = 'return ('.preg_replace('/n/', $count, $plural_exp).') ? 1 : 0;'; | ||
| 308 : | } else { | ||
| 309 : | $plural = 'return '.preg_replace('/n/', $count, $plural_exp); | ||
| 310 : | } | ||
| 311 : | } else { | ||
| 312 : | $plural = 'return '.preg_replace('/n/', $count, 'n != 1 ? 1 : 0;'); | ||
| 313 : | } | ||
| 314 : | return eval($plural); | ||
| 315 : | } | ||
| 316 : | |||
| 317 : | function load($domain, $path) { | ||
| 318 : | andphe | 896 | $root = dirname(__FILE__); |
| 319 : | require_once($root.'/phpgettext.catalog.php'); | ||
| 320 : | csouza | 266 | $catalog = new PHPGettext_catalog($domain, $path); |
| 321 : | $catalog->setproperty('mode', _MODE_MO_); | ||
| 322 : | $catalog->setproperty('lang', $this->lang); | ||
| 323 : | $catalog->load(); | ||
| 324 : | $this->headers[$domain] = $catalog->headers; | ||
| 325 : | foreach ($catalog->strings as $string) | ||
| 326 : | $this->messages[$domain][$string->msgid] = $string->msgstr; | ||
| 327 : | } | ||
| 328 : | ninekrit | 775 | //Thank you - Inicio Agregado Andres Felipe Vargas |
| 329 : | function add($domain) { | ||
| 330 : | $catalog = new PHPGettext_catalog($domain, $this->text_domains[$this->domain]["path"]); | ||
| 331 : | $catalog->setproperty('mode', _MODE_MO_); | ||
| 332 : | $catalog->setproperty('lang', $this->lang); | ||
| 333 : | $catalog->load(); | ||
| 334 : | foreach ($catalog->strings as $string) | ||
| 335 : | $this->messages[$this->domain][$string->msgid] = $string->msgstr; | ||
| 336 : | } | ||
| 337 : | //end Inicio Agregado Andres Felipe Vargas | ||
| 338 : | |||
| 339 : | csouza | 39 | } |
| 340 : | |||
| 341 : | csouza | 561 | |
| 342 : | csouza | 39 | class PHPGettextAdmin |
| 343 : | { | ||
| 344 : | csouza | 556 | var $has_gettext = false; |
| 345 : | var $gettext_path = ''; | ||
| 346 : | csouza | 561 | var $debug = false; |
| 347 : | andphe | 882 | var $is_windows = false; |
| 348 : | csouza | 149 | |
| 349 : | csouza | 561 | function PHPGettextAdmin($debug = false) |
| 350 : | csouza | 556 | { |
| 351 : | andphe | 896 | if (ini_get('open_basedir') || ini_get('safe_mode') || strstr(ini_get("disable_functions"),"exec")) { |
| 352 : | csouza | 561 | $this->has_gettext = false; |
| 353 : | return false; | ||
| 354 : | csouza | 556 | } |
| 355 : | andphe | 882 | |
| 356 : | csouza | 556 | if (substr(strtoupper(PHP_OS), 0, 3) == 'WIN'){ |
| 357 : | andphe | 882 | $this->is_windows = true; |
| 358 : | csouza | 556 | } |
| 359 : | andphe | 882 | |
| 360 : | $cmd = 'xgettext --help'; | ||
| 361 : | csouza | 556 | exec($cmd, $output, $return); |
| 362 : | if ($output) { | ||
| 363 : | andphe | 882 | $this->has_gettext = true; |
| 364 : | csouza | 556 | } |
| 365 : | csouza | 561 | if ($debug) { |
| 366 : | $this->debug = $debug; | ||
| 367 : | } | ||
| 368 : | csouza | 556 | } |
| 369 : | |||
| 370 : | csouza | 39 | /** |
| 371 : | * Enter description here... | ||
| 372 : | * | ||
| 373 : | * @param unknown_type $domain | ||
| 374 : | * @param unknown_type $langdir | ||
| 375 : | */ | ||
| 376 : | csouza | 561 | function message_format ($domain, $textdomain, $lang, $enc='utf-8') |
| 377 : | { | ||
| 378 : | $path = "$textdomain/$lang"; | ||
| 379 : | csouza | 556 | if ($this->has_gettext) { |
| 380 : | andphe | 882 | $cmd = $this->escCommand("msgfmt")." -o ".$this->escPath("{$path}/LC_MESSAGES/{$domain}.mo")." ".$this->escPath("{$path}/{$domain}.po"); |
| 381 : | csouza | 556 | return $this->execute($cmd); |
| 382 : | } | ||
| 383 : | csouza | 561 | $catalog = new PHPGettext_catalog($domain, $textdomain); |
| 384 : | csouza | 556 | $catalog->setproperty('mode', _MODE_PO_); |
| 385 : | $catalog->setproperty('lang', $lang); | ||
| 386 : | $catalog->load(); | ||
| 387 : | $catalog->setproperty('mode', _MODE_MO_); | ||
| 388 : | $catalog->save(); | ||
| 389 : | return true; | ||
| 390 : | csouza | 39 | } |
| 391 : | /** | ||
| 392 : | * Enter description here... | ||
| 393 : | * | ||
| 394 : | * @param unknown_type $domain | ||
| 395 : | * @param unknown_type $langdir | ||
| 396 : | */ | ||
| 397 : | csouza | 561 | function compile($lang, $textdomain, $enc='utf-8') { |
| 398 : | $catalog = new PHPGettext_catalog($lang, $textdomain); | ||
| 399 : | csouza | 556 | $catalog->setproperty('mode', _MODE_PO_); |
| 400 : | csouza | 561 | $catalog->setproperty('lang', $lang); |
| 401 : | andphe | 913 | $headers = $this->header(); |
| 402 : | $catalog->setproperty('comments', $headers[0]); | ||
| 403 : | $catalog->setproperty('headers', $headers[1]); | ||
| 404 : | csouza | 556 | $catalog->load(); |
| 405 : | andphe | 882 | $d = dir($textdomain."/".$lang); |
| 406 : | while (false !== ($file = $d->read())){ | ||
| 407 : | if (preg_match('/.po$/', $file)){ | ||
| 408 : | list($file,$ext) = explode(".",$file); | ||
| 409 : | $catalog_aux = new PHPGettext_catalog($file, $textdomain); | ||
| 410 : | $catalog_aux->setproperty('mode', _MODE_PO_); | ||
| 411 : | $catalog_aux->setproperty('lang', $lang); | ||
| 412 : | $catalog_aux->load(); | ||
| 413 : | foreach ($catalog_aux->strings as $msgid => $string){ | ||
| 414 : | if (!$string->is_fuzzy){ | ||
| 415 : | andphe | 913 | if (is_array($string->msgstr) ){ |
| 416 : | if(in_array("",$string->msgstr)){ | ||
| 417 : | continue; | ||
| 418 : | } | ||
| 419 : | } else if (!$string->msgstr){ | ||
| 420 : | continue; | ||
| 421 : | } | ||
| 422 : | $catalog->addentry($string->msgid, $string->msgid_plural,$string->msgstr, $string->comments ); | ||
| 423 : | andphe | 882 | } |
| 424 : | } | ||
| 425 : | } | ||
| 426 : | } | ||
| 427 : | csouza | 556 | $catalog->setproperty('mode', _MODE_MO_); |
| 428 : | $catalog->save(); | ||
| 429 : | return true; | ||
| 430 : | } | ||
| 431 : | csouza | 561 | |
| 432 : | csouza | 556 | /** |
| 433 : | * Enter description here... | ||
| 434 : | * | ||
| 435 : | * @param unknown_type $domain | ||
| 436 : | * @param unknown_type $langdir | ||
| 437 : | */ | ||
| 438 : | csouza | 561 | function initialize_translation ($domain, $textdomain, $lang, $enc='utf-8') |
| 439 : | { | ||
| 440 : | if (!$this->has_gettext) { | ||
| 441 : | return false; | ||
| 442 : | csouza | 556 | } |
| 443 : | csouza | 561 | set_time_limit(120); |
| 444 : | $path = "$textdomain/$lang"; | ||
| 445 : | copy("$textdomain/untranslated/$domain.pot", "$path/ref.po"); | ||
| 446 : | andphe | 882 | $cmd = $this->escCommand("msgmerge")." --width=80 --compendium ".$this->escPath("{$textdomain}/glossary/{$lang}.{$enc}.po")." -o ".$this->escPath("{$path}/{$domain}.po")." ".$this->escPath("{$path}/ref.po")." ".$this->escPath("{$textdomain}/untranslated/{$domain}.pot"); |
| 447 : | csouza | 561 | $this->execute($cmd); |
| 448 : | unlink("$textdomain/$lang/ref.po"); | ||
| 449 : | csouza | 556 | } |
| 450 : | andphe | 896 | |
| 451 : | csouza | 556 | /** |
| 452 : | * Enter description here... | ||
| 453 : | * | ||
| 454 : | * @param unknown_type $domain | ||
| 455 : | * @param unknown_type $langdir | ||
| 456 : | */ | ||
| 457 : | andphe | 882 | |
| 458 : | csouza | 561 | function update_translation($domain, $textdomain, $lang, $enc='utf-8') |
| 459 : | csouza | 556 | { |
| 460 : | andphe | 887 | if (!file_exists("$textdomain/glossary/$lang.$enc.po")) return false; |
| 461 : | andphe | 882 | $catalog_aux = new PHPGettext_catalog($lang.".".$enc, $textdomain); |
| 462 : | $catalog_aux->setproperty('mode', _MODE_GLO_); | ||
| 463 : | $catalog_aux->setproperty('lang', $lang); | ||
| 464 : | $catalog_aux->load(); | ||
| 465 : | foreach ($catalog_aux->strings as $msgid => $string){ | ||
| 466 : | if (!$string->is_fuzzy){ | ||
| 467 : | $trans[$string->msgid] = $string->msgstr; | ||
| 468 : | } | ||
| 469 : | csouza | 561 | } |
| 470 : | andphe | 882 | $catalog = new PHPGettext_catalog($domain, $textdomain); |
| 471 : | $catalog->setproperty('mode', _MODE_PO_); | ||
| 472 : | $catalog->setproperty('lang', $lang); | ||
| 473 : | $catalog->load(); | ||
| 474 : | enjoyman | 903 | global $mapcharset; |
| 475 : | $charsets = explode("=",$catalog->headers["Content-Type"]); | ||
| 476 : | $codecharset = str_replace("\\n","",strtolower($charsets[1])); | ||
| 477 : | $NewEncoding = new ConvertCharset(); | ||
| 478 : | foreach ($trans as $key => $tran){ | ||
| 479 : | if(trim($mapcharset[$codecharset])!="utf-8") | ||
| 480 : | $trans[$key] = $NewEncoding->Convert($tran,"utf-8",trim($mapcharset[$codecharset]),false); | ||
| 481 : | } | ||
| 482 : | andphe | 882 | $catalog->translate($trans ); |
| 483 : | $catalog->save(); | ||
| 484 : | return true; | ||
| 485 : | csouza | 556 | } |
| 486 : | andphe | 882 | |
| 487 : | csouza | 556 | /** |
| 488 : | * Enter description here... | ||
| 489 : | * | ||
| 490 : | * @param unknown_type $domain | ||
| 491 : | * @param unknown_type $langdir | ||
| 492 : | */ | ||
| 493 : | andphe | 896 | |
| 494 : | csouza | 561 | function add_to_dict($domain, $textdomain, $lang, $enc='utf-8') { |
| 495 : | $textdomain = rtrim($textdomain, '\/'); | ||
| 496 : | $path = "$textdomain/$lang"; | ||
| 497 : | andphe | 896 | |
| 498 : | csouza | 561 | if (!is_dir("$textdomain/glossary/")) { |
| 499 : | mkdir("$textdomain/glossary/"); | ||
| 500 : | } | ||
| 501 : | andphe | 896 | |
| 502 : | $catalog = new PHPGettext_catalog($domain, $textdomain); | ||
| 503 : | $catalog->setproperty('mode', _MODE_PO_); | ||
| 504 : | $catalog->setproperty('lang', $lang); | ||
| 505 : | $catalog->setproperty('charset', $enc); | ||
| 506 : | $catalog->load(); | ||
| 507 : | |||
| 508 : | foreach ($catalog->strings as $msgid => $string){ | ||
| 509 : | if (!$string->is_fuzzy){ | ||
| 510 : | if (is_array($string->msgstr) ){ | ||
| 511 : | if(in_array("",$string->msgstr)){ | ||
| 512 : | continue; | ||
| 513 : | } | ||
| 514 : | } else if (!$string->msgstr){ | ||
| 515 : | continue; | ||
| 516 : | } | ||
| 517 : | $new[$string->msgid] = $string; | ||
| 518 : | } | ||
| 519 : | } | ||
| 520 : | |||
| 521 : | $glossary = new PHPGettext_catalog($lang.".".$enc, $textdomain); | ||
| 522 : | $glossary->setproperty('mode', _MODE_GLO_); | ||
| 523 : | $glossary->setproperty('lang', $lang); | ||
| 524 : | csouza | 561 | if (!file_exists("$textdomain/glossary/$lang.$enc.po")) { |
| 525 : | andphe | 896 | $headers = $this->header(); |
| 526 : | $glossary->setproperty('comments', $headers[0]); | ||
| 527 : | $glossary->setproperty('headers', $headers[1]); | ||
| 528 : | $glossary->save(); | ||
| 529 : | }else{ | ||
| 530 : | $glossary->load(); | ||
| 531 : | csouza | 561 | } |
| 532 : | andphe | 896 | |
| 533 : | $glossary->merge($new ); | ||
| 534 : | $glossary->save(); | ||
| 535 : | return true; | ||
| 536 : | csouza | 561 | } |
| 537 : | /** | ||
| 538 : | * Enter description here... | ||
| 539 : | * | ||
| 540 : | * @param unknown_type $domain | ||
| 541 : | * @param unknown_type $langdir | ||
| 542 : | */ | ||
| 543 : | function convert_charset($domain, $textdomain, $lang, $from_charset, $to_charset) { | ||
| 544 : | |||
| 545 : | $path = "$textdomain/$lang"; | ||
| 546 : | csouza | 556 | if ($this->has_gettext) { |
| 547 : | andphe | 882 | $cmd = $this->escCommand("msgconv")." --to-code=$to_charset -o ".$this->escPath("{$path}/{$domain}.po")." ".$this->escPath("{$path}/{$domain}.po"); |
| 548 : | csouza | 556 | $ret = $this->execute($cmd); |
| 549 : | return $ret; | ||
| 550 : | } | ||
| 551 : | |||
| 552 : | enjoyman | 866 | if (!class_exists('ConvertCharset')) { |
| 553 : | csouza | 556 | return false; |
| 554 : | } | ||
| 555 : | |||
| 556 : | csouza | 561 | $catalog = new PHPGettext_catalog($domain, $textdomain); |
| 557 : | csouza | 556 | $catalog->setproperty('mode', _MODE_PO_); |
| 558 : | $catalog->setproperty('lang', $lang); | ||
| 559 : | $catalog->load(); | ||
| 560 : | $catalog->headers['Content-Type'] = "text/plain; charset=$to_charset\n"; | ||
| 561 : | enjoyman | 903 | $NewEncoding = new ConvertCharset(); |
| 562 : | foreach ($catalog->strings as $index => $message) { | ||
| 563 : | csouza | 556 | if (empty($message->msgid_plural)) { |
| 564 : | enjoyman | 866 | $catalog->strings[$index]->msgstr = $NewEncoding->Convert($message->msgstr,$from_charset,$to_charset,false); |
| 565 : | csouza | 556 | } |
| 566 : | } | ||
| 567 : | $catalog->save(); | ||
| 568 : | return true; | ||
| 569 : | } | ||
| 570 : | |||
| 571 : | |||
| 572 : | /** | ||
| 573 : | csouza | 39 | * Invoke the xgettext utility with $args |
| 574 : | * the xgettext executable must be in PATH | ||
| 575 : | * | ||
| 576 : | * @param string the commandline arguments to gettext | ||
| 577 : | * @return unknown | ||
| 578 : | */ | ||
| 579 : | csouza | 561 | function xgettext($domain, $textdomain, $php_sources, $lang='untranslated') { |
| 580 : | andphe | 882 | $path = mamboCore::get('rootPath'); |
| 581 : | $n=count($php_sources); | ||
| 582 : | if (!$n) return false; | ||
| 583 : | $cmd = $this->escCommand("xgettext"); | ||
| 584 : | if (file_exists("$textdomain/$lang/$domain.pot")) $cmd .= ' -j '; | ||
| 585 : | $cmd .= " -n -c --sort-by-file --keyword=T_ --keyword=Tn_:1,2 --keyword=Td_:2 --keyword=Tdn_:2,3 --output-dir=".$this->escPath("{$textdomain}/{$lang}")." -o {$domain}.pot"; | ||
| 586 : | csouza | 561 | if (count($php_sources) > 10) { |
| 587 : | andphe | 882 | $tmp_name = substr(uniqid(),0,8).".txt"; |
| 588 : | $tmpfile = $path."/media/$tmp_name"; | ||
| 589 : | csouza | 561 | $fp = fopen($tmpfile, "w"); |
| 590 : | fwrite($fp, implode("\r\n", $php_sources)); | ||
| 591 : | fclose($fp); | ||
| 592 : | andphe | 882 | $cmd = $cmd." --files-from=".$this->escPath($tmpfile); |
| 593 : | csouza | 561 | } else { |
| 594 : | andphe | 882 | for($i=0;$i<$n;$i++){ |
| 595 : | $php_sources[$i] = $this->escPath($php_sources[$i]); | ||
| 596 : | } | ||
| 597 : | csouza | 561 | $cmd = $cmd." ".implode(" ", $php_sources); |
| 598 : | neilt | 686 | } |
| 599 : | csouza | 561 | $ret = $this->execute($cmd); |
| 600 : | @unlink($tmpfile); | ||
| 601 : | andphe | 882 | |
| 602 : | csouza | 561 | return $ret; |
| 603 : | csouza | 149 | } |
| 604 : | |||
| 605 : | andphe | 882 | |
| 606 : | csouza | 39 | /** |
| 607 : | * Enter description here... | ||
| 608 : | * | ||
| 609 : | * @param unknown_type $domain | ||
| 610 : | * @param unknown_type $langdir | ||
| 611 : | */ | ||
| 612 : | csouza | 149 | function execute($cmd) { |
| 613 : | csouza | 39 | |
| 614 : | csouza | 561 | if (!$this || !$this->has_gettext) return false; |
| 615 : | andphe | 882 | |
| 616 : | $lastline = exec($cmd, $output, $retval); | ||
| 617 : | csouza | 561 | if ($this->debug || $retval > 0) { |
| 618 : | $trace = debug_backtrace(); | ||
| 619 : | $msg = "<div style=\"border: thin solid silver; text-align:left;margin-left:10px\">"; | ||
| 620 : | $msg .= "<p><span style=\"font-weight:bold\">PHPGettextAdmin->execute('</span><i>$cmd</i><span style=\"font-weight:bold\">')</span><br/>in ".str_replace($_SERVER['DOCUMENT_ROOT'], '', $trace[1]['file']).":{$trace[1]['line']}</p>"; | ||
| 621 : | $msg .= "<p><i><b>trace:</b></i></p><ul>"; | ||
| 622 : | for ($a=1; $a < 3; $a++) { | ||
| 623 : | $called = isset($trace[$a]['class']) ? $trace[$a]['class'] : ''; | ||
| 624 : | $called .= $trace[$a]['type'].$trace[$a]['function']."(".@implode(', ',$trace[$a]['args']).")"; | ||
| 625 : | $msg .= "<li>$called in ".str_replace($_SERVER['DOCUMENT_ROOT'], '', $trace[$a]['file'])." : {$trace[$a]['line']}</li>"; | ||
| 626 : | } | ||
| 627 : | $msg .= "</ul>"; | ||
| 628 : | $msg .= "<p><i><b>return value:</b></i> $retval</p>"; | ||
| 629 : | andphe | 882 | $msg .= "<p><i><b>output:</b></i> </p><pre>".implode("<br/>",$output)."</pre>"; |
| 630 : | csouza | 561 | $msg .= "</div>"; |
| 631 : | echo $msg; | ||
| 632 : | } | ||
| 633 : | andphe | 882 | return $retval == 0; |
| 634 : | csouza | 39 | } |
| 635 : | andphe | 882 | |
| 636 : | function escCommand($command){ | ||
| 637 : | if ($this->is_windows){ | ||
| 638 : | $command = "call \"{$command}\""; | ||
| 639 : | } | ||
| 640 : | return $command; | ||
| 641 : | } | ||
| 642 : | |||
| 643 : | function escPath($path){ | ||
| 644 : | if ($this->is_windows){ | ||
| 645 : | $path = "\"".str_replace("/","\\",$path)."\""; | ||
| 646 : | } | ||
| 647 : | return $path; | ||
| 648 : | } | ||
| 649 : | andphe | 896 | |
| 650 : | function header($charset='utf-8', $plurals='nplurals=2; plural=n == 1 ? 0 : 1;'){ | ||
| 651 : | $year = date('Y'); | ||
| 652 : | $comments = <<<EOT | ||
| 653 : | # Mambo Open Source. | ||
| 654 : | # Copyright (C) 2005 - $year Mambo Foundation Inc. | ||
| 655 : | # This file is distributed under the same license as the Mambo package. | ||
| 656 : | # Translation Team <translation@mambo-foundation.org>, $year# | ||
| 657 : | # | ||
| 658 : | #, fuzzy | ||
| 659 : | EOT; | ||
| 660 : | $comments = explode("\n", $comments); | ||
| 661 : | $headers = array( | ||
| 662 : | 'Project-Id-Version' => 'Mambo 4.6', | ||
| 663 : | 'Report-Msgid-Bugs-To' => 'translation@mambo-foundation.org', | ||
| 664 : | 'POT-Creation-Date' => date('Y-m-d h:iO'), | ||
| 665 : | 'PO-Revision-Date' => date('Y-m-d h:iO'), | ||
| 666 : | 'Last-Translator' => 'Translation <translation@mambo-foundation.org>', | ||
| 667 : | 'Language-Team' => 'Translation <translation@mambo-foundation.org>', | ||
| 668 : | 'MIME-Version' => '1.0', | ||
| 669 : | 'Content-Type' => 'text/plain; charset='.$charset, | ||
| 670 : | 'Content-Transfer-Encoding' => '8bit', | ||
| 671 : | 'Plural-Forms' => $plurals | ||
| 672 : | ); | ||
| 673 : | return array($comments, $headers); | ||
| 674 : | } | ||
| 675 : | csouza | 39 | } |
| 676 : | |||
| 677 : | /** | ||
| 678 : | * Enter description here... | ||
| 679 : | * | ||
| 680 : | * @return unknown | ||
| 681 : | */ | ||
| 682 : | function &phpgettext(){ | ||
| 683 : | static $gettext; | ||
| 684 : | andphe | 896 | $root = dirname(__FILE__); |
| 685 : | csouza | 149 | if (is_null($gettext)) { |
| 686 : | andphe | 896 | require_once($root.'/phpgettext.class.php'); |
| 687 : | csouza | 149 | $gettext = new PHPGettext(); |
| 688 : | csouza | 175 | $gettext->has_gettext = true; |
| 689 : | csouza | 39 | if (!function_exists("gettext") || !function_exists("_")) { |
| 690 : | $gettext->has_gettext = false; | ||
| 691 : | andphe | 896 | require_once($root.'/phpgettext.compat.php'); |
| 692 : | csouza | 39 | } |
| 693 : | } | ||
| 694 : | return $gettext; | ||
| 695 : | } | ||
| 696 : | function T_($message) { | ||
| 697 : | $gettext =& phpgettext(); | ||
| 698 : | csouza | 154 | $trans = $gettext->gettext($message); |
| 699 : | csouza | 153 | return $trans; |
| 700 : | csouza | 39 | } |
| 701 : | function Tn_($msg1, $msg2, $count) { | ||
| 702 : | $gettext =& phpgettext(); | ||
| 703 : | return $gettext->ngettext($msg1, $msg2, $count); | ||
| 704 : | } | ||
| 705 : | csouza | 149 | function Td_($domain, $message) { |
| 706 : | csouza | 39 | $gettext =& phpgettext(); |
| 707 : | return $gettext->dgettext($domain, $message); | ||
| 708 : | } | ||
| 709 : | csouza | 149 | function Tdn_($domain, $msg1, $msg2, $count) { |
| 710 : | csouza | 39 | $gettext =& phpgettext(); |
| 711 : | return $gettext->dngettext($domain, $msg1, $msg2, $count); | ||
| 712 : | } | ||
| 713 : | ?> |
| ViewVC Help | |
| Powered by ViewVC 1.0.0 |
Web Hosting provided by Network Redux.

