Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[RT#66264] Fixed handling of obsolete+fuzzy strings

Provided by Joshua Miller in the RT ticket.
  • Loading branch information...
commit a239d262d22cfb833ec8f470930132c4e958def6 1 parent a44fbe9
Joshua Miller authored committed

Showing 1 changed file with 96 additions and 6 deletions. Show diff stats Hide diff stats

  1. +96 6 lib/Locale/PO.pm
102 lib/Locale/PO.pm
... ... @@ -1,7 +1,7 @@
1 1 package Locale::PO;
2 2 use strict;
3 3 use warnings;
4   -our $VERSION = '0.21';
  4 +our $VERSION = '0.22';
5 5
6 6 use Carp;
7 7
@@ -12,6 +12,9 @@ sub new {
12 12 my $self = {};
13 13 bless $self, $class;
14 14 $self->_flags([]);
  15 + $self->fuzzy_msgid( $options{'-fuzzy_msgid'} ) if defined( $options{'-fuzzy_msgid'} );
  16 + $self->fuzzy_msgid_plural( $options{'-fuzzy_msgid_plural'} )
  17 + if defined( $options{'-fuzzy_msgid_plural'} );
15 18 $self->msgid( $options{'-msgid'} ) if defined( $options{'-msgid'} );
16 19 $self->msgid_plural( $options{'-msgid_plural'} )
17 20 if defined( $options{'-msgid_plural'} );
@@ -19,6 +22,7 @@ sub new {
19 22 $self->msgstr_n( $options{'-msgstr_n'} )
20 23 if defined( $options{'-msgstr_n'} );
21 24 $self->msgctxt( $options{'-msgctxt'} ) if defined( $options{'-msgctxt'} );
  25 + $self->fuzzy_msgctxt( $options{'-fuzzy_msgctxt'} ) if defined( $options{'-fuzzy_msgctxt'} );
22 26 $self->comment( $options{'-comment'} ) if defined( $options{'-comment'} );
23 27 $self->fuzzy( $options{'-fuzzy'} ) if defined( $options{'-fuzzy'} );
24 28 $self->automatic( $options{'-automatic'} )
@@ -33,6 +37,24 @@ sub new {
33 37 return $self;
34 38 }
35 39
  40 +sub fuzzy_msgctxt {
  41 + my $self = shift;
  42 + @_ ? $self->{'fuzzy_msgctxt'} = $self->quote(shift) : $self->{'fuzzy_msgctxt'};
  43 +}
  44 +
  45 +sub fuzzy_msgid {
  46 + my $self = shift;
  47 + @_ ? $self->{'fuzzy_msgid'} = $self->quote(shift) : $self->{'fuzzy_msgid'};
  48 +}
  49 +
  50 +sub fuzzy_msgid_plural {
  51 + my $self = shift;
  52 + @_
  53 + ? $self->{'fuzzy_msgid_plural'} =
  54 + $self->quote(shift)
  55 + : $self->{'fuzzy_msgid_plural'};
  56 +}
  57 +
36 58 sub msgctxt {
37 59 my $self = shift;
38 60 @_ ? $self->{'msgctxt'} = $self->quote(shift) : $self->{'msgctxt'};
@@ -207,9 +229,23 @@ sub _normalize_str {
207 229 }
208 230 }
209 231
  232 +sub _fuzzy_normalize_str {
  233 + my $self = shift;
  234 + my $string = shift;
  235 + my $prefix = shift;
  236 +
  237 + my $normalized = $self->_normalize_str($string);
  238 +
  239 + # on newlines, start them with "#| " or "#~| "
  240 + $normalized =~ s/\n"/\n$prefix"/g;
  241 +
  242 + return $normalized;
  243 +}
  244 +
210 245 sub dump {
211 246 my $self = shift;
212 247 my $obsolete = $self->obsolete? '#~ ' : '';
  248 + my $fuzzy_prefix = $self->obsolete? '#~| ' : '#| ';
213 249 my $dump;
214 250 $dump = $self->_dump_multi_comment( $self->comment, "# " )
215 251 if ( $self->comment );
@@ -225,6 +261,16 @@ sub dump {
225 261 }
226 262 $dump .= "#$flags\n" if length $flags;
227 263
  264 + $dump .= "${fuzzy_prefix}msgctxt "
  265 + . $self->_fuzzy_normalize_str( $self->fuzzy_msgctxt, $fuzzy_prefix )
  266 + if $self->fuzzy_msgctxt;
  267 + $dump .= "${fuzzy_prefix}msgid "
  268 + . $self->_fuzzy_normalize_str( $self->fuzzy_msgid, $fuzzy_prefix )
  269 + if $self->fuzzy_msgid;
  270 + $dump .= "${fuzzy_prefix}msgid_plural "
  271 + . $self->_fuzzy_normalize_str( $self->fuzzy_msgid_plural, $fuzzy_prefix )
  272 + if $self->fuzzy_msgid_plural;
  273 +
228 274 $dump .= "${obsolete}msgctxt " . $self->_normalize_str( $self->msgctxt )
229 275 if $self->msgctxt;
230 276 $dump .= "${obsolete}msgid " . $self->_normalize_str( $self->msgid );
@@ -331,6 +377,9 @@ sub _load_file {
331 377
332 378 if ( defined($po) ) {
333 379
  380 + $po->fuzzy_msgctxt( $buffer{fuzzy_msgctxt} ) if defined $buffer{fuzzy_msgctxt};
  381 + $po->fuzzy_msgid( $buffer{fuzzy_msgid} ) if defined $buffer{fuzzy_msgid};
  382 + $po->fuzzy_msgid_plural( $buffer{fuzzy_msgid_plural} ) if defined $buffer{fuzzy_msgid_plural};
334 383 $po->msgctxt( $buffer{msgctxt} ) if defined $buffer{msgctxt};
335 384 $po->msgid( $buffer{msgid} ) if defined $buffer{msgid};
336 385 $po->msgid_plural( $buffer{msgid_plural} ) if defined $buffer{msgid_plural};
@@ -396,6 +445,24 @@ sub _load_file {
396 445 $po->add_flag($flag);
397 446 }
398 447 }
  448 + elsif (/^#(~)?\|\s+msgctxt\s+(.*)/) {
  449 + $po = $class->new( -loaded_line_number => $line_number ) unless defined($po);
  450 + $buffer{fuzzy_msgctxt} = $self->dequote($2);
  451 + $last_buffer = \$buffer{fuzzy_msgctxt};
  452 + $po->obsolete(1) if $1;
  453 + }
  454 + elsif (/^#(~)?\|\s+msgid\s+(.*)/) {
  455 + $po = $class->new( -loaded_line_number => $line_number ) unless defined($po);
  456 + $buffer{fuzzy_msgid} = $self->dequote($2);
  457 + $last_buffer = \$buffer{fuzzy_msgid};
  458 + $po->obsolete(1) if $1;
  459 + }
  460 + elsif (/^#(~)?\|\s+msgid_plural\s+(.*)/) {
  461 + $po = $class->new( -loaded_line_number => $line_number ) unless defined($po);
  462 + $buffer{fuzzy_msgid_plural} = $self->dequote($2);
  463 + $last_buffer = \$buffer{fuzzy_msgid_plural};
  464 + $po->obsolete(1) if $1;
  465 + }
399 466 elsif (/^(#~\s+)?msgctxt\s+(.*)/) {
400 467 $po = $class->new( -loaded_line_number => $line_number ) unless defined($po);
401 468 $buffer{msgctxt} = $self->dequote($2);
@@ -426,10 +493,14 @@ sub _load_file {
426 493 $buffer{msgstr_n}{$1} = $self->dequote($2);
427 494 $last_buffer = \$buffer{msgstr_n}{$1};
428 495 }
429   - elsif (/^(?:#~\s+)?"/) {
430   -
431   - # contined string
432   - $$last_buffer .= $self->dequote($_);
  496 + elsif (/^(?:#(?:~|~\||\|)\s+)?(".*)/) {
  497 +
  498 + # continued string. Accounts for:
  499 + # normal : "string"
  500 + # obsolete : #~ "string"
  501 + # fuzzy : #| "string"
  502 + # fuzzy+obsolete : #~| "string"
  503 + $$last_buffer .= $self->dequote($1);
433 504 }
434 505 else {
435 506 warn "Strange line at $file line $line_number: $_\n";
@@ -529,7 +600,8 @@ a list/hash of the form:
529 600
530 601 -option=>value, -option=>value, etc.
531 602
532   -Where options are msgid, msgstr, msgctxt, comment, automatic, reference,
  603 +Where options are msgid, msgid_plural, msgstr, msgctxt, comment, automatic,
  604 +reference, fuzzy_msgctxt, fuzzy_msgid, fuzzy_msgid_plural,
533 605 fuzzy, and c-format. See accessor methods below.
534 606
535 607 To generate a po file header, add an entry with an empty
@@ -583,6 +655,24 @@ Set or get the translation context string from the object.
583 655
584 656 This method expects the new string in unquoted form but returns the current string in quoted form.
585 657
  658 +=item fuzzy_msgid
  659 +
  660 +Set or get the outdated untranslated string from the object.
  661 +
  662 +This method expects the new string in unquoted form but returns the current string in quoted form.
  663 +
  664 +=item fuzzy_msgid_plural
  665 +
  666 +Set or get the outdated untranslated plural string from the object.
  667 +
  668 +This method expects the new string in unquoted form but returns the current string in quoted form.
  669 +
  670 +=item fuzzy_msgctxt
  671 +
  672 +Set or get the outdated translation context string from the object.
  673 +
  674 +This method expects the new string in unquoted form but returns the current string in quoted form.
  675 +
586 676 =item obsolete
587 677
588 678 Returns 1 if the entry is obsolete.

0 comments on commit a239d26

Please sign in to comment.
Something went wrong with that request. Please try again.