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