Нема описа

po.php 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. <?php
  2. /**
  3. * Class for working with PO files
  4. *
  5. * @version $Id: po.php 1158 2015-11-20 04:31:23Z dd32 $
  6. * @package pomo
  7. * @subpackage po
  8. */
  9. require_once __DIR__ . '/translations.php';
  10. if ( ! defined( 'PO_MAX_LINE_LEN' ) ) {
  11. define( 'PO_MAX_LINE_LEN', 79 );
  12. }
  13. ini_set( 'auto_detect_line_endings', 1 );
  14. /**
  15. * Routines for working with PO files
  16. */
  17. if ( ! class_exists( 'PO', false ) ) :
  18. class PO extends Gettext_Translations {
  19. public $comments_before_headers = '';
  20. /**
  21. * Exports headers to a PO entry
  22. *
  23. * @return string msgid/msgstr PO entry for this PO file headers, doesn't contain newline at the end
  24. */
  25. function export_headers() {
  26. $header_string = '';
  27. foreach ( $this->headers as $header => $value ) {
  28. $header_string .= "$header: $value\n";
  29. }
  30. $poified = PO::poify( $header_string );
  31. if ( $this->comments_before_headers ) {
  32. $before_headers = $this->prepend_each_line( rtrim( $this->comments_before_headers ) . "\n", '# ' );
  33. } else {
  34. $before_headers = '';
  35. }
  36. return rtrim( "{$before_headers}msgid \"\"\nmsgstr $poified" );
  37. }
  38. /**
  39. * Exports all entries to PO format
  40. *
  41. * @return string sequence of mgsgid/msgstr PO strings, doesn't containt newline at the end
  42. */
  43. function export_entries() {
  44. // TODO: Sorting.
  45. return implode( "\n\n", array_map( array( 'PO', 'export_entry' ), $this->entries ) );
  46. }
  47. /**
  48. * Exports the whole PO file as a string
  49. *
  50. * @param bool $include_headers whether to include the headers in the export
  51. * @return string ready for inclusion in PO file string for headers and all the enrtries
  52. */
  53. function export( $include_headers = true ) {
  54. $res = '';
  55. if ( $include_headers ) {
  56. $res .= $this->export_headers();
  57. $res .= "\n\n";
  58. }
  59. $res .= $this->export_entries();
  60. return $res;
  61. }
  62. /**
  63. * Same as {@link export}, but writes the result to a file
  64. *
  65. * @param string $filename Where to write the PO string.
  66. * @param bool $include_headers Whether to include the headers in the export.
  67. * @return bool true on success, false on error
  68. */
  69. function export_to_file( $filename, $include_headers = true ) {
  70. $fh = fopen( $filename, 'w' );
  71. if ( false === $fh ) {
  72. return false;
  73. }
  74. $export = $this->export( $include_headers );
  75. $res = fwrite( $fh, $export );
  76. if ( false === $res ) {
  77. return false;
  78. }
  79. return fclose( $fh );
  80. }
  81. /**
  82. * Text to include as a comment before the start of the PO contents
  83. *
  84. * Doesn't need to include # in the beginning of lines, these are added automatically
  85. *
  86. * @param string $text Text to include as a comment.
  87. */
  88. function set_comment_before_headers( $text ) {
  89. $this->comments_before_headers = $text;
  90. }
  91. /**
  92. * Formats a string in PO-style
  93. *
  94. * @param string $string the string to format
  95. * @return string the poified string
  96. */
  97. public static function poify( $string ) {
  98. $quote = '"';
  99. $slash = '\\';
  100. $newline = "\n";
  101. $replaces = array(
  102. "$slash" => "$slash$slash",
  103. "$quote" => "$slash$quote",
  104. "\t" => '\t',
  105. );
  106. $string = str_replace( array_keys( $replaces ), array_values( $replaces ), $string );
  107. $po = $quote . implode( "${slash}n$quote$newline$quote", explode( $newline, $string ) ) . $quote;
  108. // Add empty string on first line for readbility.
  109. if ( false !== strpos( $string, $newline ) &&
  110. ( substr_count( $string, $newline ) > 1 || substr( $string, -strlen( $newline ) ) !== $newline ) ) {
  111. $po = "$quote$quote$newline$po";
  112. }
  113. // Remove empty strings.
  114. $po = str_replace( "$newline$quote$quote", '', $po );
  115. return $po;
  116. }
  117. /**
  118. * Gives back the original string from a PO-formatted string
  119. *
  120. * @param string $string PO-formatted string
  121. * @return string enascaped string
  122. */
  123. public static function unpoify( $string ) {
  124. $escapes = array(
  125. 't' => "\t",
  126. 'n' => "\n",
  127. 'r' => "\r",
  128. '\\' => '\\',
  129. );
  130. $lines = array_map( 'trim', explode( "\n", $string ) );
  131. $lines = array_map( array( 'PO', 'trim_quotes' ), $lines );
  132. $unpoified = '';
  133. $previous_is_backslash = false;
  134. foreach ( $lines as $line ) {
  135. preg_match_all( '/./u', $line, $chars );
  136. $chars = $chars[0];
  137. foreach ( $chars as $char ) {
  138. if ( ! $previous_is_backslash ) {
  139. if ( '\\' === $char ) {
  140. $previous_is_backslash = true;
  141. } else {
  142. $unpoified .= $char;
  143. }
  144. } else {
  145. $previous_is_backslash = false;
  146. $unpoified .= isset( $escapes[ $char ] ) ? $escapes[ $char ] : $char;
  147. }
  148. }
  149. }
  150. // Standardise the line endings on imported content, technically PO files shouldn't contain \r.
  151. $unpoified = str_replace( array( "\r\n", "\r" ), "\n", $unpoified );
  152. return $unpoified;
  153. }
  154. /**
  155. * Inserts $with in the beginning of every new line of $string and
  156. * returns the modified string
  157. *
  158. * @param string $string prepend lines in this string
  159. * @param string $with prepend lines with this string
  160. */
  161. public static function prepend_each_line( $string, $with ) {
  162. $lines = explode( "\n", $string );
  163. $append = '';
  164. if ( "\n" === substr( $string, -1 ) && '' === end( $lines ) ) {
  165. /*
  166. * Last line might be empty because $string was terminated
  167. * with a newline, remove it from the $lines array,
  168. * we'll restore state by re-terminating the string at the end.
  169. */
  170. array_pop( $lines );
  171. $append = "\n";
  172. }
  173. foreach ( $lines as &$line ) {
  174. $line = $with . $line;
  175. }
  176. unset( $line );
  177. return implode( "\n", $lines ) . $append;
  178. }
  179. /**
  180. * Prepare a text as a comment -- wraps the lines and prepends #
  181. * and a special character to each line
  182. *
  183. * @access private
  184. * @param string $text the comment text
  185. * @param string $char character to denote a special PO comment,
  186. * like :, default is a space
  187. */
  188. public static function comment_block( $text, $char = ' ' ) {
  189. $text = wordwrap( $text, PO_MAX_LINE_LEN - 3 );
  190. return PO::prepend_each_line( $text, "#$char " );
  191. }
  192. /**
  193. * Builds a string from the entry for inclusion in PO file
  194. *
  195. * @param Translation_Entry $entry the entry to convert to po string.
  196. * @return string|false PO-style formatted string for the entry or
  197. * false if the entry is empty
  198. */
  199. public static function export_entry( $entry ) {
  200. if ( null === $entry->singular || '' === $entry->singular ) {
  201. return false;
  202. }
  203. $po = array();
  204. if ( ! empty( $entry->translator_comments ) ) {
  205. $po[] = PO::comment_block( $entry->translator_comments );
  206. }
  207. if ( ! empty( $entry->extracted_comments ) ) {
  208. $po[] = PO::comment_block( $entry->extracted_comments, '.' );
  209. }
  210. if ( ! empty( $entry->references ) ) {
  211. $po[] = PO::comment_block( implode( ' ', $entry->references ), ':' );
  212. }
  213. if ( ! empty( $entry->flags ) ) {
  214. $po[] = PO::comment_block( implode( ', ', $entry->flags ), ',' );
  215. }
  216. if ( $entry->context ) {
  217. $po[] = 'msgctxt ' . PO::poify( $entry->context );
  218. }
  219. $po[] = 'msgid ' . PO::poify( $entry->singular );
  220. if ( ! $entry->is_plural ) {
  221. $translation = empty( $entry->translations ) ? '' : $entry->translations[0];
  222. $translation = PO::match_begin_and_end_newlines( $translation, $entry->singular );
  223. $po[] = 'msgstr ' . PO::poify( $translation );
  224. } else {
  225. $po[] = 'msgid_plural ' . PO::poify( $entry->plural );
  226. $translations = empty( $entry->translations ) ? array( '', '' ) : $entry->translations;
  227. foreach ( $translations as $i => $translation ) {
  228. $translation = PO::match_begin_and_end_newlines( $translation, $entry->plural );
  229. $po[] = "msgstr[$i] " . PO::poify( $translation );
  230. }
  231. }
  232. return implode( "\n", $po );
  233. }
  234. public static function match_begin_and_end_newlines( $translation, $original ) {
  235. if ( '' === $translation ) {
  236. return $translation;
  237. }
  238. $original_begin = "\n" === substr( $original, 0, 1 );
  239. $original_end = "\n" === substr( $original, -1 );
  240. $translation_begin = "\n" === substr( $translation, 0, 1 );
  241. $translation_end = "\n" === substr( $translation, -1 );
  242. if ( $original_begin ) {
  243. if ( ! $translation_begin ) {
  244. $translation = "\n" . $translation;
  245. }
  246. } elseif ( $translation_begin ) {
  247. $translation = ltrim( $translation, "\n" );
  248. }
  249. if ( $original_end ) {
  250. if ( ! $translation_end ) {
  251. $translation .= "\n";
  252. }
  253. } elseif ( $translation_end ) {
  254. $translation = rtrim( $translation, "\n" );
  255. }
  256. return $translation;
  257. }
  258. /**
  259. * @param string $filename
  260. * @return bool
  261. */
  262. function import_from_file( $filename ) {
  263. $f = fopen( $filename, 'r' );
  264. if ( ! $f ) {
  265. return false;
  266. }
  267. $lineno = 0;
  268. while ( true ) {
  269. $res = $this->read_entry( $f, $lineno );
  270. if ( ! $res ) {
  271. break;
  272. }
  273. if ( '' === $res['entry']->singular ) {
  274. $this->set_headers( $this->make_headers( $res['entry']->translations[0] ) );
  275. } else {
  276. $this->add_entry( $res['entry'] );
  277. }
  278. }
  279. PO::read_line( $f, 'clear' );
  280. if ( false === $res ) {
  281. return false;
  282. }
  283. if ( ! $this->headers && ! $this->entries ) {
  284. return false;
  285. }
  286. return true;
  287. }
  288. /**
  289. * Helper function for read_entry
  290. *
  291. * @param string $context
  292. * @return bool
  293. */
  294. protected static function is_final( $context ) {
  295. return ( 'msgstr' === $context ) || ( 'msgstr_plural' === $context );
  296. }
  297. /**
  298. * @param resource $f
  299. * @param int $lineno
  300. * @return null|false|array
  301. */
  302. function read_entry( $f, $lineno = 0 ) {
  303. $entry = new Translation_Entry();
  304. // Where were we in the last step.
  305. // Can be: comment, msgctxt, msgid, msgid_plural, msgstr, msgstr_plural.
  306. $context = '';
  307. $msgstr_index = 0;
  308. while ( true ) {
  309. $lineno++;
  310. $line = PO::read_line( $f );
  311. if ( ! $line ) {
  312. if ( feof( $f ) ) {
  313. if ( self::is_final( $context ) ) {
  314. break;
  315. } elseif ( ! $context ) { // We haven't read a line and EOF came.
  316. return null;
  317. } else {
  318. return false;
  319. }
  320. } else {
  321. return false;
  322. }
  323. }
  324. if ( "\n" === $line ) {
  325. continue;
  326. }
  327. $line = trim( $line );
  328. if ( preg_match( '/^#/', $line, $m ) ) {
  329. // The comment is the start of a new entry.
  330. if ( self::is_final( $context ) ) {
  331. PO::read_line( $f, 'put-back' );
  332. $lineno--;
  333. break;
  334. }
  335. // Comments have to be at the beginning.
  336. if ( $context && 'comment' !== $context ) {
  337. return false;
  338. }
  339. // Add comment.
  340. $this->add_comment_to_entry( $entry, $line );
  341. } elseif ( preg_match( '/^msgctxt\s+(".*")/', $line, $m ) ) {
  342. if ( self::is_final( $context ) ) {
  343. PO::read_line( $f, 'put-back' );
  344. $lineno--;
  345. break;
  346. }
  347. if ( $context && 'comment' !== $context ) {
  348. return false;
  349. }
  350. $context = 'msgctxt';
  351. $entry->context .= PO::unpoify( $m[1] );
  352. } elseif ( preg_match( '/^msgid\s+(".*")/', $line, $m ) ) {
  353. if ( self::is_final( $context ) ) {
  354. PO::read_line( $f, 'put-back' );
  355. $lineno--;
  356. break;
  357. }
  358. if ( $context && 'msgctxt' !== $context && 'comment' !== $context ) {
  359. return false;
  360. }
  361. $context = 'msgid';
  362. $entry->singular .= PO::unpoify( $m[1] );
  363. } elseif ( preg_match( '/^msgid_plural\s+(".*")/', $line, $m ) ) {
  364. if ( 'msgid' !== $context ) {
  365. return false;
  366. }
  367. $context = 'msgid_plural';
  368. $entry->is_plural = true;
  369. $entry->plural .= PO::unpoify( $m[1] );
  370. } elseif ( preg_match( '/^msgstr\s+(".*")/', $line, $m ) ) {
  371. if ( 'msgid' !== $context ) {
  372. return false;
  373. }
  374. $context = 'msgstr';
  375. $entry->translations = array( PO::unpoify( $m[1] ) );
  376. } elseif ( preg_match( '/^msgstr\[(\d+)\]\s+(".*")/', $line, $m ) ) {
  377. if ( 'msgid_plural' !== $context && 'msgstr_plural' !== $context ) {
  378. return false;
  379. }
  380. $context = 'msgstr_plural';
  381. $msgstr_index = $m[1];
  382. $entry->translations[ $m[1] ] = PO::unpoify( $m[2] );
  383. } elseif ( preg_match( '/^".*"$/', $line ) ) {
  384. $unpoified = PO::unpoify( $line );
  385. switch ( $context ) {
  386. case 'msgid':
  387. $entry->singular .= $unpoified;
  388. break;
  389. case 'msgctxt':
  390. $entry->context .= $unpoified;
  391. break;
  392. case 'msgid_plural':
  393. $entry->plural .= $unpoified;
  394. break;
  395. case 'msgstr':
  396. $entry->translations[0] .= $unpoified;
  397. break;
  398. case 'msgstr_plural':
  399. $entry->translations[ $msgstr_index ] .= $unpoified;
  400. break;
  401. default:
  402. return false;
  403. }
  404. } else {
  405. return false;
  406. }
  407. }
  408. $have_translations = false;
  409. foreach ( $entry->translations as $t ) {
  410. if ( $t || ( '0' === $t ) ) {
  411. $have_translations = true;
  412. break;
  413. }
  414. }
  415. if ( false === $have_translations ) {
  416. $entry->translations = array();
  417. }
  418. return array(
  419. 'entry' => $entry,
  420. 'lineno' => $lineno,
  421. );
  422. }
  423. /**
  424. * @param resource $f
  425. * @param string $action
  426. * @return bool
  427. */
  428. function read_line( $f, $action = 'read' ) {
  429. static $last_line = '';
  430. static $use_last_line = false;
  431. if ( 'clear' === $action ) {
  432. $last_line = '';
  433. return true;
  434. }
  435. if ( 'put-back' === $action ) {
  436. $use_last_line = true;
  437. return true;
  438. }
  439. $line = $use_last_line ? $last_line : fgets( $f );
  440. $line = ( "\r\n" === substr( $line, -2 ) ) ? rtrim( $line, "\r\n" ) . "\n" : $line;
  441. $last_line = $line;
  442. $use_last_line = false;
  443. return $line;
  444. }
  445. /**
  446. * @param Translation_Entry $entry
  447. * @param string $po_comment_line
  448. */
  449. function add_comment_to_entry( &$entry, $po_comment_line ) {
  450. $first_two = substr( $po_comment_line, 0, 2 );
  451. $comment = trim( substr( $po_comment_line, 2 ) );
  452. if ( '#:' === $first_two ) {
  453. $entry->references = array_merge( $entry->references, preg_split( '/\s+/', $comment ) );
  454. } elseif ( '#.' === $first_two ) {
  455. $entry->extracted_comments = trim( $entry->extracted_comments . "\n" . $comment );
  456. } elseif ( '#,' === $first_two ) {
  457. $entry->flags = array_merge( $entry->flags, preg_split( '/,\s*/', $comment ) );
  458. } else {
  459. $entry->translator_comments = trim( $entry->translator_comments . "\n" . $comment );
  460. }
  461. }
  462. /**
  463. * @param string $s
  464. * @return string
  465. */
  466. public static function trim_quotes( $s ) {
  467. if ( '"' === substr( $s, 0, 1 ) ) {
  468. $s = substr( $s, 1 );
  469. }
  470. if ( '"' === substr( $s, -1, 1 ) ) {
  471. $s = substr( $s, 0, -1 );
  472. }
  473. return $s;
  474. }
  475. }
  476. endif;