Changeset 35714 for trunk/src/wp-includes/pomo/po.php
- Timestamp:
- 11/20/2015 04:33:12 AM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/pomo/po.php
r34348 r35714 3 3 * Class for working with PO files 4 4 * 5 * @version $Id: po.php 718 2012-10-31 00:32:02Z nbachiyski$5 * @version $Id: po.php 1158 2015-11-20 04:31:23Z dd32 $ 6 6 * @package pomo 7 7 * @subpackage po … … 10 10 require_once dirname(__FILE__) . '/translations.php'; 11 11 12 define('PO_MAX_LINE_LEN', 79); 12 if ( ! defined( 'PO_MAX_LINE_LEN' ) ) { 13 define('PO_MAX_LINE_LEN', 79); 14 } 13 15 14 16 ini_set('auto_detect_line_endings', 1); … … 98 100 * @return string the poified string 99 101 */ 100 function poify($string) {102 public static function poify($string) { 101 103 $quote = '"'; 102 104 $slash = '\\'; … … 129 131 * @return string enascaped string 130 132 */ 131 function unpoify($string) {132 $escapes = array('t' => "\t", 'n' => "\n", ' \\' => '\\');133 public static function unpoify($string) { 134 $escapes = array('t' => "\t", 'n' => "\n", 'r' => "\r", '\\' => '\\'); 133 135 $lines = array_map('trim', explode("\n", $string)); 134 136 $lines = array_map(array('PO', 'trim_quotes'), $lines); … … 150 152 } 151 153 } 154 155 // Standardise the line endings on imported content, technically PO files shouldn't contain \r 156 $unpoified = str_replace( array( "\r\n", "\r" ), "\n", $unpoified ); 157 152 158 return $unpoified; 153 159 } … … 161 167 * @param string $with prepend lines with this string 162 168 */ 163 function prepend_each_line($string, $with) {169 public static function prepend_each_line($string, $with) { 164 170 $php_with = var_export($with, true); 165 171 $lines = explode("\n", $string); … … 181 187 * like :, default is a space 182 188 */ 183 function comment_block($text, $char=' ') {189 public static function comment_block($text, $char=' ') { 184 190 $text = wordwrap($text, PO_MAX_LINE_LEN - 3); 185 191 return PO::prepend_each_line($text, "#$char "); … … 194 200 * false if the entry is empty 195 201 */ 196 function export_entry(&$entry) {197 if ( is_null($entry->singular)) return false;202 public static function export_entry(&$entry) { 203 if ( null === $entry->singular || '' === $entry->singular ) return false; 198 204 $po = array(); 199 205 if (!empty($entry->translator_comments)) $po[] = PO::comment_block($entry->translator_comments); … … 201 207 if (!empty($entry->references)) $po[] = PO::comment_block(implode(' ', $entry->references), ':'); 202 208 if (!empty($entry->flags)) $po[] = PO::comment_block(implode(", ", $entry->flags), ','); 203 if ( !is_null($entry->context)) $po[] = 'msgctxt '.PO::poify($entry->context);209 if ($entry->context) $po[] = 'msgctxt '.PO::poify($entry->context); 204 210 $po[] = 'msgid '.PO::poify($entry->singular); 205 211 if (!$entry->is_plural) { 206 212 $translation = empty($entry->translations)? '' : $entry->translations[0]; 213 $translation = PO::match_begin_and_end_newlines( $translation, $entry->singular ); 207 214 $po[] = 'msgstr '.PO::poify($translation); 208 215 } else { … … 210 217 $translations = empty($entry->translations)? array('', '') : $entry->translations; 211 218 foreach($translations as $i => $translation) { 219 $translation = PO::match_begin_and_end_newlines( $translation, $entry->plural ); 212 220 $po[] = "msgstr[$i] ".PO::poify($translation); 213 221 } 214 222 } 215 223 return implode("\n", $po); 224 } 225 226 public static function match_begin_and_end_newlines( $translation, $original ) { 227 if ( '' === $translation ) { 228 return $translation; 229 } 230 231 $original_begin = "\n" === substr( $original, 0, 1 ); 232 $original_end = "\n" === substr( $original, -1 ); 233 $translation_begin = "\n" === substr( $translation, 0, 1 ); 234 $translation_end = "\n" === substr( $translation, -1 ); 235 236 if ( $original_begin ) { 237 if ( ! $translation_begin ) { 238 $translation = "\n" . $translation; 239 } 240 } elseif ( $translation_begin ) { 241 $translation = ltrim( $translation, "\n" ); 242 } 243 244 if ( $original_end ) { 245 if ( ! $translation_end ) { 246 $translation .= "\n"; 247 } 248 } elseif ( $translation_end ) { 249 $translation = rtrim( $translation, "\n" ); 250 } 251 252 return $translation; 216 253 } 217 254 … … 401 438 * @return sring 402 439 */ 403 function trim_quotes($s) {440 public static function trim_quotes($s) { 404 441 if ( substr($s, 0, 1) == '"') $s = substr($s, 1); 405 442 if ( substr($s, -1, 1) == '"') $s = substr($s, 0, -1);
Note: See TracChangeset
for help on using the changeset viewer.