]> git.mxchange.org Git - friendica-addons.git/blob - jappixmini/jappix/php/gettext.php
Merge branch '3.6-release'
[friendica-addons.git] / jappixmini / jappix / php / gettext.php
1 <?php
2
3 /*
4    Copyright (c) 2003, 2005, 2006, 2009 Danilo Segan <danilo@kvota.net>.
5
6    This file is part of PHP-gettext.
7
8    PHP-gettext is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12
13    PHP-gettext is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with PHP-gettext; if not, write to the Free Software
20    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21
22 */
23
24 /**
25  * MODIFIED FOR THE JAPPIX PROJECT
26  * Last revision: 10/11/10
27  */
28
29   // Simple class to wrap file streams, string streams, etc.
30   // seek is essential, and it should be byte stream
31 class StreamReader {
32   // should return a string [FIXME: perhaps return array of bytes?]
33   function read($bytes) {
34     return false;
35   }
36
37   // should return new position
38   function seekto($position) {
39     return false;
40   }
41
42   // returns current position
43   function currentpos() {
44     return false;
45   }
46
47   // returns length of entire stream (limit for seekto()s)
48   function length() {
49     return false;
50   }
51 };
52
53 class StringReader {
54   var $_pos;
55   var $_str;
56
57   function StringReader($str='') {
58     $this->_str = $str;
59     $this->_pos = 0;
60   }
61
62   function read($bytes) {
63     $data = substr($this->_str, $this->_pos, $bytes);
64     $this->_pos += $bytes;
65     if (strlen($this->_str)<$this->_pos)
66       $this->_pos = strlen($this->_str);
67
68     return $data;
69   }
70
71   function seekto($pos) {
72     $this->_pos = $pos;
73     if (strlen($this->_str)<$this->_pos)
74       $this->_pos = strlen($this->_str);
75     return $this->_pos;
76   }
77
78   function currentpos() {
79     return $this->_pos;
80   }
81
82   function length() {
83     return strlen($this->_str);
84   }
85
86 };
87
88
89 class FileReader {
90   var $_pos;
91   var $_fd;
92   var $_length;
93
94   function FileReader($filename) {
95     if (file_exists($filename)) {
96
97       $this->_length=filesize($filename);
98       $this->_pos = 0;
99       $this->_fd = fopen($filename,'rb');
100       if (!$this->_fd) {
101         $this->error = 3; // Cannot read file, probably permissions
102         return false;
103       }
104     } else {
105       $this->error = 2; // File doesn't exist
106       return false;
107     }
108   }
109
110   function read($bytes) {
111     if ($bytes) {
112       fseek($this->_fd, $this->_pos);
113
114       // PHP 5.1.1 does not read more than 8192 bytes in one fread()
115       // the discussions at PHP Bugs suggest it's the intended behaviour
116       $data = '';
117       while ($bytes > 0) {
118         $chunk  = fread($this->_fd, $bytes);
119         $data  .= $chunk;
120         $bytes -= strlen($chunk);
121       }
122       $this->_pos = ftell($this->_fd);
123
124       return $data;
125     } else return '';
126   }
127
128   function seekto($pos) {
129     fseek($this->_fd, $pos);
130     $this->_pos = ftell($this->_fd);
131     return $this->_pos;
132   }
133
134   function currentpos() {
135     return $this->_pos;
136   }
137
138   function length() {
139     return $this->_length;
140   }
141
142   function close() {
143     fclose($this->_fd);
144   }
145
146 };
147
148 // Preloads entire file in memory first, then creates a StringReader
149 // over it (it assumes knowledge of StringReader internals)
150 class CachedFileReader extends StringReader {
151   function CachedFileReader($filename) {
152     if (file_exists($filename)) {
153
154       $length=filesize($filename);
155       $fd = fopen($filename,'rb');
156
157       if (!$fd) {
158         $this->error = 3; // Cannot read file, probably permissions
159         return false;
160       }
161       $this->_str = fread($fd, $length);
162       fclose($fd);
163
164     } else {
165       $this->error = 2; // File doesn't exist
166       return false;
167     }
168   }
169 };
170
171 /*
172    Copyright (c) 2003, 2009 Danilo Segan <danilo@kvota.net>.
173    Copyright (c) 2005 Nico Kaiser <nico@siriux.net>
174
175    This file is part of PHP-gettext.
176
177    PHP-gettext is free software; you can redistribute it and/or modify
178    it under the terms of the GNU General Public License as published by
179    the Free Software Foundation; either version 2 of the License, or
180    (at your option) any later version.
181
182    PHP-gettext is distributed in the hope that it will be useful,
183    but WITHOUT ANY WARRANTY; without even the implied warranty of
184    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
185    GNU General Public License for more details.
186
187    You should have received a copy of the GNU General Public License
188    along with PHP-gettext; if not, write to the Free Software
189    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
190
191 */
192
193 /**
194  * Provides a simple gettext replacement that works independently from
195  * the system's gettext abilities.
196  * It can read MO files and use them for translating strings.
197  * The files are passed to gettext_reader as a Stream (see streams.php)
198  *
199  * This version has the ability to cache all strings and translations to
200  * speed up the string lookup.
201  * While the cache is enabled by default, it can be switched off with the
202  * second parameter in the constructor (e.g. whenusing very large MO files
203  * that you don't want to keep in memory)
204  */
205 class gettext_reader {
206   //public:
207    var $error = 0; // public variable that holds error code (0 if no error)
208
209    //private:
210   var $BYTEORDER = 0;        // 0: low endian, 1: big endian
211   var $STREAM = NULL;
212   var $short_circuit = false;
213   var $enable_cache = false;
214   var $originals = NULL;      // offset of original table
215   var $translations = NULL;    // offset of translation table
216   var $pluralheader = NULL;    // cache header field for plural forms
217   var $total = 0;          // total string count
218   var $table_originals = NULL;  // table for original strings (offsets)
219   var $table_translations = NULL;  // table for translated strings (offsets)
220   var $cache_translations = NULL;  // original -> translation mapping
221
222
223   /* Methods */
224
225
226   /**
227    * Reads a 32bit Integer from the Stream
228    *
229    * @access private
230    * @return Integer from the Stream
231    */
232   function readint() {
233       if ($this->BYTEORDER == 0) {
234         // low endian
235         $input=unpack('V', $this->STREAM->read(4));
236         return array_shift($input);
237       } else {
238         // big endian
239         $input=unpack('N', $this->STREAM->read(4));
240         return array_shift($input);
241       }
242     }
243
244   function read($bytes) {
245     return $this->STREAM->read($bytes);
246   }
247
248   /**
249    * Reads an array of Integers from the Stream
250    *
251    * @param int count How many elements should be read
252    * @return Array of Integers
253    */
254   function readintarray($count) {
255     if ($this->BYTEORDER == 0) {
256         // low endian
257         return unpack('V'.$count, $this->STREAM->read(4 * $count));
258       } else {
259         // big endian
260         return unpack('N'.$count, $this->STREAM->read(4 * $count));
261       }
262   }
263
264   /**
265    * Constructor
266    *
267    * @param object Reader the StreamReader object
268    * @param boolean enable_cache Enable or disable caching of strings (default on)
269    */
270   function gettext_reader($Reader, $enable_cache = true) {
271     // If there isn't a StreamReader, turn on short circuit mode.
272     if (! $Reader || isset($Reader->error) ) {
273       $this->short_circuit = true;
274       return;
275     }
276
277     // Caching can be turned off
278     $this->enable_cache = $enable_cache;
279
280     $MAGIC1 = "\x95\x04\x12\xde";
281     $MAGIC2 = "\xde\x12\x04\x95";
282
283     $this->STREAM = $Reader;
284     $magic = $this->read(4);
285     if ($magic == $MAGIC1) {
286       $this->BYTEORDER = 1;
287     } elseif ($magic == $MAGIC2) {
288       $this->BYTEORDER = 0;
289     } else {
290       $this->error = 1; // not MO file
291       return false;
292     }
293
294     // FIXME: Do we care about revision? We should.
295     $revision = $this->readint();
296
297     $this->total = $this->readint();
298     $this->originals = $this->readint();
299     $this->translations = $this->readint();
300   }
301
302   /**
303    * Loads the translation tables from the MO file into the cache
304    * If caching is enabled, also loads all strings into a cache
305    * to speed up translation lookups
306    * 
307    * @access private
308    */
309   function load_tables() {
310     if (is_array($this->cache_translations) &&
311       is_array($this->table_originals) &&
312       is_array($this->table_translations))
313       return;
314
315     /* get original and translations tables */
316     $this->STREAM->seekto($this->originals);
317     $this->table_originals = $this->readintarray($this->total * 2);
318     $this->STREAM->seekto($this->translations);
319     $this->table_translations = $this->readintarray($this->total * 2);
320
321     if ($this->enable_cache) {
322       $this->cache_translations = array ();
323       /* read all strings in the cache */
324       for ($i = 0; $i < $this->total; $i++) {
325         $this->STREAM->seekto($this->table_originals[$i * 2 + 2]);
326         $original = $this->STREAM->read($this->table_originals[$i * 2 + 1]);
327         $this->STREAM->seekto($this->table_translations[$i * 2 + 2]);
328         $translation = $this->STREAM->read($this->table_translations[$i * 2 + 1]);
329         $this->cache_translations[$original] = $translation;
330       }
331     }
332   }
333
334   /**
335    * Returns a string from the "originals" table
336    *
337    * @access private
338    * @param int num Offset number of original string
339    * @return string Requested string if found, otherwise ''
340    */
341   function get_original_string($num) {
342     $length = $this->table_originals[$num * 2 + 1];
343     $offset = $this->table_originals[$num * 2 + 2];
344     if (! $length)
345       return '';
346     $this->STREAM->seekto($offset);
347     $data = $this->STREAM->read($length);
348     return (string)$data;
349   }
350
351   /**
352    * Returns a string from the "translations" table
353    *
354    * @access private
355    * @param int num Offset number of original string
356    * @return string Requested string if found, otherwise ''
357    */
358   function get_translation_string($num) {
359     $length = $this->table_translations[$num * 2 + 1];
360     $offset = $this->table_translations[$num * 2 + 2];
361     if (! $length)
362       return '';
363     $this->STREAM->seekto($offset);
364     $data = $this->STREAM->read($length);
365     return (string)$data;
366   }
367
368   /**
369    * Binary search for string
370    *
371    * @access private
372    * @param string string
373    * @param int start (internally used in recursive function)
374    * @param int end (internally used in recursive function)
375    * @return int string number (offset in originals table)
376    */
377   function find_string($string, $start = -1, $end = -1) {
378     if (($start == -1) or ($end == -1)) {
379       // find_string is called with only one parameter, set start end end
380       $start = 0;
381       $end = $this->total;
382     }
383     if (abs($start - $end) <= 1) {
384       // We're done, now we either found the string, or it doesn't exist
385       $txt = $this->get_original_string($start);
386       if ($string == $txt)
387         return $start;
388       else
389         return -1;
390     } else if ($start > $end) {
391       // start > end -> turn around and start over
392       return $this->find_string($string, $end, $start);
393     } else {
394       // Divide table in two parts
395       $half = (int)(($start + $end) / 2);
396       $cmp = strcmp($string, $this->get_original_string($half));
397       if ($cmp == 0)
398         // string is exactly in the middle => return it
399         return $half;
400       else if ($cmp < 0)
401         // The string is in the upper half
402         return $this->find_string($string, $start, $half);
403       else
404         // The string is in the lower half
405         return $this->find_string($string, $half, $end);
406     }
407   }
408
409   /**
410    * Translates a string
411    *
412    * @access public
413    * @param string string to be translated
414    * @return string translated string (or original, if not found)
415    */
416   function translate($string) {
417     if ($this->short_circuit)
418       return $string;
419     $this->load_tables();
420
421     if ($this->enable_cache) {
422       // Caching enabled, get translated string from cache
423       if (array_key_exists($string, $this->cache_translations))
424         return $this->cache_translations[$string];
425       else
426         return $string;
427     } else {
428       // Caching not enabled, try to find string
429       $num = $this->find_string($string);
430       if ($num == -1)
431         return $string;
432       else
433         return $this->get_translation_string($num);
434     }
435   }
436
437   /**
438    * Sanitize plural form expression for use in PHP eval call.
439    *
440    * @access private
441    * @return string sanitized plural form expression
442    */
443   function sanitize_plural_expression($expr) {
444     // Get rid of disallowed characters.
445     $expr = preg_replace('@[^a-zA-Z0-9_:;\(\)\?\|\&=!<>+*/\%-]@', '', $expr);
446
447     // Add parenthesis for tertiary '?' operator.
448     $expr .= ';';
449     $res = '';
450     $p = 0;
451     for ($i = 0; $i < strlen($expr); $i++) {
452       $ch = $expr[$i];
453       switch ($ch) {
454       case '?':
455         $res .= ' ? (';
456         $p++;
457         break;
458       case ':':
459         $res .= ') : (';
460         break;
461       case ';':
462         $res .= str_repeat( ')', $p) . ';';
463         $p = 0;
464         break;
465       default:
466         $res .= $ch;
467       }
468     }
469     return $res;
470   }
471
472   /**
473    * Get possible plural forms from MO header
474    *
475    * @access private
476    * @return string plural form header
477    */
478   function get_plural_forms() {
479     // lets assume message number 0 is header
480     // this is true, right?
481     $this->load_tables();
482
483     // cache header field for plural forms
484     if (! is_string($this->pluralheader)) {
485       if ($this->enable_cache) {
486         $header = $this->cache_translations[""];
487       } else {
488         $header = $this->get_translation_string(0);
489       }
490       if (eregi("plural-forms: ([^\n]*)\n", $header, $regs))
491         $expr = $regs[1];
492       else
493         $expr = "nplurals=2; plural=n == 1 ? 0 : 1;";
494
495       $this->pluralheader = $this->sanitize_plural_expression($expr);
496     }
497     return $this->pluralheader;
498   }
499
500   /**
501    * Detects which plural form to take
502    *
503    * @access private
504    * @param n count
505    * @return int array index of the right plural form
506    */
507   function select_string($n) {
508     $string = $this->get_plural_forms();
509     $string = str_replace('nplurals',"\$total",$string);
510     $string = str_replace("n",$n,$string);
511     $string = str_replace('plural',"\$plural",$string);
512
513     $total = 0;
514     $plural = 0;
515
516     eval("$string");
517     if ($plural >= $total) $plural = $total - 1;
518     return $plural;
519   }
520
521   /**
522    * Plural version of gettext
523    *
524    * @access public
525    * @param string single
526    * @param string plural
527    * @param string number
528    * @return translated plural form
529    */
530   function ngettext($single, $plural, $number) {
531     if ($this->short_circuit) {
532       if ($number != 1)
533         return $plural;
534       else
535         return $single;
536     }
537
538     // find out the appropriate form
539     $select = $this->select_string($number);
540
541     // this should contains all strings separated by NULLs
542     $key = $single.chr(0).$plural;
543
544
545     if ($this->enable_cache) {
546       if (! array_key_exists($key, $this->cache_translations)) {
547         return ($number != 1) ? $plural : $single;
548       } else {
549         $result = $this->cache_translations[$key];
550         $list = explode(chr(0), $result);
551         return $list[$select];
552       }
553     } else {
554       $num = $this->find_string($key);
555       if ($num == -1) {
556         return ($number != 1) ? $plural : $single;
557       } else {
558         $result = $this->get_translation_string($num);
559         $list = explode(chr(0), $result);
560         return $list[$select];
561       }
562     }
563   }
564
565 }
566
567 /*
568    Copyright (c) 2005 Steven Armstrong <sa at c-area dot ch>
569    Copyright (c) 2009 Danilo Segan <danilo@kvota.net>
570
571    Drop in replacement for native gettext.
572
573    This file is part of PHP-gettext.
574
575    PHP-gettext is free software; you can redistribute it and/or modify
576    it under the terms of the GNU General Public License as published by
577    the Free Software Foundation; either version 2 of the License, or
578    (at your option) any later version.
579
580    PHP-gettext is distributed in the hope that it will be useful,
581    but WITHOUT ANY WARRANTY; without even the implied warranty of
582    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
583    GNU General Public License for more details.
584
585    You should have received a copy of the GNU General Public License
586    along with PHP-gettext; if not, write to the Free Software
587    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
588
589 */
590 /*
591 LC_CTYPE        0
592 LC_NUMERIC      1
593 LC_TIME         2
594 LC_COLLATE      3
595 LC_MONETARY     4
596 LC_MESSAGES     5
597 LC_ALL          6
598 */
599
600
601 // LC_MESSAGES is not available if php-gettext is not loaded
602 // while the other constants are already available from session extension.
603 if (!defined('LC_MESSAGES')) {
604   define('LC_MESSAGES', 5);
605 }
606
607 // Variables
608
609 global $text_domains, $default_domain, $LC_CATEGORIES, $EMULATEGETTEXT, $CURRENTLOCALE;
610 $text_domains = array();
611 $default_domain = 'messages';
612 $LC_CATEGORIES = array('LC_CTYPE', 'LC_NUMERIC', 'LC_TIME', 'LC_COLLATE', 'LC_MONETARY', 'LC_MESSAGES', 'LC_ALL');
613 $EMULATEGETTEXT = 0;
614 $CURRENTLOCALE = '';
615
616 /* Class to hold a single domain included in $text_domains. */
617 class domain {
618   var $l10n;
619   var $path;
620   var $codeset;
621 }
622
623 // Utility functions
624
625 /**
626  * Utility function to get a StreamReader for the given text domain.
627  */
628 function _get_reader($domain=null, $category=5, $enable_cache=true) {
629     global $text_domains, $default_domain, $LC_CATEGORIES;
630     if (!isset($domain)) $domain = $default_domain;
631     if (!isset($text_domains[$domain]->l10n)) {
632         // get the current locale
633         $locale = _setlocale(LC_MESSAGES, 0);
634         $bound_path = isset($text_domains[$domain]->path) ?
635           $text_domains[$domain]->path : './';
636         $subpath = $LC_CATEGORIES[$category] ."/$domain.mo";
637         /* Figure out all possible locale names and start with the most
638            specific ones.  I.e. for sr_CS.UTF-8@latin, look through all of
639            sr_CS.UTF-8@latin, sr_CS@latin, sr@latin, sr_CS.UTF-8, sr_CS, sr.
640         */
641         $locale_names = array();
642         if (preg_match("/([a-z]{2,3})"            // language code
643                        ."(_([A-Z]{2}))?"          // country code
644                        ."(\.([-A-Za-z0-9_]))?"    // charset
645                        ."(@([-A-Za-z0-9_]+))?/",  // @ modifier
646                        $locale, $matches)) {
647           
648           $lang = '';
649           $country = '';
650           $charset = '';
651           $modifier = '';
652           
653           if(isset($matches[1]))
654              $lang = $matches[1];
655           if(isset($matches[3]))
656              $country = $matches[3];
657           if(isset($matches[5]))
658              $charset = $matches[5];
659           if(isset($matches[7]))
660              $modifier = $matches[7];
661           
662           if ($modifier) {
663             $locale_names = array("${lang}_$country.$charset@$modifier",
664                                   "${lang}_$country@$modifier",
665                                   "$lang@$modifier");
666           }
667           array_push($locale_names,
668                      "${lang}_$country.$charset", "${lang}_$country", "$lang");
669         }
670         array_push($locale_names, $locale);
671
672         $input = null;
673         foreach ($locale_names as $locale) {
674           $full_path = $bound_path . $locale . "/" . $subpath;
675           if (file_exists($full_path)) {
676             $input = new FileReader($full_path);
677             break;
678           }
679         }
680
681         if (!array_key_exists($domain, $text_domains)) {
682           // Initialize an empty domain object.
683           $text_domains[$domain] = new domain();
684         }
685         $text_domains[$domain]->l10n = new gettext_reader($input,
686                                                           $enable_cache);
687     }
688     return $text_domains[$domain]->l10n;
689 }
690
691 /**
692  * Returns whether we are using our emulated gettext API or PHP built-in one.
693  */
694 function locale_emulation() {
695     global $EMULATEGETTEXT;
696     return $EMULATEGETTEXT;
697 }
698
699 /**
700  * Checks if the current locale is supported on this system.
701  */
702 function _check_locale() {
703     global $EMULATEGETTEXT;
704     return !$EMULATEGETTEXT;
705 }
706
707 /**
708  * Get the codeset for the given domain.
709  */
710 function _get_codeset($domain=null) {
711     global $text_domains, $default_domain, $LC_CATEGORIES;
712     if (!isset($domain)) $domain = $default_domain;
713     return (isset($text_domains[$domain]->codeset))? $text_domains[$domain]->codeset : ini_get('mbstring.internal_encoding');
714 }
715
716 /**
717  * Convert the given string to the encoding set by bind_textdomain_codeset.
718  */
719 function _encode($text) {
720     $source_encoding = mb_detect_encoding($text);
721     $target_encoding = _get_codeset();
722     if ($source_encoding != $target_encoding) {
723         return mb_convert_encoding($text, $target_encoding, $source_encoding);
724     }
725     else {
726         return $text;
727     }
728 }
729
730
731
732
733 // Custom implementation of the standard gettext related functions
734
735 /**
736  * Sets a requested locale, if needed emulates it.
737  */
738 function _setlocale($category, $locale) {
739     global $CURRENTLOCALE, $EMULATEGETTEXT;
740     if ($locale === 0) { // use === to differentiate between string "0"
741         if ($CURRENTLOCALE != '')
742             return $CURRENTLOCALE;
743         else
744             // obey LANG variable, maybe extend to support all of LC_* vars
745             // even if we tried to read locale without setting it first
746             return _setlocale($category, $CURRENTLOCALE);
747     } else {
748         $ret = 0;
749         if (function_exists('setlocale')) // I don't know if this ever happens ;)
750            $ret = setlocale($category, $locale);
751         if (($ret and $locale == '') or ($ret == $locale)) {
752             $EMULATEGETTEXT = 0;
753             $CURRENTLOCALE = $ret;
754         } else {
755           if ($locale == '') // emulate variable support
756              $CURRENTLOCALE = getenv('LANG');
757         else
758             $CURRENTLOCALE = $locale;
759             $EMULATEGETTEXT = 1;
760         }
761         // Allow locale to be changed on the go for one translation domain.
762         global $text_domains, $default_domain;
763         unset($text_domains[$default_domain]->l10n);
764         return $CURRENTLOCALE;
765     }
766 }
767
768 /**
769  * Sets the path for a domain.
770  */
771 function _bindtextdomain($domain, $path) {
772     global $text_domains;
773     // ensure $path ends with a slash ('/' should work for both, but lets still play nice)
774     if (substr(php_uname(), 0, 7) == "Windows") {
775       if ($path[strlen($path)-1] != '\\' and $path[strlen($path)-1] != '/')
776         $path .= '\\';
777     } else {
778       if ($path[strlen($path)-1] != '/')
779         $path .= '/';
780     }
781     if (!array_key_exists($domain, $text_domains)) {
782       // Initialize an empty domain object.
783       $text_domains[$domain] = new domain();
784     }
785     $text_domains[$domain]->path = $path;
786 }
787
788 /**
789  * Specify the character encoding in which the messages from the DOMAIN message catalog will be returned.
790  */
791 function _bind_textdomain_codeset($domain, $codeset) {
792     global $text_domains;
793     $text_domains[$domain]->codeset = $codeset;
794 }
795
796 /**
797  * Sets the default domain.
798  */
799 function _textdomain($domain) {
800     global $default_domain;
801     $default_domain = $domain;
802 }
803
804 /**
805  * Lookup a message in the current domain.
806  */
807 function _gettext($msgid) {
808     $l10n = _get_reader();
809     //return $l10n->translate($msgid);
810     return _encode($l10n->translate($msgid));
811 }
812 /**
813  * Alias for gettext.
814  */
815 function __($msgid) {
816     return _gettext($msgid);
817 }
818 /**
819  * Plural version of gettext.
820  */
821 function _ngettext($single, $plural, $number) {
822     $l10n = _get_reader();
823     //return $l10n->ngettext($single, $plural, $number);
824     return _encode($l10n->ngettext($single, $plural, $number));
825 }
826
827 /**
828  * Override the current domain.
829  */
830 function _dgettext($domain, $msgid) {
831     $l10n = _get_reader($domain);
832     //return $l10n->translate($msgid);
833     return _encode($l10n->translate($msgid));
834 }
835 /**
836  * Plural version of dgettext.
837  */
838 function _dngettext($domain, $single, $plural, $number) {
839     $l10n = _get_reader($domain);
840     //return $l10n->ngettext($single, $plural, $number);
841     return _encode($l10n->ngettext($single, $plural, $number));
842 }
843
844 /**
845  * Overrides the domain and category for a single lookup.
846  */
847 function _dcgettext($domain, $msgid, $category) {
848     $l10n = _get_reader($domain, $category);
849     //return $l10n->translate($msgid);
850     return _encode($l10n->translate($msgid));
851 }
852 /**
853  * Plural version of dcgettext.
854  */
855 function _dcngettext($domain, $single, $plural, $number, $category) {
856     $l10n = _get_reader($domain, $category);
857     //return $l10n->ngettext($single, $plural, $number);
858     return _encode($l10n->ngettext($single, $plural, $number));
859 }
860
861
862
863 // Wrappers to use if the standard gettext functions are available, but the current locale is not supported by the system.
864 // Use the standard impl if the current locale is supported, use the custom impl otherwise.
865
866 function T_setlocale($category, $locale) {
867     return _setlocale($category, $locale);
868 }
869
870 function T_bindtextdomain($domain, $path) {
871     if (_check_locale()) return bindtextdomain($domain, $path);
872     else return _bindtextdomain($domain, $path);
873 }
874 function T_bind_textdomain_codeset($domain, $codeset) {
875     // bind_textdomain_codeset is available only in PHP 4.2.0+
876     if (_check_locale() and function_exists('bind_textdomain_codeset')) return bind_textdomain_codeset($domain, $codeset);
877     else return _bind_textdomain_codeset($domain, $codeset);
878 }
879 function T_textdomain($domain) {
880     if (_check_locale()) return textdomain($domain);
881     else return _textdomain($domain);
882 }
883 function T_gettext($msgid) {
884     if (_check_locale()) return gettext($msgid);
885     else return _gettext($msgid);
886 }
887 function T_($msgid) {
888     if (_check_locale()) return _($msgid);
889     return __($msgid);
890 }
891 function T_ngettext($single, $plural, $number) {
892     if (_check_locale()) return ngettext($single, $plural, $number);
893     else return _ngettext($single, $plural, $number);
894 }
895 function T_dgettext($domain, $msgid) {
896     if (_check_locale()) return dgettext($domain, $msgid);
897     else return _dgettext($domain, $msgid);
898 }
899 function T_dngettext($domain, $single, $plural, $number) {
900     if (_check_locale()) return dngettext($domain, $single, $plural, $number);
901     else return _dngettext($domain, $single, $plural, $number);
902 }
903 function T_dcgettext($domain, $msgid, $category) {
904     if (_check_locale()) return dcgettext($domain, $msgid, $category);
905     else return _dcgettext($domain, $msgid, $category);
906 }
907 function T_dcngettext($domain, $single, $plural, $number, $category) {
908     if (_check_locale()) return dcngettext($domain, $single, $plural, $number, $category);
909     else return _dcngettext($domain, $single, $plural, $number, $category);
910 }
911
912
913
914 // Wrappers used as a drop in replacement for the standard gettext functions
915
916 if (!function_exists('gettext')) {
917     function bindtextdomain($domain, $path) {
918         return _bindtextdomain($domain, $path);
919     }
920     function bind_textdomain_codeset($domain, $codeset) {
921         return _bind_textdomain_codeset($domain, $codeset);
922     }
923     function textdomain($domain) {
924         return _textdomain($domain);
925     }
926     function gettext($msgid) {
927         return _gettext($msgid);
928     }
929     function _($msgid) {
930         return __($msgid);
931     }
932     function ngettext($single, $plural, $number) {
933         return _ngettext($single, $plural, $number);
934     }
935     function dgettext($domain, $msgid) {
936         return _dgettext($domain, $msgid);
937     }
938     function dngettext($domain, $single, $plural, $number) {
939         return _dngettext($domain, $single, $plural, $number);
940     }
941     function dcgettext($domain, $msgid, $category) {
942         return _dcgettext($domain, $msgid, $category);
943     }
944     function dcngettext($domain, $single, $plural, $number, $category) {
945         return _dcngettext($domain, $single, $plural, $number, $category);
946     }
947 }
948
949 ?>