|
1 <?php |
|
2 /** |
|
3 * Class for working with MO files |
|
4 * |
|
5 * @version $Id: mo.php 293 2009-11-12 15:43:50Z nbachiyski $ |
|
6 * @package pomo |
|
7 * @subpackage mo |
|
8 */ |
|
9 |
|
10 require_once dirname(__FILE__) . '/translations.php'; |
|
11 require_once dirname(__FILE__) . '/streams.php'; |
|
12 |
|
13 if ( !class_exists( 'MO' ) ): |
|
14 class MO extends Gettext_Translations { |
|
15 |
|
16 var $_nplurals = 2; |
|
17 |
|
18 /** |
|
19 * Fills up with the entries from MO file $filename |
|
20 * |
|
21 * @param string $filename MO file to load |
|
22 */ |
|
23 function import_from_file($filename) { |
|
24 $reader = new POMO_FileReader($filename); |
|
25 if (!$reader->is_resource()) |
|
26 return false; |
|
27 return $this->import_from_reader($reader); |
|
28 } |
|
29 |
|
30 function export_to_file($filename) { |
|
31 $fh = fopen($filename, 'wb'); |
|
32 if ( !$fh ) return false; |
|
33 $entries = array_filter($this->entries, create_function('$e', 'return !empty($e->translations);')); |
|
34 ksort($entries); |
|
35 $magic = 0x950412de; |
|
36 $revision = 0; |
|
37 $total = count($entries) + 1; // all the headers are one entry |
|
38 $originals_lenghts_addr = 28; |
|
39 $translations_lenghts_addr = $originals_lenghts_addr + 8 * $total; |
|
40 $size_of_hash = 0; |
|
41 $hash_addr = $translations_lenghts_addr + 8 * $total; |
|
42 $current_addr = $hash_addr; |
|
43 fwrite($fh, pack('V*', $magic, $revision, $total, $originals_lenghts_addr, |
|
44 $translations_lenghts_addr, $size_of_hash, $hash_addr)); |
|
45 fseek($fh, $originals_lenghts_addr); |
|
46 |
|
47 // headers' msgid is an empty string |
|
48 fwrite($fh, pack('VV', 0, $current_addr)); |
|
49 $current_addr++; |
|
50 $originals_table = chr(0); |
|
51 |
|
52 foreach($entries as $entry) { |
|
53 $originals_table .= $this->export_original($entry) . chr(0); |
|
54 $length = strlen($this->export_original($entry)); |
|
55 fwrite($fh, pack('VV', $length, $current_addr)); |
|
56 $current_addr += $length + 1; // account for the NULL byte after |
|
57 } |
|
58 |
|
59 $exported_headers = $this->export_headers(); |
|
60 fwrite($fh, pack('VV', strlen($exported_headers), $current_addr)); |
|
61 $current_addr += strlen($exported_headers) + 1; |
|
62 $translations_table = $exported_headers . chr(0); |
|
63 |
|
64 foreach($entries as $entry) { |
|
65 $translations_table .= $this->export_translations($entry) . chr(0); |
|
66 $length = strlen($this->export_translations($entry)); |
|
67 fwrite($fh, pack('VV', $length, $current_addr)); |
|
68 $current_addr += $length + 1; |
|
69 } |
|
70 |
|
71 fwrite($fh, $originals_table); |
|
72 fwrite($fh, $translations_table); |
|
73 fclose($fh); |
|
74 } |
|
75 |
|
76 function export_original($entry) { |
|
77 //TODO: warnings for control characters |
|
78 $exported = $entry->singular; |
|
79 if ($entry->is_plural) $exported .= chr(0).$entry->plural; |
|
80 if (!is_null($entry->context)) $exported = $entry->context . chr(4) . $exported; |
|
81 return $exported; |
|
82 } |
|
83 |
|
84 function export_translations($entry) { |
|
85 //TODO: warnings for control characters |
|
86 return implode(chr(0), $entry->translations); |
|
87 } |
|
88 |
|
89 function export_headers() { |
|
90 $exported = ''; |
|
91 foreach($this->headers as $header => $value) { |
|
92 $exported.= "$header: $value\n"; |
|
93 } |
|
94 return $exported; |
|
95 } |
|
96 |
|
97 function get_byteorder($magic) { |
|
98 // The magic is 0x950412de |
|
99 |
|
100 // bug in PHP 5.0.2, see https://savannah.nongnu.org/bugs/?func=detailitem&item_id=10565 |
|
101 $magic_little = (int) - 1794895138; |
|
102 $magic_little_64 = (int) 2500072158; |
|
103 // 0xde120495 |
|
104 $magic_big = ((int) - 569244523) & 0xFFFFFFFF; |
|
105 if ($magic_little == $magic || $magic_little_64 == $magic) { |
|
106 return 'little'; |
|
107 } else if ($magic_big == $magic) { |
|
108 return 'big'; |
|
109 } else { |
|
110 return false; |
|
111 } |
|
112 } |
|
113 |
|
114 function import_from_reader($reader) { |
|
115 $endian_string = MO::get_byteorder($reader->readint32()); |
|
116 if (false === $endian_string) { |
|
117 return false; |
|
118 } |
|
119 $reader->setEndian($endian_string); |
|
120 |
|
121 $endian = ('big' == $endian_string)? 'N' : 'V'; |
|
122 |
|
123 $header = $reader->read(24); |
|
124 if ($reader->strlen($header) != 24) |
|
125 return false; |
|
126 |
|
127 // parse header |
|
128 $header = unpack("{$endian}revision/{$endian}total/{$endian}originals_lenghts_addr/{$endian}translations_lenghts_addr/{$endian}hash_length/{$endian}hash_addr", $header); |
|
129 if (!is_array($header)) |
|
130 return false; |
|
131 |
|
132 extract( $header ); |
|
133 |
|
134 // support revision 0 of MO format specs, only |
|
135 if ($revision != 0) |
|
136 return false; |
|
137 |
|
138 // seek to data blocks |
|
139 $reader->seekto($originals_lenghts_addr); |
|
140 |
|
141 // read originals' indices |
|
142 $originals_lengths_length = $translations_lenghts_addr - $originals_lenghts_addr; |
|
143 if ( $originals_lengths_length != $total * 8 ) |
|
144 return false; |
|
145 |
|
146 $originals = $reader->read($originals_lengths_length); |
|
147 if ( $reader->strlen( $originals ) != $originals_lengths_length ) |
|
148 return false; |
|
149 |
|
150 // read translations' indices |
|
151 $translations_lenghts_length = $hash_addr - $translations_lenghts_addr; |
|
152 if ( $translations_lenghts_length != $total * 8 ) |
|
153 return false; |
|
154 |
|
155 $translations = $reader->read($translations_lenghts_length); |
|
156 if ( $reader->strlen( $translations ) != $translations_lenghts_length ) |
|
157 return false; |
|
158 |
|
159 // transform raw data into set of indices |
|
160 $originals = $reader->str_split( $originals, 8 ); |
|
161 $translations = $reader->str_split( $translations, 8 ); |
|
162 |
|
163 // skip hash table |
|
164 $strings_addr = $hash_addr + $hash_length * 4; |
|
165 |
|
166 $reader->seekto($strings_addr); |
|
167 |
|
168 $strings = $reader->read_all(); |
|
169 $reader->close(); |
|
170 |
|
171 for ( $i = 0; $i < $total; $i++ ) { |
|
172 $o = unpack( "{$endian}length/{$endian}pos", $originals[$i] ); |
|
173 $t = unpack( "{$endian}length/{$endian}pos", $translations[$i] ); |
|
174 if ( !$o || !$t ) return false; |
|
175 |
|
176 // adjust offset due to reading strings to separate space before |
|
177 $o['pos'] -= $strings_addr; |
|
178 $t['pos'] -= $strings_addr; |
|
179 |
|
180 $original = $reader->substr( $strings, $o['pos'], $o['length'] ); |
|
181 $translation = $reader->substr( $strings, $t['pos'], $t['length'] ); |
|
182 |
|
183 if ('' === $original) { |
|
184 $this->set_headers($this->make_headers($translation)); |
|
185 } else { |
|
186 $entry = &$this->make_entry($original, $translation); |
|
187 $this->entries[$entry->key()] = &$entry; |
|
188 } |
|
189 } |
|
190 return true; |
|
191 } |
|
192 |
|
193 /** |
|
194 * Build a Translation_Entry from original string and translation strings, |
|
195 * found in a MO file |
|
196 * |
|
197 * @static |
|
198 * @param string $original original string to translate from MO file. Might contain |
|
199 * 0x04 as context separator or 0x00 as singular/plural separator |
|
200 * @param string $translation translation string from MO file. Might contain |
|
201 * 0x00 as a plural translations separator |
|
202 */ |
|
203 function &make_entry($original, $translation) { |
|
204 $entry = & new Translation_Entry(); |
|
205 // look for context |
|
206 $parts = explode(chr(4), $original); |
|
207 if (isset($parts[1])) { |
|
208 $original = $parts[1]; |
|
209 $entry->context = $parts[0]; |
|
210 } |
|
211 // look for plural original |
|
212 $parts = explode(chr(0), $original); |
|
213 $entry->singular = $parts[0]; |
|
214 if (isset($parts[1])) { |
|
215 $entry->is_plural = true; |
|
216 $entry->plural = $parts[1]; |
|
217 } |
|
218 // plural translations are also separated by \0 |
|
219 $entry->translations = explode(chr(0), $translation); |
|
220 return $entry; |
|
221 } |
|
222 |
|
223 function select_plural_form($count) { |
|
224 return $this->gettext_select_plural_form($count); |
|
225 } |
|
226 |
|
227 function get_plural_forms_count() { |
|
228 return $this->_nplurals; |
|
229 } |
|
230 } |
|
231 endif; |