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