MO

The WordPress Core MO class.

Defined (1)

The class is defined in the following location(s).

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