wp/wp-includes/pomo/mo.php
changeset 7 cf61fcea0001
parent 5 5e2f62d02dcd
child 9 177826044cd9
equal deleted inserted replaced
6:490d5cc509ed 7:cf61fcea0001
     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 718 2012-10-31 00:32:02Z nbachiyski $
     5  * @version $Id: mo.php 1157 2015-11-20 04:30:11Z dd32 $
     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';
    11 require_once dirname(__FILE__) . '/streams.php';
    11 require_once dirname(__FILE__) . '/streams.php';
    12 
    12 
    13 if ( !class_exists( 'MO' ) ):
    13 if ( ! class_exists( 'MO', false ) ):
    14 class MO extends Gettext_Translations {
    14 class MO extends Gettext_Translations {
    15 
    15 
    16 	var $_nplurals = 2;
    16 	var $_nplurals = 2;
       
    17 
       
    18 	/**
       
    19 	 * Loaded MO file.
       
    20 	 *
       
    21 	 * @var string
       
    22 	 */
       
    23 	private $filename = '';
       
    24 
       
    25 	/**
       
    26 	 * Returns the loaded MO file.
       
    27 	 *
       
    28 	 * @return string The loaded MO file.
       
    29 	 */
       
    30 	public function get_filename() {
       
    31 		return $this->filename;
       
    32 	}
    17 
    33 
    18 	/**
    34 	/**
    19 	 * Fills up with the entries from MO file $filename
    35 	 * Fills up with the entries from MO file $filename
    20 	 *
    36 	 *
    21 	 * @param string $filename MO file to load
    37 	 * @param string $filename MO file to load
    22 	 */
    38 	 */
    23 	function import_from_file($filename) {
    39 	function import_from_file($filename) {
    24 		$reader = new POMO_FileReader($filename);
    40 		$reader = new POMO_FileReader( $filename );
    25 		if (!$reader->is_resource())
    41 
    26 			return false;
    42 		if ( ! $reader->is_resource() ) {
    27 		return $this->import_from_reader($reader);
    43 			return false;
    28 	}
    44 		}
    29 
    45 
       
    46 		$this->filename = (string) $filename;
       
    47 
       
    48 		return $this->import_from_reader( $reader );
       
    49 	}
       
    50 
       
    51 	/**
       
    52 	 * @param string $filename
       
    53 	 * @return bool
       
    54 	 */
    30 	function export_to_file($filename) {
    55 	function export_to_file($filename) {
    31 		$fh = fopen($filename, 'wb');
    56 		$fh = fopen($filename, 'wb');
    32 		if ( !$fh ) return false;
    57 		if ( !$fh ) return false;
    33 		$res = $this->export_to_file_handle( $fh );
    58 		$res = $this->export_to_file_handle( $fh );
    34 		fclose($fh);
    59 		fclose($fh);
    35 		return $res;
    60 		return $res;
    36 	}
    61 	}
    37 
    62 
       
    63 	/**
       
    64 	 * @return string|false
       
    65 	 */
    38 	function export() {
    66 	function export() {
    39 		$tmp_fh = fopen("php://temp", 'r+');
    67 		$tmp_fh = fopen("php://temp", 'r+');
    40 		if ( !$tmp_fh ) return false;
    68 		if ( !$tmp_fh ) return false;
    41 		$this->export_to_file_handle( $tmp_fh );
    69 		$this->export_to_file_handle( $tmp_fh );
    42 		rewind( $tmp_fh );
    70 		rewind( $tmp_fh );
    43 		return stream_get_contents( $tmp_fh );
    71 		return stream_get_contents( $tmp_fh );
    44 	}
    72 	}
    45 
    73 
       
    74 	/**
       
    75 	 * @param Translation_Entry $entry
       
    76 	 * @return bool
       
    77 	 */
    46 	function is_entry_good_for_export( $entry ) {
    78 	function is_entry_good_for_export( $entry ) {
    47 		if ( empty( $entry->translations ) ) {
    79 		if ( empty( $entry->translations ) ) {
    48 			return false;
    80 			return false;
    49 		}
    81 		}
    50 
    82 
    53 		}
    85 		}
    54 
    86 
    55 		return true;
    87 		return true;
    56 	}
    88 	}
    57 
    89 
       
    90 	/**
       
    91 	 * @param resource $fh
       
    92 	 * @return true
       
    93 	 */
    58 	function export_to_file_handle($fh) {
    94 	function export_to_file_handle($fh) {
    59 		$entries = array_filter( $this->entries, array( $this, 'is_entry_good_for_export' ) );
    95 		$entries = array_filter( $this->entries, array( $this, 'is_entry_good_for_export' ) );
    60 		ksort($entries);
    96 		ksort($entries);
    61 		$magic = 0x950412de;
    97 		$magic = 0x950412de;
    62 		$revision = 0;
    98 		$revision = 0;
    99 		fwrite($fh, $originals_table);
   135 		fwrite($fh, $originals_table);
   100 		fwrite($fh, $translations_table);
   136 		fwrite($fh, $translations_table);
   101 		return true;
   137 		return true;
   102 	}
   138 	}
   103 
   139 
       
   140 	/**
       
   141 	 * @param Translation_Entry $entry
       
   142 	 * @return string
       
   143 	 */
   104 	function export_original($entry) {
   144 	function export_original($entry) {
   105 		//TODO: warnings for control characters
   145 		//TODO: warnings for control characters
   106 		$exported = $entry->singular;
   146 		$exported = $entry->singular;
   107 		if ($entry->is_plural) $exported .= chr(0).$entry->plural;
   147 		if ($entry->is_plural) $exported .= chr(0).$entry->plural;
   108 		if (!is_null($entry->context)) $exported = $entry->context . chr(4) . $exported;
   148 		if ($entry->context) $exported = $entry->context . chr(4) . $exported;
   109 		return $exported;
   149 		return $exported;
   110 	}
   150 	}
   111 
   151 
       
   152 	/**
       
   153 	 * @param Translation_Entry $entry
       
   154 	 * @return string
       
   155 	 */
   112 	function export_translations($entry) {
   156 	function export_translations($entry) {
   113 		//TODO: warnings for control characters
   157 		//TODO: warnings for control characters
   114 		return implode(chr(0), $entry->translations);
   158 		return $entry->is_plural ? implode(chr(0), $entry->translations) : $entry->translations[0];
   115 	}
   159 	}
   116 
   160 
       
   161 	/**
       
   162 	 * @return string
       
   163 	 */
   117 	function export_headers() {
   164 	function export_headers() {
   118 		$exported = '';
   165 		$exported = '';
   119 		foreach($this->headers as $header => $value) {
   166 		foreach($this->headers as $header => $value) {
   120 			$exported.= "$header: $value\n";
   167 			$exported.= "$header: $value\n";
   121 		}
   168 		}
   122 		return $exported;
   169 		return $exported;
   123 	}
   170 	}
   124 
   171 
       
   172 	/**
       
   173 	 * @param int $magic
       
   174 	 * @return string|false
       
   175 	 */
   125 	function get_byteorder($magic) {
   176 	function get_byteorder($magic) {
   126 		// The magic is 0x950412de
   177 		// The magic is 0x950412de
   127 
   178 
   128 		// bug in PHP 5.0.2, see https://savannah.nongnu.org/bugs/?func=detailitem&item_id=10565
   179 		// bug in PHP 5.0.2, see https://savannah.nongnu.org/bugs/?func=detailitem&item_id=10565
   129 		$magic_little = (int) - 1794895138;
   180 		$magic_little = (int) - 1794895138;
   252 		// plural translations are also separated by \0
   303 		// plural translations are also separated by \0
   253 		$entry->translations = explode(chr(0), $translation);
   304 		$entry->translations = explode(chr(0), $translation);
   254 		return $entry;
   305 		return $entry;
   255 	}
   306 	}
   256 
   307 
       
   308 	/**
       
   309 	 * @param int $count
       
   310 	 * @return string
       
   311 	 */
   257 	function select_plural_form($count) {
   312 	function select_plural_form($count) {
   258 		return $this->gettext_select_plural_form($count);
   313 		return $this->gettext_select_plural_form($count);
   259 	}
   314 	}
   260 
   315 
       
   316 	/**
       
   317 	 * @return int
       
   318 	 */
   261 	function get_plural_forms_count() {
   319 	function get_plural_forms_count() {
   262 		return $this->_nplurals;
   320 		return $this->_nplurals;
   263 	}
   321 	}
   264 }
   322 }
   265 endif;
   323 endif;