4 Copyright (c) 2003, 2005, 2006, 2009 Danilo Segan <danilo@kvota.net>.
6 This file is part of PHP-gettext.
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.
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.
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
25 * MODIFIED FOR THE JAPPIX PROJECT
26 * Last revision: 10/11/10
29 // Simple class to wrap file streams, string streams, etc.
30 // seek is essential, and it should be byte stream
32 // should return a string [FIXME: perhaps return array of bytes?]
33 function read($bytes) {
37 // should return new position
38 function seekto($position) {
42 // returns current position
43 function currentpos() {
47 // returns length of entire stream (limit for seekto()s)
57 function StringReader($str='') {
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);
71 function seekto($pos) {
73 if (strlen($this->_str)<$this->_pos)
74 $this->_pos = strlen($this->_str);
78 function currentpos() {
83 return strlen($this->_str);
94 function FileReader($filename) {
95 if (file_exists($filename)) {
97 $this->_length=filesize($filename);
99 $this->_fd = fopen($filename,'rb');
101 $this->error = 3; // Cannot read file, probably permissions
105 $this->error = 2; // File doesn't exist
110 function read($bytes) {
112 fseek($this->_fd, $this->_pos);
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
118 $chunk = fread($this->_fd, $bytes);
120 $bytes -= strlen($chunk);
122 $this->_pos = ftell($this->_fd);
128 function seekto($pos) {
129 fseek($this->_fd, $pos);
130 $this->_pos = ftell($this->_fd);
134 function currentpos() {
139 return $this->_length;
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)) {
154 $length=filesize($filename);
155 $fd = fopen($filename,'rb');
158 $this->error = 3; // Cannot read file, probably permissions
161 $this->_str = fread($fd, $length);
165 $this->error = 2; // File doesn't exist
172 Copyright (c) 2003, 2009 Danilo Segan <danilo@kvota.net>.
173 Copyright (c) 2005 Nico Kaiser <nico@siriux.net>
175 This file is part of PHP-gettext.
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.
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.
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
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)
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)
205 class gettext_reader {
207 var $error = 0; // public variable that holds error code (0 if no error)
210 var $BYTEORDER = 0; // 0: low endian, 1: big endian
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
227 * Reads a 32bit Integer from the Stream
230 * @return Integer from the Stream
233 if ($this->BYTEORDER == 0) {
235 $input=unpack('V', $this->STREAM->read(4));
236 return array_shift($input);
239 $input=unpack('N', $this->STREAM->read(4));
240 return array_shift($input);
244 function read($bytes) {
245 return $this->STREAM->read($bytes);
249 * Reads an array of Integers from the Stream
251 * @param int count How many elements should be read
252 * @return Array of Integers
254 function readintarray($count) {
255 if ($this->BYTEORDER == 0) {
257 return unpack('V'.$count, $this->STREAM->read(4 * $count));
260 return unpack('N'.$count, $this->STREAM->read(4 * $count));
267 * @param object Reader the StreamReader object
268 * @param boolean enable_cache Enable or disable caching of strings (default on)
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;
277 // Caching can be turned off
278 $this->enable_cache = $enable_cache;
280 $MAGIC1 = "\x95\x04\x12\xde";
281 $MAGIC2 = "\xde\x12\x04\x95";
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;
290 $this->error = 1; // not MO file
294 // FIXME: Do we care about revision? We should.
295 $revision = $this->readint();
297 $this->total = $this->readint();
298 $this->originals = $this->readint();
299 $this->translations = $this->readint();
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
309 function load_tables() {
310 if (is_array($this->cache_translations) &&
311 is_array($this->table_originals) &&
312 is_array($this->table_translations))
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);
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;
335 * Returns a string from the "originals" table
338 * @param int num Offset number of original string
339 * @return string Requested string if found, otherwise ''
341 function get_original_string($num) {
342 $length = $this->table_originals[$num * 2 + 1];
343 $offset = $this->table_originals[$num * 2 + 2];
346 $this->STREAM->seekto($offset);
347 $data = $this->STREAM->read($length);
348 return (string)$data;
352 * Returns a string from the "translations" table
355 * @param int num Offset number of original string
356 * @return string Requested string if found, otherwise ''
358 function get_translation_string($num) {
359 $length = $this->table_translations[$num * 2 + 1];
360 $offset = $this->table_translations[$num * 2 + 2];
363 $this->STREAM->seekto($offset);
364 $data = $this->STREAM->read($length);
365 return (string)$data;
369 * Binary search for string
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)
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
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);
390 } else if ($start > $end) {
391 // start > end -> turn around and start over
392 return $this->find_string($string, $end, $start);
394 // Divide table in two parts
395 $half = (int)(($start + $end) / 2);
396 $cmp = strcmp($string, $this->get_original_string($half));
398 // string is exactly in the middle => return it
401 // The string is in the upper half
402 return $this->find_string($string, $start, $half);
404 // The string is in the lower half
405 return $this->find_string($string, $half, $end);
410 * Translates a string
413 * @param string string to be translated
414 * @return string translated string (or original, if not found)
416 function translate($string) {
417 if ($this->short_circuit)
419 $this->load_tables();
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];
428 // Caching not enabled, try to find string
429 $num = $this->find_string($string);
433 return $this->get_translation_string($num);
438 * Sanitize plural form expression for use in PHP eval call.
441 * @return string sanitized plural form expression
443 function sanitize_plural_expression($expr) {
444 // Get rid of disallowed characters.
445 $expr = preg_replace('@[^a-zA-Z0-9_:;\(\)\?\|\&=!<>+*/\%-]@', '', $expr);
447 // Add parenthesis for tertiary '?' operator.
451 for ($i = 0; $i < strlen($expr); $i++) {
462 $res .= str_repeat( ')', $p) . ';';
473 * Get possible plural forms from MO header
476 * @return string plural form header
478 function get_plural_forms() {
479 // lets assume message number 0 is header
480 // this is true, right?
481 $this->load_tables();
483 // cache header field for plural forms
484 if (! is_string($this->pluralheader)) {
485 if ($this->enable_cache) {
486 $header = $this->cache_translations[""];
488 $header = $this->get_translation_string(0);
490 if (eregi("plural-forms: ([^\n]*)\n", $header, $regs))
493 $expr = "nplurals=2; plural=n == 1 ? 0 : 1;";
495 $this->pluralheader = $this->sanitize_plural_expression($expr);
497 return $this->pluralheader;
501 * Detects which plural form to take
505 * @return int array index of the right plural form
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);
517 if ($plural >= $total) $plural = $total - 1;
522 * Plural version of gettext
525 * @param string single
526 * @param string plural
527 * @param string number
528 * @return translated plural form
530 function ngettext($single, $plural, $number) {
531 if ($this->short_circuit) {
538 // find out the appropriate form
539 $select = $this->select_string($number);
541 // this should contains all strings separated by NULLs
542 $key = $single.chr(0).$plural;
545 if ($this->enable_cache) {
546 if (! array_key_exists($key, $this->cache_translations)) {
547 return ($number != 1) ? $plural : $single;
549 $result = $this->cache_translations[$key];
550 $list = explode(chr(0), $result);
551 return $list[$select];
554 $num = $this->find_string($key);
556 return ($number != 1) ? $plural : $single;
558 $result = $this->get_translation_string($num);
559 $list = explode(chr(0), $result);
560 return $list[$select];
568 Copyright (c) 2005 Steven Armstrong <sa at c-area dot ch>
569 Copyright (c) 2009 Danilo Segan <danilo@kvota.net>
571 Drop in replacement for native gettext.
573 This file is part of PHP-gettext.
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.
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.
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
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);
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');
616 /* Class to hold a single domain included in $text_domains. */
626 * Utility function to get a StreamReader for the given text domain.
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.
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)) {
653 if(isset($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];
663 $locale_names = array("${lang}_$country.$charset@$modifier",
664 "${lang}_$country@$modifier",
667 array_push($locale_names,
668 "${lang}_$country.$charset", "${lang}_$country", "$lang");
670 array_push($locale_names, $locale);
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);
681 if (!array_key_exists($domain, $text_domains)) {
682 // Initialize an empty domain object.
683 $text_domains[$domain] = new domain();
685 $text_domains[$domain]->l10n = new gettext_reader($input,
688 return $text_domains[$domain]->l10n;
692 * Returns whether we are using our emulated gettext API or PHP built-in one.
694 function locale_emulation() {
695 global $EMULATEGETTEXT;
696 return $EMULATEGETTEXT;
700 * Checks if the current locale is supported on this system.
702 function _check_locale() {
703 global $EMULATEGETTEXT;
704 return !$EMULATEGETTEXT;
708 * Get the codeset for the given domain.
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');
717 * Convert the given string to the encoding set by bind_textdomain_codeset.
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);
733 // Custom implementation of the standard gettext related functions
736 * Sets a requested locale, if needed emulates it.
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;
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);
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)) {
753 $CURRENTLOCALE = $ret;
755 if ($locale == '') // emulate variable support
756 $CURRENTLOCALE = getenv('LANG');
758 $CURRENTLOCALE = $locale;
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;
769 * Sets the path for a domain.
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] != '/')
778 if ($path[strlen($path)-1] != '/')
781 if (!array_key_exists($domain, $text_domains)) {
782 // Initialize an empty domain object.
783 $text_domains[$domain] = new domain();
785 $text_domains[$domain]->path = $path;
789 * Specify the character encoding in which the messages from the DOMAIN message catalog will be returned.
791 function _bind_textdomain_codeset($domain, $codeset) {
792 global $text_domains;
793 $text_domains[$domain]->codeset = $codeset;
797 * Sets the default domain.
799 function _textdomain($domain) {
800 global $default_domain;
801 $default_domain = $domain;
805 * Lookup a message in the current domain.
807 function _gettext($msgid) {
808 $l10n = _get_reader();
809 //return $l10n->translate($msgid);
810 return _encode($l10n->translate($msgid));
815 function __($msgid) {
816 return _gettext($msgid);
819 * Plural version of gettext.
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));
828 * Override the current domain.
830 function _dgettext($domain, $msgid) {
831 $l10n = _get_reader($domain);
832 //return $l10n->translate($msgid);
833 return _encode($l10n->translate($msgid));
836 * Plural version of dgettext.
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));
845 * Overrides the domain and category for a single lookup.
847 function _dcgettext($domain, $msgid, $category) {
848 $l10n = _get_reader($domain, $category);
849 //return $l10n->translate($msgid);
850 return _encode($l10n->translate($msgid));
853 * Plural version of dcgettext.
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));
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.
866 function T_setlocale($category, $locale) {
867 return _setlocale($category, $locale);
870 function T_bindtextdomain($domain, $path) {
871 if (_check_locale()) return bindtextdomain($domain, $path);
872 else return _bindtextdomain($domain, $path);
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);
879 function T_textdomain($domain) {
880 if (_check_locale()) return textdomain($domain);
881 else return _textdomain($domain);
883 function T_gettext($msgid) {
884 if (_check_locale()) return gettext($msgid);
885 else return _gettext($msgid);
887 function T_($msgid) {
888 if (_check_locale()) return _($msgid);
891 function T_ngettext($single, $plural, $number) {
892 if (_check_locale()) return ngettext($single, $plural, $number);
893 else return _ngettext($single, $plural, $number);
895 function T_dgettext($domain, $msgid) {
896 if (_check_locale()) return dgettext($domain, $msgid);
897 else return _dgettext($domain, $msgid);
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);
903 function T_dcgettext($domain, $msgid, $category) {
904 if (_check_locale()) return dcgettext($domain, $msgid, $category);
905 else return _dcgettext($domain, $msgid, $category);
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);
914 // Wrappers used as a drop in replacement for the standard gettext functions
916 if (!function_exists('gettext')) {
917 function bindtextdomain($domain, $path) {
918 return _bindtextdomain($domain, $path);
920 function bind_textdomain_codeset($domain, $codeset) {
921 return _bind_textdomain_codeset($domain, $codeset);
923 function textdomain($domain) {
924 return _textdomain($domain);
926 function gettext($msgid) {
927 return _gettext($msgid);
932 function ngettext($single, $plural, $number) {
933 return _ngettext($single, $plural, $number);
935 function dgettext($domain, $msgid) {
936 return _dgettext($domain, $msgid);
938 function dngettext($domain, $single, $plural, $number) {
939 return _dngettext($domain, $single, $plural, $number);
941 function dcgettext($domain, $msgid, $category) {
942 return _dcgettext($domain, $msgid, $category);
944 function dcngettext($domain, $single, $plural, $number, $category) {
945 return _dcngettext($domain, $single, $plural, $number, $category);