Fixing a small css issue in the user class.
[elgg.git] / lib / php-getttext / gettext.php
blobd26d52e46641604c82dff60eee4893a9e8cd0521
1 <?php
2 /*
3 Copyright (c) 2003 Danilo Segan <danilo@kvota.net>.
4 Copyright (c) 2005 Nico Kaiser <nico@siriux.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
24 /**
25 * Provides a simple gettext replacement that works independently from
26 * the system's gettext abilities.
27 * It can read MO files and use them for translating strings.
28 * The files are passed to gettext_reader as a Stream (see streams.php)
30 * This version has the ability to cache all strings and translations to
31 * speed up the string lookup.
32 * While the cache is enabled by default, it can be switched off with the
33 * second parameter in the constructor (e.g. whenusing very large MO files
34 * that you don't want to keep in memory)
36 class gettext_reader {
37 //public:
38 var $error = 0; // public variable that holds error code (0 if no error)
40 //private:
41 var $BYTEORDER = 0; // 0: low endian, 1: big endian
42 var $STREAM = NULL;
43 var $short_circuit = false;
44 var $enable_cache = false;
45 var $originals = NULL; // offset of original table
46 var $translations = NULL; // offset of translation table
47 var $pluralheader = NULL; // cache header field for plural forms
48 var $total = 0; // total string count
49 var $table_originals = NULL; // table for original strings (offsets)
50 var $table_translations = NULL; // table for translated strings (offsets)
51 var $cache_translations = NULL; // original -> translation mapping
54 /* Methods */
57 /**
58 * Reads a 32bit Integer from the Stream
60 * @access private
61 * @return Integer from the Stream
63 function readint() {
64 if ($this->BYTEORDER == 0) {
65 // low endian
66 return array_shift(unpack('V', $this->STREAM->read(4)));
67 } else {
68 // big endian
69 return array_shift(unpack('N', $this->STREAM->read(4)));
73 /**
74 * Reads an array of Integers from the Stream
76 * @param int count How many elements should be read
77 * @return Array of Integers
79 function readintarray($count) {
80 if ($this->BYTEORDER == 0) {
81 // low endian
82 return unpack('V'.$count, $this->STREAM->read(4 * $count));
83 } else {
84 // big endian
85 return unpack('N'.$count, $this->STREAM->read(4 * $count));
89 /**
90 * Constructor
92 * @param object Reader the StreamReader object
93 * @param boolean enable_cache Enable or disable caching of strings (default on)
95 function gettext_reader($Reader, $enable_cache = true) {
96 // If there isn't a StreamReader, turn on short circuit mode.
97 if (! $Reader || isset($Reader->error) ) {
98 $this->short_circuit = true;
99 return;
102 // Caching can be turned off
103 $this->enable_cache = $enable_cache;
105 // $MAGIC1 = (int)0x950412de; //bug in PHP 5
106 $MAGIC1 = (int) - 1794895138;
107 // $MAGIC2 = (int)0xde120495; //bug
108 $MAGIC2 = (int) - 569244523;
110 $this->STREAM = $Reader;
111 $magic = $this->readint();
112 if ($magic == ($MAGIC1 & 0xFFFFFFFF)) { // CHANGED FROM UPSTREAM
113 $this->BYTEORDER = 0;
114 } elseif ($magic == ($MAGIC2 & 0xFFFFFFFF)) { // CHANGED FROM UPSTREAM
115 $this->BYTEORDER = 1;
116 } else {
117 $this->error = 1; // not MO file
118 return false;
121 // FIXME: Do we care about revision? We should.
122 $revision = $this->readint();
124 $this->total = $this->readint();
125 $this->originals = $this->readint();
126 $this->translations = $this->readint();
128 $this->load_tables(); // CHANGED FROM UPSTREAM
132 * Loads the translation tables from the MO file into the cache
133 * If caching is enabled, also loads all strings into a cache
134 * to speed up translation lookups
136 * @access private
138 function load_tables() {
139 if (is_array($this->cache_translations) &&
140 is_array($this->table_originals) &&
141 is_array($this->table_translations))
142 return;
144 /* get original and translations tables */
145 $this->STREAM->seekto($this->originals);
146 $this->table_originals = $this->readintarray($this->total * 2);
147 $this->STREAM->seekto($this->translations);
148 $this->table_translations = $this->readintarray($this->total * 2);
150 if ($this->enable_cache) {
151 $this->cache_translations = array ();
152 /* read all strings in the cache */
153 for ($i = 0; $i < $this->total; $i++) {
154 $this->STREAM->seekto($this->table_originals[$i * 2 + 2]);
155 $original = $this->STREAM->read($this->table_originals[$i * 2 + 1]);
156 $this->STREAM->seekto($this->table_translations[$i * 2 + 2]);
157 $translation = $this->STREAM->read($this->table_translations[$i * 2 + 1]);
158 $this->cache_translations[$original] = $translation;
164 * Returns a string from the "originals" table
166 * @access private
167 * @param int num Offset number of original string
168 * @return string Requested string if found, otherwise ''
170 function get_original_string($num) {
171 $length = $this->table_originals[$num * 2 + 1];
172 $offset = $this->table_originals[$num * 2 + 2];
173 if (! $length)
174 return '';
175 $this->STREAM->seekto($offset);
176 $data = $this->STREAM->read($length);
177 return (string)$data;
181 * Returns a string from the "translations" table
183 * @access private
184 * @param int num Offset number of original string
185 * @return string Requested string if found, otherwise ''
187 function get_translation_string($num) {
188 $length = $this->table_translations[$num * 2 + 1];
189 $offset = $this->table_translations[$num * 2 + 2];
190 if (! $length)
191 return '';
192 $this->STREAM->seekto($offset);
193 $data = $this->STREAM->read($length);
194 return (string)$data;
198 * Binary search for string
200 * @access private
201 * @param string string
202 * @param int start (internally used in recursive function)
203 * @param int end (internally used in recursive function)
204 * @return int string number (offset in originals table)
206 function find_string($string, $start = -1, $end = -1) {
207 if (($start == -1) or ($end == -1)) {
208 // find_string is called with only one parameter, set start end end
209 $start = 0;
210 $end = $this->total;
212 if (abs($start - $end) <= 1) {
213 // We're done, now we either found the string, or it doesn't exist
214 $txt = $this->get_original_string($start);
215 if ($string == $txt)
216 return $start;
217 else
218 return -1;
219 } else if ($start > $end) {
220 // start > end -> turn around and start over
221 return $this->find_string($string, $end, $start);
222 } else {
223 // Divide table in two parts
224 $half = (int)(($start + $end) / 2);
225 $cmp = strcmp($string, $this->get_original_string($half));
226 if ($cmp == 0)
227 // string is exactly in the middle => return it
228 return $half;
229 else if ($cmp < 0)
230 // The string is in the upper half
231 return $this->find_string($string, $start, $half);
232 else
233 // The string is in the lower half
234 return $this->find_string($string, $half, $end);
239 * Translates a string
241 * @access public
242 * @param string string to be translated
243 * @return string translated string (or original, if not found)
245 function translate($string) {
246 if ($this->short_circuit)
247 return $string;
248 //$this->load_tables(); // CHANGED FROM UPSTREAM
250 if ($this->enable_cache) {
251 // Caching enabled, get translated string from cache
252 if (array_key_exists($string, $this->cache_translations))
253 return $this->cache_translations[$string];
254 else
255 return $string;
256 } else {
257 // Caching not enabled, try to find string
258 $num = $this->find_string($string);
259 if ($num == -1)
260 return $string;
261 else
262 return $this->get_translation_string($num);
267 * Get possible plural forms from MO header
269 * @access private
270 * @return string plural form header
272 function get_plural_forms() {
273 // lets assume message number 0 is header
274 // this is true, right?
275 //$this->load_tables(); // CHANGED FROM UPSTREAM
277 // cache header field for plural forms
278 if (! is_string($this->pluralheader)) {
279 if ($this->enable_cache) {
280 $header = $this->cache_translations[""];
281 } else {
282 $header = $this->get_translation_string(0);
284 if (eregi("plural-forms: ([^\n]*)\n", $header, $regs))
285 $expr = $regs[1];
286 else
287 $expr = "nplurals=2; plural=n == 1 ? 0 : 1;";
288 $this->pluralheader = $expr;
290 return $this->pluralheader;
294 * Detects which plural form to take
296 * @access private
297 * @param n count
298 * @return int array index of the right plural form
300 function select_string($n) {
301 $string = $this->get_plural_forms();
302 $string = str_replace('nplurals',"\$total",$string);
303 $string = str_replace("n",$n,$string);
304 $string = str_replace('plural',"\$plural",$string);
306 $total = 0;
307 $plural = 0;
309 eval("$string");
310 if ($plural >= $total) $plural = $total - 1;
311 return $plural;
315 * Plural version of gettext
317 * @access public
318 * @param string single
319 * @param string plural
320 * @param string number
321 * @return translated plural form
323 function ngettext($single, $plural, $number) {
324 if ($this->short_circuit) {
325 if ($number != 1)
326 return $plural;
327 else
328 return $single;
331 // find out the appropriate form
332 $select = $this->select_string($number);
334 // this should contains all strings separated by NULLs
335 $key = $single.chr(0).$plural;
338 if ($this->enable_cache) {
339 if (! array_key_exists($key, $this->cache_translations)) {
340 return ($number != 1) ? $plural : $single;
341 } else {
342 $result = $this->cache_translations[$key];
343 $list = explode(chr(0), $result);
344 return $list[$select];
346 } else {
347 $num = $this->find_string($key);
348 if ($num == -1) {
349 return ($number != 1) ? $plural : $single;
350 } else {
351 $result = $this->get_translation_string($num);
352 $list = explode(chr(0), $result);
353 return $list[$select];