web/wp-includes/pomo/mo.php
changeset 194 32102edaa81b
parent 136 bde1974c263b
child 204 09a1c134465b
equal deleted inserted replaced
193:2f6f6f7551ca 194:32102edaa81b
     1 <?php
     1 <?php
     2 /**
     2 /**
     3  * Class for working with MO files
     3  * Class for working with MO files
     4  *
     4  *
     5  * @version $Id: mo.php 293 2009-11-12 15:43:50Z nbachiyski $
     5  * @version $Id: mo.php 602 2011-01-30 12:43:29Z nbachiyski $
     6  * @package pomo
     6  * @package pomo
     7  * @subpackage mo
     7  * @subpackage mo
     8  */
     8  */
     9 
     9 
    10 require_once dirname(__FILE__) . '/translations.php';
    10 require_once dirname(__FILE__) . '/translations.php';
    24 		$reader = new POMO_FileReader($filename);
    24 		$reader = new POMO_FileReader($filename);
    25 		if (!$reader->is_resource())
    25 		if (!$reader->is_resource())
    26 			return false;
    26 			return false;
    27 		return $this->import_from_reader($reader);
    27 		return $this->import_from_reader($reader);
    28 	}
    28 	}
    29 	
    29 
    30 	function export_to_file($filename) {
    30 	function export_to_file($filename) {
    31 		$fh = fopen($filename, 'wb');
    31 		$fh = fopen($filename, 'wb');
    32 		if ( !$fh ) return false;
    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) {
    33 		$entries = array_filter($this->entries, create_function('$e', 'return !empty($e->translations);'));
    47 		$entries = array_filter($this->entries, create_function('$e', 'return !empty($e->translations);'));
    34 		ksort($entries);
    48 		ksort($entries);
    35 		$magic = 0x950412de;
    49 		$magic = 0x950412de;
    36 		$revision = 0;
    50 		$revision = 0;
    37 		$total = count($entries) + 1; // all the headers are one entry
    51 		$total = count($entries) + 1; // all the headers are one entry
    68 			$current_addr += $length + 1;
    82 			$current_addr += $length + 1;
    69 		}
    83 		}
    70 		
    84 		
    71 		fwrite($fh, $originals_table);
    85 		fwrite($fh, $originals_table);
    72 		fwrite($fh, $translations_table);
    86 		fwrite($fh, $translations_table);
    73 		fclose($fh);
    87 		return true;
    74 	}
    88 	}
    75 	
    89 	
    76 	function export_original($entry) {
    90 	function export_original($entry) {
    77 		//TODO: warnings for control characters
    91 		//TODO: warnings for control characters
    78 		$exported = $entry->singular;
    92 		$exported = $entry->singular;
   179 
   193 
   180 			$original    = $reader->substr( $strings, $o['pos'], $o['length'] );
   194 			$original    = $reader->substr( $strings, $o['pos'], $o['length'] );
   181 			$translation = $reader->substr( $strings, $t['pos'], $t['length'] );
   195 			$translation = $reader->substr( $strings, $t['pos'], $t['length'] );
   182 
   196 
   183 			if ('' === $original) {
   197 			if ('' === $original) {
   184 				$this->set_headers($this->make_headers($translation));
   198 				$headers = $this->make_headers($translation);
       
   199 				$this->set_headers($headers);
   185 			} else {
   200 			} else {
   186 				$entry = &$this->make_entry($original, $translation);
   201 				$entry = &$this->make_entry($original, $translation);
   187 				$this->entries[$entry->key()] = &$entry;
   202 				$this->entries[$entry->key()] = &$entry;
   188 			}
   203 			}
   189 		}
   204 		}
   199 	 * 	0x04 as context separator or 0x00 as singular/plural separator
   214 	 * 	0x04 as context separator or 0x00 as singular/plural separator
   200 	 * @param string $translation translation string from MO file. Might contain
   215 	 * @param string $translation translation string from MO file. Might contain
   201 	 * 	0x00 as a plural translations separator
   216 	 * 	0x00 as a plural translations separator
   202 	 */
   217 	 */
   203 	function &make_entry($original, $translation) {
   218 	function &make_entry($original, $translation) {
   204 		$entry = & new Translation_Entry();
   219 		$entry = new Translation_Entry();
   205 		// look for context
   220 		// look for context
   206 		$parts = explode(chr(4), $original);
   221 		$parts = explode(chr(4), $original);
   207 		if (isset($parts[1])) {
   222 		if (isset($parts[1])) {
   208 			$original = $parts[1];
   223 			$original = $parts[1];
   209 			$entry->context = $parts[0];
   224 			$entry->context = $parts[0];