You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

324 lines
8.3 KiB

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