2 /*=====================================================================*\
3 || ###################################################################
5 || # Copyright ©2002-2007 Blue Static
7 || # This program is free software; you can redistribute it and/or modify
8 || # it under the terms of the GNU General Public License as published by
9 || # the Free Software Foundation; version 2 of the License.
11 || # This program is distributed in the hope that it will be useful, but
12 || # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 || # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 || # You should have received a copy of the GNU General Public License along
17 || # with this program; if not, write to the Free Software Foundation, Inc.,
18 || # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 || ###################################################################
20 \*=====================================================================*/
23 * Message Object File Reader (.mo reader)
25 * Reads a MO file that is used by gettext but without needing the gettext
26 * extension. The format is detailed here:
27 * http://www.gnu.org/software/gettext/manual/html_node/MO-Files.html
30 * @copyright Copyright (c)2006 - 2008, Blue Static
38 * The name of the file to use
44 * The file pointer of the MO file
50 * Is the MO file a big endian one?
53 var $bigEndian = false
;
56 * The string table as an array
59 var $strings = array();
62 * Creates a new MOReader given a path to a MO file
64 function MOReader($filename)
66 $this->filename
= $filename;
71 * Returns the translated string for T, or the original if none exists
73 * @param string Native string to look up a translation for
79 if (empty($this->strings
[$str]))
83 return $this->strings
[$str];
87 * Reads $size number of bytes
89 * @param integer Number of bytes to read
93 function _readBytes($size)
95 $stream = fread($this->file
, 4 * $size);
98 return unpack('N' . $size, $stream);
102 return unpack('V' . $size, $stream);
107 * Lodas the MO data information into the stirng table
111 $this->file
= @fopen($this->filename
, 'r');
114 BSApp
::debug("could not open MO file {$this->filename}");
118 // read the magic number
119 $stream = $this->_readBytes(1);
120 $stream = dechex($stream[1]);
121 if ($stream == '950412de')
123 $this->bigEndian
= false
;
125 else if ($stream == 'de120495')
127 $this->bigEndian
= true
;
131 throw new Exception('Invalid MO file format');
134 // read the revision (unused in MOs)
135 $this->_readBytes(1);
137 // read the number of strings
138 $count = $this->_readBytes(1);
141 // get the start positions of the original and translated tables
142 $offsetO = $this->_readBytes(1);
143 $offsetT = $this->_readBytes(1);
145 fseek($this->file
, $offsetO[1]);
146 $tableO = $this->_readBytes(2 * $count);
147 fseek($this->file
, $offsetT[1]);
148 $tableT = $this->_readBytes(2 * $count);
150 for ($i = 0; $i < $count; $i++
)
152 if ($tableO[$i * 2 +
1] > 0)
154 fseek($this->file
, $tableO[$i * 2 +
2]);
155 $O = fread($this->file
, $tableO[$i * 2 +
1]);
158 if ($tableT[$i * 2 +
1] > 0)
160 fseek($this->file
, $tableT[$i * 2 +
2]);
161 $this->strings
[$O] = fread($this->file
, $tableT[$i * 2 +
1]);
169 /*=====================================================================*\
170 || ###################################################################
173 || ###################################################################
174 \*=====================================================================*/