115 ); |
117 ); |
116 |
118 |
117 $string = str_replace( array_keys( $replaces ), array_values( $replaces ), $string ); |
119 $string = str_replace( array_keys( $replaces ), array_values( $replaces ), $string ); |
118 |
120 |
119 $po = $quote . implode( "${slash}n$quote$newline$quote", explode( $newline, $string ) ) . $quote; |
121 $po = $quote . implode( "${slash}n$quote$newline$quote", explode( $newline, $string ) ) . $quote; |
120 // add empty string on first line for readbility |
122 // Add empty string on first line for readbility. |
121 if ( false !== strpos( $string, $newline ) && |
123 if ( false !== strpos( $string, $newline ) && |
122 ( substr_count( $string, $newline ) > 1 || ! ( $newline === substr( $string, -strlen( $newline ) ) ) ) ) { |
124 ( substr_count( $string, $newline ) > 1 || substr( $string, -strlen( $newline ) ) !== $newline ) ) { |
123 $po = "$quote$quote$newline$po"; |
125 $po = "$quote$quote$newline$po"; |
124 } |
126 } |
125 // remove empty strings |
127 // Remove empty strings. |
126 $po = str_replace( "$newline$quote$quote", '', $po ); |
128 $po = str_replace( "$newline$quote$quote", '', $po ); |
127 return $po; |
129 return $po; |
128 } |
130 } |
129 |
131 |
130 /** |
132 /** |
313 * |
317 * |
314 * @param string $context |
318 * @param string $context |
315 * @return bool |
319 * @return bool |
316 */ |
320 */ |
317 protected static function is_final( $context ) { |
321 protected static function is_final( $context ) { |
318 return ( $context === 'msgstr' ) || ( $context === 'msgstr_plural' ); |
322 return ( 'msgstr' === $context ) || ( 'msgstr_plural' === $context ); |
319 } |
323 } |
320 |
324 |
321 /** |
325 /** |
322 * @param resource $f |
326 * @param resource $f |
323 * @param int $lineno |
327 * @param int $lineno |
324 * @return null|false|array |
328 * @return null|false|array |
325 */ |
329 */ |
326 function read_entry( $f, $lineno = 0 ) { |
330 function read_entry( $f, $lineno = 0 ) { |
327 $entry = new Translation_Entry(); |
331 $entry = new Translation_Entry(); |
328 // where were we in the last step |
332 // Where were we in the last step. |
329 // can be: comment, msgctxt, msgid, msgid_plural, msgstr, msgstr_plural |
333 // Can be: comment, msgctxt, msgid, msgid_plural, msgstr, msgstr_plural. |
330 $context = ''; |
334 $context = ''; |
331 $msgstr_index = 0; |
335 $msgstr_index = 0; |
332 while ( true ) { |
336 while ( true ) { |
333 $lineno++; |
337 $lineno++; |
334 $line = PO::read_line( $f ); |
338 $line = PO::read_line( $f ); |
335 if ( ! $line ) { |
339 if ( ! $line ) { |
336 if ( feof( $f ) ) { |
340 if ( feof( $f ) ) { |
337 if ( self::is_final( $context ) ) { |
341 if ( self::is_final( $context ) ) { |
338 break; |
342 break; |
339 } elseif ( ! $context ) { // we haven't read a line and eof came |
343 } elseif ( ! $context ) { // We haven't read a line and EOF came. |
340 return null; |
344 return null; |
341 } else { |
345 } else { |
342 return false; |
346 return false; |
343 } |
347 } |
344 } else { |
348 } else { |
345 return false; |
349 return false; |
346 } |
350 } |
347 } |
351 } |
348 if ( $line == "\n" ) { |
352 if ( "\n" === $line ) { |
349 continue; |
353 continue; |
350 } |
354 } |
351 $line = trim( $line ); |
355 $line = trim( $line ); |
352 if ( preg_match( '/^#/', $line, $m ) ) { |
356 if ( preg_match( '/^#/', $line, $m ) ) { |
353 // the comment is the start of a new entry |
357 // The comment is the start of a new entry. |
354 if ( self::is_final( $context ) ) { |
358 if ( self::is_final( $context ) ) { |
355 PO::read_line( $f, 'put-back' ); |
359 PO::read_line( $f, 'put-back' ); |
356 $lineno--; |
360 $lineno--; |
357 break; |
361 break; |
358 } |
362 } |
359 // comments have to be at the beginning |
363 // Comments have to be at the beginning. |
360 if ( $context && $context != 'comment' ) { |
364 if ( $context && 'comment' !== $context ) { |
361 return false; |
365 return false; |
362 } |
366 } |
363 // add comment |
367 // Add comment. |
364 $this->add_comment_to_entry( $entry, $line ); |
368 $this->add_comment_to_entry( $entry, $line ); |
365 } elseif ( preg_match( '/^msgctxt\s+(".*")/', $line, $m ) ) { |
369 } elseif ( preg_match( '/^msgctxt\s+(".*")/', $line, $m ) ) { |
366 if ( self::is_final( $context ) ) { |
370 if ( self::is_final( $context ) ) { |
367 PO::read_line( $f, 'put-back' ); |
371 PO::read_line( $f, 'put-back' ); |
368 $lineno--; |
372 $lineno--; |
369 break; |
373 break; |
370 } |
374 } |
371 if ( $context && $context != 'comment' ) { |
375 if ( $context && 'comment' !== $context ) { |
372 return false; |
376 return false; |
373 } |
377 } |
374 $context = 'msgctxt'; |
378 $context = 'msgctxt'; |
375 $entry->context .= PO::unpoify( $m[1] ); |
379 $entry->context .= PO::unpoify( $m[1] ); |
376 } elseif ( preg_match( '/^msgid\s+(".*")/', $line, $m ) ) { |
380 } elseif ( preg_match( '/^msgid\s+(".*")/', $line, $m ) ) { |
377 if ( self::is_final( $context ) ) { |
381 if ( self::is_final( $context ) ) { |
378 PO::read_line( $f, 'put-back' ); |
382 PO::read_line( $f, 'put-back' ); |
379 $lineno--; |
383 $lineno--; |
380 break; |
384 break; |
381 } |
385 } |
382 if ( $context && $context != 'msgctxt' && $context != 'comment' ) { |
386 if ( $context && 'msgctxt' !== $context && 'comment' !== $context ) { |
383 return false; |
387 return false; |
384 } |
388 } |
385 $context = 'msgid'; |
389 $context = 'msgid'; |
386 $entry->singular .= PO::unpoify( $m[1] ); |
390 $entry->singular .= PO::unpoify( $m[1] ); |
387 } elseif ( preg_match( '/^msgid_plural\s+(".*")/', $line, $m ) ) { |
391 } elseif ( preg_match( '/^msgid_plural\s+(".*")/', $line, $m ) ) { |
388 if ( $context != 'msgid' ) { |
392 if ( 'msgid' !== $context ) { |
389 return false; |
393 return false; |
390 } |
394 } |
391 $context = 'msgid_plural'; |
395 $context = 'msgid_plural'; |
392 $entry->is_plural = true; |
396 $entry->is_plural = true; |
393 $entry->plural .= PO::unpoify( $m[1] ); |
397 $entry->plural .= PO::unpoify( $m[1] ); |
394 } elseif ( preg_match( '/^msgstr\s+(".*")/', $line, $m ) ) { |
398 } elseif ( preg_match( '/^msgstr\s+(".*")/', $line, $m ) ) { |
395 if ( $context != 'msgid' ) { |
399 if ( 'msgid' !== $context ) { |
396 return false; |
400 return false; |
397 } |
401 } |
398 $context = 'msgstr'; |
402 $context = 'msgstr'; |
399 $entry->translations = array( PO::unpoify( $m[1] ) ); |
403 $entry->translations = array( PO::unpoify( $m[1] ) ); |
400 } elseif ( preg_match( '/^msgstr\[(\d+)\]\s+(".*")/', $line, $m ) ) { |
404 } elseif ( preg_match( '/^msgstr\[(\d+)\]\s+(".*")/', $line, $m ) ) { |
401 if ( $context != 'msgid_plural' && $context != 'msgstr_plural' ) { |
405 if ( 'msgid_plural' !== $context && 'msgstr_plural' !== $context ) { |
402 return false; |
406 return false; |
403 } |
407 } |
404 $context = 'msgstr_plural'; |
408 $context = 'msgstr_plural'; |
405 $msgstr_index = $m[1]; |
409 $msgstr_index = $m[1]; |
406 $entry->translations[ $m[1] ] = PO::unpoify( $m[2] ); |
410 $entry->translations[ $m[1] ] = PO::unpoify( $m[2] ); |
446 'lineno' => $lineno, |
450 'lineno' => $lineno, |
447 ); |
451 ); |
448 } |
452 } |
449 |
453 |
450 /** |
454 /** |
451 * @staticvar string $last_line |
455 * @param resource $f |
452 * @staticvar boolean $use_last_line |
456 * @param string $action |
453 * |
|
454 * @param resource $f |
|
455 * @param string $action |
|
456 * @return boolean |
457 * @return boolean |
457 */ |
458 */ |
458 function read_line( $f, $action = 'read' ) { |
459 function read_line( $f, $action = 'read' ) { |
459 static $last_line = ''; |
460 static $last_line = ''; |
460 static $use_last_line = false; |
461 static $use_last_line = false; |
461 if ( 'clear' == $action ) { |
462 if ( 'clear' === $action ) { |
462 $last_line = ''; |
463 $last_line = ''; |
463 return true; |
464 return true; |
464 } |
465 } |
465 if ( 'put-back' == $action ) { |
466 if ( 'put-back' === $action ) { |
466 $use_last_line = true; |
467 $use_last_line = true; |
467 return true; |
468 return true; |
468 } |
469 } |
469 $line = $use_last_line ? $last_line : fgets( $f ); |
470 $line = $use_last_line ? $last_line : fgets( $f ); |
470 $line = ( "\r\n" == substr( $line, -2 ) ) ? rtrim( $line, "\r\n" ) . "\n" : $line; |
471 $line = ( "\r\n" === substr( $line, -2 ) ) ? rtrim( $line, "\r\n" ) . "\n" : $line; |
471 $last_line = $line; |
472 $last_line = $line; |
472 $use_last_line = false; |
473 $use_last_line = false; |
473 return $line; |
474 return $line; |
474 } |
475 } |
475 |
476 |
478 * @param string $po_comment_line |
479 * @param string $po_comment_line |
479 */ |
480 */ |
480 function add_comment_to_entry( &$entry, $po_comment_line ) { |
481 function add_comment_to_entry( &$entry, $po_comment_line ) { |
481 $first_two = substr( $po_comment_line, 0, 2 ); |
482 $first_two = substr( $po_comment_line, 0, 2 ); |
482 $comment = trim( substr( $po_comment_line, 2 ) ); |
483 $comment = trim( substr( $po_comment_line, 2 ) ); |
483 if ( '#:' == $first_two ) { |
484 if ( '#:' === $first_two ) { |
484 $entry->references = array_merge( $entry->references, preg_split( '/\s+/', $comment ) ); |
485 $entry->references = array_merge( $entry->references, preg_split( '/\s+/', $comment ) ); |
485 } elseif ( '#.' == $first_two ) { |
486 } elseif ( '#.' === $first_two ) { |
486 $entry->extracted_comments = trim( $entry->extracted_comments . "\n" . $comment ); |
487 $entry->extracted_comments = trim( $entry->extracted_comments . "\n" . $comment ); |
487 } elseif ( '#,' == $first_two ) { |
488 } elseif ( '#,' === $first_two ) { |
488 $entry->flags = array_merge( $entry->flags, preg_split( '/,\s*/', $comment ) ); |
489 $entry->flags = array_merge( $entry->flags, preg_split( '/,\s*/', $comment ) ); |
489 } else { |
490 } else { |
490 $entry->translator_comments = trim( $entry->translator_comments . "\n" . $comment ); |
491 $entry->translator_comments = trim( $entry->translator_comments . "\n" . $comment ); |
491 } |
492 } |
492 } |
493 } |
493 |
494 |
494 /** |
495 /** |
495 * @param string $s |
496 * @param string $s |
496 * @return sring |
497 * @return string |
497 */ |
498 */ |
498 public static function trim_quotes( $s ) { |
499 public static function trim_quotes( $s ) { |
499 if ( substr( $s, 0, 1 ) == '"' ) { |
500 if ( '"' === substr( $s, 0, 1 ) ) { |
500 $s = substr( $s, 1 ); |
501 $s = substr( $s, 1 ); |
501 } |
502 } |
502 if ( substr( $s, -1, 1 ) == '"' ) { |
503 if ( '"' === substr( $s, -1, 1 ) ) { |
503 $s = substr( $s, 0, -1 ); |
504 $s = substr( $s, 0, -1 ); |
504 } |
505 } |
505 return $s; |
506 return $s; |
506 } |
507 } |
507 } |
508 } |