-
+ 7E16D8355643C11F8F0A2EAB3B3C6CAE27BF83D5E09A7FD1E7936CD4759AC9540FCA1C56D9209197E374862F4F0EAB2A765F0DB8AF6FD0F765F843BE4C2565A9
mp-wp/wp-includes/gettext.php
(0 . 0)(1 . 400)
96626 <?php
96627 /**
96628 * PHP-Gettext External Library: gettext_reader class
96629 *
96630 * @package External
96631 * @subpackage PHP-gettext
96632 *
96633 * @internal
96634 Copyright (c) 2003 Danilo Segan <danilo@kvota.net>.
96635 Copyright (c) 2005 Nico Kaiser <nico@siriux.net>
96636
96637 This file is part of PHP-gettext.
96638
96639 PHP-gettext is free software; you can redistribute it and/or modify
96640 it under the terms of the GNU General Public License as published by
96641 the Free Software Foundation; either version 2 of the License, or
96642 (at your option) any later version.
96643
96644 PHP-gettext is distributed in the hope that it will be useful,
96645 but WITHOUT ANY WARRANTY; without even the implied warranty of
96646 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
96647 GNU General Public License for more details.
96648
96649 You should have received a copy of the GNU General Public License
96650 along with PHP-gettext; if not, write to the Free Software
96651 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
96652
96653 */
96654
96655 /**
96656 * Provides a simple gettext replacement that works independently from
96657 * the system's gettext abilities.
96658 * It can read MO files and use them for translating strings.
96659 * The files are passed to gettext_reader as a Stream (see streams.php)
96660 *
96661 * This version has the ability to cache all strings and translations to
96662 * speed up the string lookup.
96663 * While the cache is enabled by default, it can be switched off with the
96664 * second parameter in the constructor (e.g. whenusing very large MO files
96665 * that you don't want to keep in memory)
96666 */
96667 class gettext_reader {
96668 //public:
96669 var $error = 0; // public variable that holds error code (0 if no error)
96670
96671 //private:
96672 var $BYTEORDER = 0; // 0: low endian, 1: big endian
96673 var $STREAM = NULL;
96674 var $short_circuit = false;
96675 var $enable_cache = false;
96676 var $originals = NULL; // offset of original table
96677 var $translations = NULL; // offset of translation table
96678 var $pluralheader = NULL; // cache header field for plural forms
96679 var $select_string_function = NULL; // cache function, which chooses plural forms
96680 var $total = 0; // total string count
96681 var $table_originals = NULL; // table for original strings (offsets)
96682 var $table_translations = NULL; // table for translated strings (offsets)
96683 var $cache_translations = NULL; // original -> translation mapping
96684
96685
96686 /* Methods */
96687
96688
96689 /**
96690 * Reads a 32bit Integer from the Stream
96691 *
96692 * @access private
96693 * @return Integer from the Stream
96694 */
96695 function readint() {
96696 if ($this->BYTEORDER == 0) {
96697 // low endian
96698 $low_end = unpack('V', $this->STREAM->read(4));
96699 return array_shift($low_end);
96700 } else {
96701 // big endian
96702 $big_end = unpack('N', $this->STREAM->read(4));
96703 return array_shift($big_end);
96704 }
96705 }
96706
96707 /**
96708 * Reads an array of Integers from the Stream
96709 *
96710 * @param int count How many elements should be read
96711 * @return Array of Integers
96712 */
96713 function readintarray($count) {
96714 if ($this->BYTEORDER == 0) {
96715 // low endian
96716 return unpack('V'.$count, $this->STREAM->read(4 * $count));
96717 } else {
96718 // big endian
96719 return unpack('N'.$count, $this->STREAM->read(4 * $count));
96720 }
96721 }
96722
96723 /**
96724 * Constructor
96725 *
96726 * @param object Reader the StreamReader object
96727 * @param boolean enable_cache Enable or disable caching of strings (default on)
96728 */
96729 function gettext_reader($Reader, $enable_cache = true) {
96730 // If there isn't a StreamReader, turn on short circuit mode.
96731 if (! $Reader || isset($Reader->error) ) {
96732 $this->short_circuit = true;
96733 return;
96734 }
96735
96736 // Caching can be turned off
96737 $this->enable_cache = $enable_cache;
96738
96739 // $MAGIC1 = (int)0x950412de; //bug in PHP 5.0.2, see https://savannah.nongnu.org/bugs/?func=detailitem&item_id=10565
96740 $MAGIC1 = (int) - 1794895138;
96741 // $MAGIC2 = (int)0xde120495; //bug
96742 $MAGIC2 = (int) - 569244523;
96743 // 64-bit fix
96744 $MAGIC3 = (int) 2500072158;
96745
96746 $this->STREAM = $Reader;
96747 $magic = $this->readint();
96748 if ($magic == $MAGIC1 || $magic == $MAGIC3) { // to make sure it works for 64-bit platforms
96749 $this->BYTEORDER = 0;
96750 } elseif ($magic == ($MAGIC2 & 0xFFFFFFFF)) {
96751 $this->BYTEORDER = 1;
96752 } else {
96753 $this->error = 1; // not MO file
96754 return false;
96755 }
96756
96757 // FIXME: Do we care about revision? We should.
96758 $revision = $this->readint();
96759
96760 $this->total = $this->readint();
96761 $this->originals = $this->readint();
96762 $this->translations = $this->readint();
96763 }
96764
96765 /**
96766 * Loads the translation tables from the MO file into the cache
96767 * If caching is enabled, also loads all strings into a cache
96768 * to speed up translation lookups
96769 *
96770 * @access private
96771 */
96772 function load_tables() {
96773 if (is_array($this->cache_translations) &&
96774 is_array($this->table_originals) &&
96775 is_array($this->table_translations))
96776 return;
96777
96778 /* get original and translations tables */
96779 $this->STREAM->seekto($this->originals);
96780 $this->table_originals = $this->readintarray($this->total * 2);
96781 $this->STREAM->seekto($this->translations);
96782 $this->table_translations = $this->readintarray($this->total * 2);
96783
96784 if ($this->enable_cache) {
96785 $this->cache_translations = array ();
96786 /* read all strings in the cache */
96787 for ($i = 0; $i < $this->total; $i++) {
96788 $this->STREAM->seekto($this->table_originals[$i * 2 + 2]);
96789 $original = $this->STREAM->read($this->table_originals[$i * 2 + 1]);
96790 $this->STREAM->seekto($this->table_translations[$i * 2 + 2]);
96791 $translation = $this->STREAM->read($this->table_translations[$i * 2 + 1]);
96792 $this->cache_translations[$original] = $translation;
96793 }
96794 }
96795 }
96796
96797 /**
96798 * Returns a string from the "originals" table
96799 *
96800 * @access private
96801 * @param int num Offset number of original string
96802 * @return string Requested string if found, otherwise ''
96803 */
96804 function get_original_string($num) {
96805 $length = $this->table_originals[$num * 2 + 1];
96806 $offset = $this->table_originals[$num * 2 + 2];
96807 if (! $length)
96808 return '';
96809 $this->STREAM->seekto($offset);
96810 $data = $this->STREAM->read($length);
96811 return (string)$data;
96812 }
96813
96814 /**
96815 * Returns a string from the "translations" table
96816 *
96817 * @access private
96818 * @param int num Offset number of original string
96819 * @return string Requested string if found, otherwise ''
96820 */
96821 function get_translation_string($num) {
96822 $length = $this->table_translations[$num * 2 + 1];
96823 $offset = $this->table_translations[$num * 2 + 2];
96824 if (! $length)
96825 return '';
96826 $this->STREAM->seekto($offset);
96827 $data = $this->STREAM->read($length);
96828 return (string)$data;
96829 }
96830
96831 /**
96832 * Binary search for string
96833 *
96834 * @access private
96835 * @param string string
96836 * @param int start (internally used in recursive function)
96837 * @param int end (internally used in recursive function)
96838 * @return int string number (offset in originals table)
96839 */
96840 function find_string($string, $start = -1, $end = -1) {
96841 if (($start == -1) or ($end == -1)) {
96842 // find_string is called with only one parameter, set start end end
96843 $start = 0;
96844 $end = $this->total;
96845 }
96846 if (abs($start - $end) <= 1) {
96847 // We're done, now we either found the string, or it doesn't exist
96848 $txt = $this->get_original_string($start);
96849 if ($string == $txt)
96850 return $start;
96851 else
96852 return -1;
96853 } else if ($start > $end) {
96854 // start > end -> turn around and start over
96855 return $this->find_string($string, $end, $start);
96856 } else {
96857 // Divide table in two parts
96858 $half = (int)(($start + $end) / 2);
96859 $cmp = strcmp($string, $this->get_original_string($half));
96860 if ($cmp == 0)
96861 // string is exactly in the middle => return it
96862 return $half;
96863 else if ($cmp < 0)
96864 // The string is in the upper half
96865 return $this->find_string($string, $start, $half);
96866 else
96867 // The string is in the lower half
96868 return $this->find_string($string, $half, $end);
96869 }
96870 }
96871
96872 /**
96873 * Translates a string
96874 *
96875 * @access public
96876 * @param string string to be translated
96877 * @return string translated string (or original, if not found)
96878 */
96879 function translate($string) {
96880 if ($this->short_circuit)
96881 return $string;
96882 $this->load_tables();
96883
96884 if ($this->enable_cache) {
96885 // Caching enabled, get translated string from cache
96886 if (array_key_exists($string, $this->cache_translations))
96887 return $this->cache_translations[$string];
96888 else
96889 return $string;
96890 } else {
96891 // Caching not enabled, try to find string
96892 $num = $this->find_string($string);
96893 if ($num == -1)
96894 return $string;
96895 else
96896 return $this->get_translation_string($num);
96897 }
96898 }
96899
96900 /**
96901 * Get possible plural forms from MO header
96902 *
96903 * @access private
96904 * @return string plural form header
96905 */
96906 function get_plural_forms() {
96907 // lets assume message number 0 is header
96908 // this is true, right?
96909 $this->load_tables();
96910
96911 // cache header field for plural forms
96912 if (! is_string($this->pluralheader)) {
96913 if ($this->enable_cache) {
96914 $header = $this->cache_translations[""];
96915 } else {
96916 $header = $this->get_translation_string(0);
96917 }
96918 $header .= "\n"; //make sure our regex matches
96919 if (eregi("plural-forms: ([^\n]*)\n", $header, $regs))
96920 $expr = $regs[1];
96921 else
96922 $expr = "nplurals=2; plural=n == 1 ? 0 : 1;";
96923
96924 // add parentheses
96925 // important since PHP's ternary evaluates from left to right
96926 $expr.= ';';
96927 $res= '';
96928 $p= 0;
96929 for ($i= 0; $i < strlen($expr); $i++) {
96930 $ch= $expr[$i];
96931 switch ($ch) {
96932 case '?':
96933 $res.= ' ? (';
96934 $p++;
96935 break;
96936 case ':':
96937 $res.= ') : (';
96938 break;
96939 case ';':
96940 $res.= str_repeat( ')', $p) . ';';
96941 $p= 0;
96942 break;
96943 default:
96944 $res.= $ch;
96945 }
96946 }
96947 $this->pluralheader = $res;
96948 }
96949
96950 return $this->pluralheader;
96951 }
96952
96953 /**
96954 * Detects which plural form to take
96955 *
96956 * @access private
96957 * @param n count
96958 * @return int array index of the right plural form
96959 */
96960 function select_string($n) {
96961 if (is_null($this->select_string_function)) {
96962 $string = $this->get_plural_forms();
96963 if (preg_match("/nplurals\s*=\s*(\d+)\s*\;\s*plural\s*=\s*(.*?)\;+/", $string, $matches)) {
96964 $nplurals = $matches[1];
96965 $expression = $matches[2];
96966 $expression = str_replace("n", '$n', $expression);
96967 } else {
96968 $nplurals = 2;
96969 $expression = ' $n == 1 ? 0 : 1 ';
96970 }
96971 $func_body = "
96972 \$plural = ($expression);
96973 return (\$plural <= $nplurals)? \$plural : \$plural - 1;";
96974 $this->select_string_function = create_function('$n', $func_body);
96975 }
96976 return call_user_func($this->select_string_function, $n);
96977 }
96978
96979 /**
96980 * Plural version of gettext
96981 *
96982 * @access public
96983 * @param string single
96984 * @param string plural
96985 * @param string number
96986 * @return translated plural form
96987 */
96988 function ngettext($single, $plural, $number) {
96989 if ($this->short_circuit) {
96990 if ($number != 1)
96991 return $plural;
96992 else
96993 return $single;
96994 }
96995
96996 // find out the appropriate form
96997 $select = $this->select_string($number);
96998
96999 // this should contains all strings separated by NULLs
97000 $key = $single.chr(0).$plural;
97001
97002
97003 if ($this->enable_cache) {
97004 if (! array_key_exists($key, $this->cache_translations)) {
97005 return ($number != 1) ? $plural : $single;
97006 } else {
97007 $result = $this->cache_translations[$key];
97008 $list = explode(chr(0), $result);
97009 return $list[$select];
97010 }
97011 } else {
97012 $num = $this->find_string($key);
97013 if ($num == -1) {
97014 return ($number != 1) ? $plural : $single;
97015 } else {
97016 $result = $this->get_translation_string($num);
97017 $list = explode(chr(0), $result);
97018 return $list[$select];
97019 }
97020 }
97021 }
97022
97023 }
97024
97025 ?>