[ Index ] |
PHP Cross Reference of BuddyPress |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Class for working with MO files 4 * 5 * @version $Id: mo.php 1157 2015-11-20 04:30:11Z dd32 $ 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', false ) ): 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 /** 31 * @param string $filename 32 * @return bool 33 */ 34 function export_to_file($filename) { 35 $fh = fopen($filename, 'wb'); 36 if ( !$fh ) return false; 37 $res = $this->export_to_file_handle( $fh ); 38 fclose($fh); 39 return $res; 40 } 41 42 /** 43 * @return string|false 44 */ 45 function export() { 46 $tmp_fh = fopen("php://temp", 'r+'); 47 if ( !$tmp_fh ) return false; 48 $this->export_to_file_handle( $tmp_fh ); 49 rewind( $tmp_fh ); 50 return stream_get_contents( $tmp_fh ); 51 } 52 53 /** 54 * @param Translation_Entry $entry 55 * @return bool 56 */ 57 function is_entry_good_for_export( $entry ) { 58 if ( empty( $entry->translations ) ) { 59 return false; 60 } 61 62 if ( !array_filter( $entry->translations ) ) { 63 return false; 64 } 65 66 return true; 67 } 68 69 /** 70 * @param resource $fh 71 * @return true 72 */ 73 function export_to_file_handle($fh) { 74 $entries = array_filter( $this->entries, array( $this, 'is_entry_good_for_export' ) ); 75 ksort($entries); 76 $magic = 0x950412de; 77 $revision = 0; 78 $total = count($entries) + 1; // all the headers are one entry 79 $originals_lenghts_addr = 28; 80 $translations_lenghts_addr = $originals_lenghts_addr + 8 * $total; 81 $size_of_hash = 0; 82 $hash_addr = $translations_lenghts_addr + 8 * $total; 83 $current_addr = $hash_addr; 84 fwrite($fh, pack('V*', $magic, $revision, $total, $originals_lenghts_addr, 85 $translations_lenghts_addr, $size_of_hash, $hash_addr)); 86 fseek($fh, $originals_lenghts_addr); 87 88 // headers' msgid is an empty string 89 fwrite($fh, pack('VV', 0, $current_addr)); 90 $current_addr++; 91 $originals_table = chr(0); 92 93 $reader = new POMO_Reader(); 94 95 foreach($entries as $entry) { 96 $originals_table .= $this->export_original($entry) . chr(0); 97 $length = $reader->strlen($this->export_original($entry)); 98 fwrite($fh, pack('VV', $length, $current_addr)); 99 $current_addr += $length + 1; // account for the NULL byte after 100 } 101 102 $exported_headers = $this->export_headers(); 103 fwrite($fh, pack('VV', $reader->strlen($exported_headers), $current_addr)); 104 $current_addr += strlen($exported_headers) + 1; 105 $translations_table = $exported_headers . chr(0); 106 107 foreach($entries as $entry) { 108 $translations_table .= $this->export_translations($entry) . chr(0); 109 $length = $reader->strlen($this->export_translations($entry)); 110 fwrite($fh, pack('VV', $length, $current_addr)); 111 $current_addr += $length + 1; 112 } 113 114 fwrite($fh, $originals_table); 115 fwrite($fh, $translations_table); 116 return true; 117 } 118 119 /** 120 * @param Translation_Entry $entry 121 * @return string 122 */ 123 function export_original($entry) { 124 //TODO: warnings for control characters 125 $exported = $entry->singular; 126 if ($entry->is_plural) $exported .= chr(0).$entry->plural; 127 if ($entry->context) $exported = $entry->context . chr(4) . $exported; 128 return $exported; 129 } 130 131 /** 132 * @param Translation_Entry $entry 133 * @return string 134 */ 135 function export_translations($entry) { 136 //TODO: warnings for control characters 137 return $entry->is_plural ? implode(chr(0), $entry->translations) : $entry->translations[0]; 138 } 139 140 /** 141 * @return string 142 */ 143 function export_headers() { 144 $exported = ''; 145 foreach($this->headers as $header => $value) { 146 $exported.= "$header: $value\n"; 147 } 148 return $exported; 149 } 150 151 /** 152 * @param int $magic 153 * @return string|false 154 */ 155 function get_byteorder($magic) { 156 // The magic is 0x950412de 157 158 // bug in PHP 5.0.2, see https://savannah.nongnu.org/bugs/?func=detailitem&item_id=10565 159 $magic_little = (int) - 1794895138; 160 $magic_little_64 = (int) 2500072158; 161 // 0xde120495 162 $magic_big = ((int) - 569244523) & 0xFFFFFFFF; 163 if ($magic_little == $magic || $magic_little_64 == $magic) { 164 return 'little'; 165 } else if ($magic_big == $magic) { 166 return 'big'; 167 } else { 168 return false; 169 } 170 } 171 172 /** 173 * @param POMO_FileReader $reader 174 */ 175 function import_from_reader($reader) { 176 $endian_string = MO::get_byteorder($reader->readint32()); 177 if (false === $endian_string) { 178 return false; 179 } 180 $reader->setEndian($endian_string); 181 182 $endian = ('big' == $endian_string)? 'N' : 'V'; 183 184 $header = $reader->read(24); 185 if ($reader->strlen($header) != 24) 186 return false; 187 188 // parse header 189 $header = unpack("{$endian}revision/{$endian}total/{$endian}originals_lenghts_addr/{$endian}translations_lenghts_addr/{$endian}hash_length/{$endian}hash_addr", $header); 190 if (!is_array($header)) 191 return false; 192 193 // support revision 0 of MO format specs, only 194 if ( $header['revision'] != 0 ) { 195 return false; 196 } 197 198 // seek to data blocks 199 $reader->seekto( $header['originals_lenghts_addr'] ); 200 201 // read originals' indices 202 $originals_lengths_length = $header['translations_lenghts_addr'] - $header['originals_lenghts_addr']; 203 if ( $originals_lengths_length != $header['total'] * 8 ) { 204 return false; 205 } 206 207 $originals = $reader->read($originals_lengths_length); 208 if ( $reader->strlen( $originals ) != $originals_lengths_length ) { 209 return false; 210 } 211 212 // read translations' indices 213 $translations_lenghts_length = $header['hash_addr'] - $header['translations_lenghts_addr']; 214 if ( $translations_lenghts_length != $header['total'] * 8 ) { 215 return false; 216 } 217 218 $translations = $reader->read($translations_lenghts_length); 219 if ( $reader->strlen( $translations ) != $translations_lenghts_length ) { 220 return false; 221 } 222 223 // transform raw data into set of indices 224 $originals = $reader->str_split( $originals, 8 ); 225 $translations = $reader->str_split( $translations, 8 ); 226 227 // skip hash table 228 $strings_addr = $header['hash_addr'] + $header['hash_length'] * 4; 229 230 $reader->seekto($strings_addr); 231 232 $strings = $reader->read_all(); 233 $reader->close(); 234 235 for ( $i = 0; $i < $header['total']; $i++ ) { 236 $o = unpack( "{$endian}length/{$endian}pos", $originals[$i] ); 237 $t = unpack( "{$endian}length/{$endian}pos", $translations[$i] ); 238 if ( !$o || !$t ) return false; 239 240 // adjust offset due to reading strings to separate space before 241 $o['pos'] -= $strings_addr; 242 $t['pos'] -= $strings_addr; 243 244 $original = $reader->substr( $strings, $o['pos'], $o['length'] ); 245 $translation = $reader->substr( $strings, $t['pos'], $t['length'] ); 246 247 if ('' === $original) { 248 $this->set_headers($this->make_headers($translation)); 249 } else { 250 $entry = &$this->make_entry($original, $translation); 251 $this->entries[$entry->key()] = &$entry; 252 } 253 } 254 return true; 255 } 256 257 /** 258 * Build a Translation_Entry from original string and translation strings, 259 * found in a MO file 260 * 261 * @static 262 * @param string $original original string to translate from MO file. Might contain 263 * 0x04 as context separator or 0x00 as singular/plural separator 264 * @param string $translation translation string from MO file. Might contain 265 * 0x00 as a plural translations separator 266 */ 267 function &make_entry($original, $translation) { 268 $entry = new Translation_Entry(); 269 // look for context 270 $parts = explode(chr(4), $original); 271 if (isset($parts[1])) { 272 $original = $parts[1]; 273 $entry->context = $parts[0]; 274 } 275 // look for plural original 276 $parts = explode(chr(0), $original); 277 $entry->singular = $parts[0]; 278 if (isset($parts[1])) { 279 $entry->is_plural = true; 280 $entry->plural = $parts[1]; 281 } 282 // plural translations are also separated by \0 283 $entry->translations = explode(chr(0), $translation); 284 return $entry; 285 } 286 287 /** 288 * @param int $count 289 * @return string 290 */ 291 function select_plural_form($count) { 292 return $this->gettext_select_plural_form($count); 293 } 294 295 /** 296 * @return int 297 */ 298 function get_plural_forms_count() { 299 return $this->_nplurals; 300 } 301 } 302 endif;
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Nov 21 01:00:57 2024 | Cross-referenced by PHPXref 0.7.1 |