po.php 13 KB

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