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