4 * Copyright (c) 2003 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
22 * @copyright 2004-2022 The SquirrelMail Project Team
23 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
25 * @package squirrelmail
30 * Class that uses parsed translation input objects
31 * @package squirrelmail
34 class gettext_reader
{
36 * holds error code (0 if no error)
42 * specifies the byte order: 0 low endian, 1 big endian
58 // Reads 4 byte value from $FD and puts it in int
59 // $BYTEORDER specifies the byte order: 0 low endian, 1 big endian
60 for ($i=0; $i<4; $i++
) {
61 $byte[$i]=ord($this->STREAM
->read(1));
63 //print sprintf("pos: %d\n",$this->STREAM->currentpos());
64 if ($this->BYTEORDER
== 0)
65 return (int)(($byte[0]) |
($byte[1]<<8) |
($byte[2]<<16) |
($byte[3]<<24));
67 return (int)(($byte[3]) |
($byte[2]<<8) |
($byte[1]<<16) |
($byte[0]<<24));
71 * Constructor (PHP5 style, required in some future version of PHP)
72 * constructor that requires StreamReader object
73 * @param object $Reader
74 * @return boolean false, if some error with stream
75 TODO: Constructors should not return anything.
77 function __construct($Reader) {
78 $MAGIC1 = (int) ((222) |
(18<<8) |
(4<<16) |
(149<<24));
79 $MAGIC2 = (int) ((149) |
(4<<8) |
(18<<16) |
(222<<24));
81 $this->STREAM
= $Reader;
82 if ($this->STREAM
->error
>0) {
86 $magic = $this->readint();
87 if ($magic == $MAGIC1) {
89 } elseif ($magic == $MAGIC2) {
92 $this->error
= 1; // not MO file
96 // FIXME: Do we care about revision? We should.
97 $revision = $this->readint();
99 $total = $this->readint();
100 $originals = $this->readint();
101 $translations = $this->readint();
103 $this->total
= $total;
104 $this->originals
= $originals;
105 $this->translations
= $translations;
107 // Here we store already found translations
108 $this->_HASHED
= array();
112 * Constructor (PHP4 style, kept for compatibility reasons)
113 * constructor that requires StreamReader object
114 * @param object $Reader
115 * @return boolean false, if some error with stream
116 TODO: Constructors should not return anything.
118 function gettext_reader($Reader) {
119 return self
::__construct($Reader);
123 * @param boolean $translations do translation have to be loaded
125 function load_tables($translations=false) {
126 // if tables are loaded do not load them again
127 if (!isset($this->ORIGINALS
)) {
128 $this->ORIGINALS
= array();
129 $this->STREAM
->seekto($this->originals
);
130 for ($i=0; $i<$this->total
; $i++
) {
131 $len = $this->readint();
132 $ofs = $this->readint();
133 $this->ORIGINALS
[] = array($len,$ofs);
137 // similar for translations
138 if ($translations and !isset($this->TRANSLATIONS
)) {
139 $this->TRANSLATIONS
= array();
140 $this->STREAM
->seekto($this->translations
);
141 for ($i=0; $i<$this->total
; $i++
) {
142 $len = $this->readint();
143 $ofs = $this->readint();
144 $this->TRANSLATIONS
[] = array($len,$ofs);
150 * get a string with particular number
151 * @param integer $num
152 * @return string untranslated string
154 function get_string_number($num) {
155 // TODO: Add simple hashing [check array, add if not already there]
156 $this->load_tables();
157 $meta = $this->ORIGINALS
[$num];
160 $this->STREAM
->seekto($offset);
161 $data = $this->STREAM
->read($length);
162 return (string)$data;
166 * get translated string with particular number
167 * @param integer $num
168 * @return string translated string
170 function get_translation_number($num) {
171 // get a string with particular number
172 // TODO: Add simple hashing [check array, add if not already there]
173 $this->load_tables(true);
174 $meta = $this->TRANSLATIONS
[$num];
177 $this->STREAM
->seekto($offset);
178 $data = $this->STREAM
->read($length);
179 return (string)$data;
183 * binary search for string
184 * @param string $string
185 * @param integer $start
186 * @param integer $end
188 function find_string($string, $start,$end) {
189 //print "start: $start, end: $end\n";
190 // Simple hashing to improve speed
191 if (isset($this->_HASHED
[$string])) return $this->_HASHED
[$string];
193 if (abs($start-$end)<=1) {
194 // we're done, if it's not it, bye bye
195 $txt = $this->get_string_number($start);
196 if ($string == $txt) {
197 $this->_HASHED
[$string] = $start;
201 } elseif ($start>$end) {
202 return $this->find_string($string,$end,$start);
204 $half = (int)(($start+
$end)/2);
205 $tst = $this->get_string_number($half);
206 $cmp = strcmp($string,$tst);
208 $this->_HASHED
[$string] = $half;
211 return $this->find_string($string,$start,$half);
213 return $this->find_string($string,$half,$end);
219 * @param string $string English string
220 * @return string translated string
222 function translate($string) {
223 if ($this->error
> 0) return $string;
224 $num = $this->find_string($string, 0, $this->total
);
228 return $this->get_translation_number($num);
232 * extract plural forms header
233 * @return string plural-forms header string
235 function get_plural_forms() {
236 // lets assume message number 0 is header
237 // this is true, right?
239 // cache header field for plural forms
240 if (isset($this->pluralheader
) && is_string($this->pluralheader
))
241 return $this->pluralheader
;
243 $header = $this->get_translation_number(0);
245 if (preg_match('/plural-forms: (.*)\n/i',$header,$regs)) {
248 $expr = "nplurals=2; plural=n == 1 ? 0 : 1;";
250 $this->pluralheader
= $expr;
256 * find out the appropriate form number
257 * @param integer $n count
260 function select_string($n) {
261 $string = $this->get_plural_forms();
262 $string = str_replace('nplurals',"\$total",$string);
263 $string = str_replace("n",$n,$string);
264 $string = str_replace('plural',"\$plural",$string);
270 if ($plural>=$total) $plural = 0;
275 * translate string with singular/plural forms
276 * @param string $single English singural form of translation
277 * @param string $plural English plural form of translation
278 * @param string $number count
281 function ngettext($single, $plural, $number) {
282 if ($this->error
> 0) {
285 // find out the appropriate form
286 $select = $this->select_string($number);
288 // this should contains all strings separated by NULLs
289 $result = $this->find_string($single.chr(0).$plural,0,$this->total
);
292 if ($number != 1) return $plural;
295 $result = $this->get_translation_number($result);
297 // lets try to parse all the NUL staff
298 //$result = "proba0".chr(0)."proba1".chr(0)."proba2";
299 $list = explode (chr(0), $result);
300 return $list[$select];