PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-includes/pomo/po.php

https://bitbucket.org/aqge/deptandashboard
PHP | 377 lines | 280 code | 19 blank | 78 comment | 72 complexity | 9ac1709c3dc3ba1e5d9c71aa72657b0f MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * Class for working with PO files
  4. *
  5. * @version $Id: po.php 589 2010-12-18 01:40:57Z nbachiyski $
  6. * @package pomo
  7. * @subpackage po
  8. */
  9. require_once dirname(__FILE__) . '/translations.php';
  10. define('PO_MAX_LINE_LEN', 79);
  11. ini_set('auto_detect_line_endings', 1);
  12. /**
  13. * Routines for working with PO files
  14. */
  15. if ( !class_exists( 'PO' ) ):
  16. class PO extends Gettext_Translations {
  17. var $comments_before_headers = '';
  18. /**
  19. * Exports headers to a PO entry
  20. *
  21. * @return string msgid/msgstr PO entry for this PO file headers, doesn't contain newline at the end
  22. */
  23. function export_headers() {
  24. $header_string = '';
  25. foreach($this->headers as $header => $value) {
  26. $header_string.= "$header: $value\n";
  27. }
  28. $poified = PO::poify($header_string);
  29. if ($this->comments_before_headers)
  30. $before_headers = $this->prepend_each_line(rtrim($this->comments_before_headers)."\n", '# ');
  31. else
  32. $before_headers = '';
  33. return rtrim("{$before_headers}msgid \"\"\nmsgstr $poified");
  34. }
  35. /**
  36. * Exports all entries to PO format
  37. *
  38. * @return string sequence of mgsgid/msgstr PO strings, doesn't containt newline at the end
  39. */
  40. function export_entries() {
  41. //TODO sorting
  42. return implode("\n\n", array_map(array('PO', 'export_entry'), $this->entries));
  43. }
  44. /**
  45. * Exports the whole PO file as a string
  46. *
  47. * @param bool $include_headers whether to include the headers in the export
  48. * @return string ready for inclusion in PO file string for headers and all the enrtries
  49. */
  50. function export($include_headers = true) {
  51. $res = '';
  52. if ($include_headers) {
  53. $res .= $this->export_headers();
  54. $res .= "\n\n";
  55. }
  56. $res .= $this->export_entries();
  57. return $res;
  58. }
  59. /**
  60. * Same as {@link export}, but writes the result to a file
  61. *
  62. * @param string $filename where to write the PO string
  63. * @param bool $include_headers whether to include tje headers in the export
  64. * @return bool true on success, false on error
  65. */
  66. function export_to_file($filename, $include_headers = true) {
  67. $fh = fopen($filename, 'w');
  68. if (false === $fh) return false;
  69. $export = $this->export($include_headers);
  70. $res = fwrite($fh, $export);
  71. if (false === $res) return false;
  72. return fclose($fh);
  73. }
  74. /**
  75. * Text to include as a comment before the start of the PO contents
  76. *
  77. * Doesn't need to include # in the beginning of lines, these are added automatically
  78. */
  79. function set_comment_before_headers( $text ) {
  80. $this->comments_before_headers = $text;
  81. }
  82. /**
  83. * Formats a string in PO-style
  84. *
  85. * @static
  86. * @param string $string the string to format
  87. * @return string the poified string
  88. */
  89. function poify($string) {
  90. $quote = '"';
  91. $slash = '\\';
  92. $newline = "\n";
  93. $replaces = array(
  94. "$slash" => "$slash$slash",
  95. "$quote" => "$slash$quote",
  96. "\t" => '\t',
  97. );
  98. $string = str_replace(array_keys($replaces), array_values($replaces), $string);
  99. $po = $quote.implode("${slash}n$quote$newline$quote", explode($newline, $string)).$quote;
  100. // add empty string on first line for readbility
  101. if (false !== strpos($string, $newline) &&
  102. (substr_count($string, $newline) > 1 || !($newline === substr($string, -strlen($newline))))) {
  103. $po = "$quote$quote$newline$po";
  104. }
  105. // remove empty strings
  106. $po = str_replace("$newline$quote$quote", '', $po);
  107. return $po;
  108. }
  109. /**
  110. * Gives back the original string from a PO-formatted string
  111. *
  112. * @static
  113. * @param string $string PO-formatted string
  114. * @return string enascaped string
  115. */
  116. function unpoify($string) {
  117. $escapes = array('t' => "\t", 'n' => "\n", '\\' => '\\');
  118. $lines = array_map('trim', explode("\n", $string));
  119. $lines = array_map(array('PO', 'trim_quotes'), $lines);
  120. $unpoified = '';
  121. $previous_is_backslash = false;
  122. foreach($lines as $line) {
  123. preg_match_all('/./u', $line, $chars);
  124. $chars = $chars[0];
  125. foreach($chars as $char) {
  126. if (!$previous_is_backslash) {
  127. if ('\\' == $char)
  128. $previous_is_backslash = true;
  129. else
  130. $unpoified .= $char;
  131. } else {
  132. $previous_is_backslash = false;
  133. $unpoified .= isset($escapes[$char])? $escapes[$char] : $char;
  134. }
  135. }
  136. }
  137. return $unpoified;
  138. }
  139. /**
  140. * Inserts $with in the beginning of every new line of $string and
  141. * returns the modified string
  142. *
  143. * @static
  144. * @param string $string prepend lines in this string
  145. * @param string $with prepend lines with this string
  146. */
  147. function prepend_each_line($string, $with) {
  148. $php_with = var_export($with, true);
  149. $lines = explode("\n", $string);
  150. // do not prepend the string on the last empty line, artefact by explode
  151. if ("\n" == substr($string, -1)) unset($lines[count($lines) - 1]);
  152. $res = implode("\n", array_map(create_function('$x', "return $php_with.\$x;"), $lines));
  153. // give back the empty line, we ignored above
  154. if ("\n" == substr($string, -1)) $res .= "\n";
  155. return $res;
  156. }
  157. /**
  158. * Prepare a text as a comment -- wraps the lines and prepends #
  159. * and a special character to each line
  160. *
  161. * @access private
  162. * @param string $text the comment text
  163. * @param string $char character to denote a special PO comment,
  164. * like :, default is a space
  165. */
  166. function comment_block($text, $char=' ') {
  167. $text = wordwrap($text, PO_MAX_LINE_LEN - 3);
  168. return PO::prepend_each_line($text, "#$char ");
  169. }
  170. /**
  171. * Builds a string from the entry for inclusion in PO file
  172. *
  173. * @static
  174. * @param object &$entry the entry to convert to po string
  175. * @return string|bool PO-style formatted string for the entry or
  176. * false if the entry is empty
  177. */
  178. function export_entry(&$entry) {
  179. if (is_null($entry->singular)) return false;
  180. $po = array();
  181. if (!empty($entry->translator_comments)) $po[] = PO::comment_block($entry->translator_comments);
  182. if (!empty($entry->extracted_comments)) $po[] = PO::comment_block($entry->extracted_comments, '.');
  183. if (!empty($entry->references)) $po[] = PO::comment_block(implode(' ', $entry->references), ':');
  184. if (!empty($entry->flags)) $po[] = PO::comment_block(implode(", ", $entry->flags), ',');
  185. if (!is_null($entry->context)) $po[] = 'msgctxt '.PO::poify($entry->context);
  186. $po[] = 'msgid '.PO::poify($entry->singular);
  187. if (!$entry->is_plural) {
  188. $translation = empty($entry->translations)? '' : $entry->translations[0];
  189. $po[] = 'msgstr '.PO::poify($translation);
  190. } else {
  191. $po[] = 'msgid_plural '.PO::poify($entry->plural);
  192. $translations = empty($entry->translations)? array('', '') : $entry->translations;
  193. foreach($translations as $i => $translation) {
  194. $po[] = "msgstr[$i] ".PO::poify($translation);
  195. }
  196. }
  197. return implode("\n", $po);
  198. }
  199. function import_from_file($filename) {
  200. $f = fopen($filename, 'r');
  201. if (!$f) return false;
  202. $lineno = 0;
  203. while (true) {
  204. $res = $this->read_entry($f, $lineno);
  205. if (!$res) break;
  206. if ($res['entry']->singular == '') {
  207. $this->set_headers($this->make_headers($res['entry']->translations[0]));
  208. } else {
  209. $this->add_entry($res['entry']);
  210. }
  211. }
  212. PO::read_line($f, 'clear');
  213. return $res !== false;
  214. }
  215. function read_entry($f, $lineno = 0) {
  216. $entry = new Translation_Entry();
  217. // where were we in the last step
  218. // can be: comment, msgctxt, msgid, msgid_plural, msgstr, msgstr_plural
  219. $context = '';
  220. $msgstr_index = 0;
  221. $is_final = create_function('$context', 'return $context == "msgstr" || $context == "msgstr_plural";');
  222. while (true) {
  223. $lineno++;
  224. $line = PO::read_line($f);
  225. if (!$line) {
  226. if (feof($f)) {
  227. if ($is_final($context))
  228. break;
  229. elseif (!$context) // we haven't read a line and eof came
  230. return null;
  231. else
  232. return false;
  233. } else {
  234. return false;
  235. }
  236. }
  237. if ($line == "\n") continue;
  238. $line = trim($line);
  239. if (preg_match('/^#/', $line, $m)) {
  240. // the comment is the start of a new entry
  241. if ($is_final($context)) {
  242. PO::read_line($f, 'put-back');
  243. $lineno--;
  244. break;
  245. }
  246. // comments have to be at the beginning
  247. if ($context && $context != 'comment') {
  248. return false;
  249. }
  250. // add comment
  251. $this->add_comment_to_entry($entry, $line);;
  252. } elseif (preg_match('/^msgctxt\s+(".*")/', $line, $m)) {
  253. if ($is_final($context)) {
  254. PO::read_line($f, 'put-back');
  255. $lineno--;
  256. break;
  257. }
  258. if ($context && $context != 'comment') {
  259. return false;
  260. }
  261. $context = 'msgctxt';
  262. $entry->context .= PO::unpoify($m[1]);
  263. } elseif (preg_match('/^msgid\s+(".*")/', $line, $m)) {
  264. if ($is_final($context)) {
  265. PO::read_line($f, 'put-back');
  266. $lineno--;
  267. break;
  268. }
  269. if ($context && $context != 'msgctxt' && $context != 'comment') {
  270. return false;
  271. }
  272. $context = 'msgid';
  273. $entry->singular .= PO::unpoify($m[1]);
  274. } elseif (preg_match('/^msgid_plural\s+(".*")/', $line, $m)) {
  275. if ($context != 'msgid') {
  276. return false;
  277. }
  278. $context = 'msgid_plural';
  279. $entry->is_plural = true;
  280. $entry->plural .= PO::unpoify($m[1]);
  281. } elseif (preg_match('/^msgstr\s+(".*")/', $line, $m)) {
  282. if ($context != 'msgid') {
  283. return false;
  284. }
  285. $context = 'msgstr';
  286. $entry->translations = array(PO::unpoify($m[1]));
  287. } elseif (preg_match('/^msgstr\[(\d+)\]\s+(".*")/', $line, $m)) {
  288. if ($context != 'msgid_plural' && $context != 'msgstr_plural') {
  289. return false;
  290. }
  291. $context = 'msgstr_plural';
  292. $msgstr_index = $m[1];
  293. $entry->translations[$m[1]] = PO::unpoify($m[2]);
  294. } elseif (preg_match('/^".*"$/', $line)) {
  295. $unpoified = PO::unpoify($line);
  296. switch ($context) {
  297. case 'msgid':
  298. $entry->singular .= $unpoified; break;
  299. case 'msgctxt':
  300. $entry->context .= $unpoified; break;
  301. case 'msgid_plural':
  302. $entry->plural .= $unpoified; break;
  303. case 'msgstr':
  304. $entry->translations[0] .= $unpoified; break;
  305. case 'msgstr_plural':
  306. $entry->translations[$msgstr_index] .= $unpoified; break;
  307. default:
  308. return false;
  309. }
  310. } else {
  311. return false;
  312. }
  313. }
  314. if (array() == array_filter($entry->translations, create_function('$t', 'return $t || "0" === $t;'))) {
  315. $entry->translations = array();
  316. }
  317. return array('entry' => $entry, 'lineno' => $lineno);
  318. }
  319. function read_line($f, $action = 'read') {
  320. static $last_line = '';
  321. static $use_last_line = false;
  322. if ('clear' == $action) {
  323. $last_line = '';
  324. return true;
  325. }
  326. if ('put-back' == $action) {
  327. $use_last_line = true;
  328. return true;
  329. }
  330. $line = $use_last_line? $last_line : fgets($f);
  331. $last_line = $line;
  332. $use_last_line = false;
  333. return $line;
  334. }
  335. function add_comment_to_entry(&$entry, $po_comment_line) {
  336. $first_two = substr($po_comment_line, 0, 2);
  337. $comment = trim(substr($po_comment_line, 2));
  338. if ('#:' == $first_two) {
  339. $entry->references = array_merge($entry->references, preg_split('/\s+/', $comment));
  340. } elseif ('#.' == $first_two) {
  341. $entry->extracted_comments = trim($entry->extracted_comments . "\n" . $comment);
  342. } elseif ('#,' == $first_two) {
  343. $entry->flags = array_merge($entry->flags, preg_split('/,\s*/', $comment));
  344. } else {
  345. $entry->translator_comments = trim($entry->translator_comments . "\n" . $comment);
  346. }
  347. }
  348. function trim_quotes($s) {
  349. if ( substr($s, 0, 1) == '"') $s = substr($s, 1);
  350. if ( substr($s, -1, 1) == '"') $s = substr($s, 0, -1);
  351. return $s;
  352. }
  353. }
  354. endif;