PO

The WordPress Core PO class.

Defined (1)

The class is defined in the following location(s).

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