File Coverage

blib/lib/Locale/XGettext/Util/POEntries.pm
Criterion Covered Total %
statement 32 73 43.8
branch 14 50 28.0
condition 4 15 26.6
subroutine 7 10 70.0
pod 0 5 0.0
total 57 153 37.2


line stmt bran cond sub pod time code
1             #! /bin/false
2             # vim: ts=4:et
3              
4             # Copyright (C) 2016-2017 Guido Flohr ,
5             # all rights reserved.
6              
7             # This program is free software; you can redistribute it and/or modify it
8             # under the terms of the GNU Library General Public License as published
9             # by the Free Software Foundation; either version 2, or (at your option)
10             # any later version.
11              
12             # This program is distributed in the hope that it will be useful,
13             # but WITHOUT ANY WARRANTY; without even the implied warranty of
14             # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15             # Library General Public License for more details.
16              
17             # You should have received a copy of the GNU Library General Public
18             # License along with this program; if not, write to the Free Software
19             # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20             # USA.
21              
22             package Locale::XGettext::Util::POEntries;
23             $Locale::XGettext::Util::POEntries::VERSION = '0.8';
24 15     15   109 use strict;
  15         35  
  15         560  
25              
26 15     15   90 use Locale::TextDomain qw(Locale-XGettext);
  15         37  
  15         102  
27              
28             sub new {
29 45     45 0 264 bless {
30             __entries => [],
31             __lookup => {},
32             }, shift;
33             }
34              
35             sub __add {
36 98     98   222 my ($self, $entry, $prepend) = @_;
37              
38 98 50 33     237 my $msgid = $entry->msgid || defined $entry->msgid ? $entry->msgid : '""';
39 98 50 33     1034 my $msgctxt = $entry->msgctxt || defined $entry->msgctxt ? $entry->msgctxt : '""';
40 98 100 66     1079 my $msgid_plural = $entry->msgid_plural
41             || defined $entry->msgid_plural ? $entry->msgid_plural : '""';
42 98         1107 my $existing = $self->{__lookup}->{$msgid}->{$msgctxt};
43 98 50       202 if ($existing) {
44 0         0 $self->__mergeEntries($existing, $entry);
45 0         0 $entry = $existing;
46             } else {
47 98 100       239 if ($prepend) {
48 44         64 unshift @{$self->{__entries}}, $entry;
  44         123  
49             } else {
50 54         88 push @{$self->{__entries}}, $entry;
  54         159  
51             }
52 98         266 $self->{__lookup}->{$msgid}->{$msgctxt} = $entry;
53             }
54              
55 98         244 return $self;
56             }
57              
58             sub add {
59 54     54 0 123 my ($self, $entry) = @_;
60              
61 54 50       133 $entry->msgid('') if !defined $entry->msgid;
62 54 100       418 if (defined $entry->msgid_plural) {
    100          
63 5 50       31 if (!defined $entry->msgstr_n) {
64 5         49 $entry->msgstr_n({0 => '', 1 => ''});
65             }
66 5         197 $entry->msgstr(undef);
67             } elsif (!defined $entry->msgstr) {
68 48 50       624 $entry->msgstr('') if !defined $entry->msgstr;
69             }
70            
71 54         1067 return $self->__add($entry);
72             }
73              
74             sub prepend {
75 44     44 0 104 my ($self, $entry) = @_;
76              
77 44         125 return $self->__add($entry, 1);
78             }
79              
80             sub addEntries {
81 0     0 0 0 my ($self, @entries) = @_;
82              
83 0         0 foreach my $entry (@entries) {
84 0         0 $self->add($entry);
85             }
86              
87 0         0 return $self;
88             }
89              
90             sub entries {
91 144     144 0 235 @{shift->{__entries}};
  144         792  
92             }
93              
94             # This is a simplified merge for merging entries without any translations.
95             sub __mergeEntries {
96 0     0     my ($self, $entry, $overlay) = @_;
97              
98 0 0 0       if (defined $entry->msgid_plural
      0        
99             && defined $overlay->msgid_plural
100             && $entry->msgid_plural ne $overlay->msgid_plural) {
101             # This is a fatal error as GNU gettext cannot grok with
102             # this case.
103             # See https://savannah.gnu.org/bugs/index.php?48411
104 0           $self->__conflict($entry, $overlay,
105             __"conflicting plural forms");
106             }
107              
108             # If one of the two entries currently has no plural form, there is no
109             # problem.
110 0 0         $entry->msgid_plural($overlay->dequote($overlay->msgid_plural))
111             if defined $overlay->msgid_plural;
112              
113 0           my $new_ref = $overlay->reference;
114 0           my $reference = $entry->reference;
115 0           my @lines = split "\n", $reference;
116 0 0         if (!@lines) {
117 0           push @lines, $new_ref;
118             } else {
119 0           my $last_line = $lines[-1];
120 0           my $ref_length = 1 + length $new_ref;
121              
122 0 0         if ($ref_length > 76) {
    0          
123 0           push @lines, $new_ref;
124             } elsif ($ref_length + length $last_line > 76) {
125 0           push @lines, $new_ref;
126             } else {
127 0           $lines[-1] .= ' ' . $new_ref;
128             }
129             }
130              
131 0           $entry->reference(join "\n", @lines);
132              
133 0 0         $entry->fuzzy($overlay->fuzzy) if $overlay->fuzzy;
134 0 0         if (defined $entry->comment) {
135 0           $entry->comment(join "\n", $entry->comment, $overlay->comment);
136             } else {
137 0 0         $entry->comment($overlay->comment) if defined $overlay->comment;
138             }
139 0 0         if (defined $entry->automatic) {
140 0           $entry->automatic(join "\n", $entry->automatic, $overlay->automatic);
141             } else {
142 0 0         $entry->automatic($overlay->automatic) if defined $overlay->automatic;
143             }
144              
145             # Locale::PO does not allow to iterate over the flags. We have to
146             # use the private property directly.
147 0 0         my @flags = @{$overlay->{_flags} || []};
  0            
148 0           foreach my $flag (@flags) {
149 0 0         if ($flag =~ /^no-(.*)/) {
    0          
150 0 0         $self->__conflict($entry, $overlay,
151             __x"conflicting flags")
152             if $entry->has_flag($1);
153             } elsif ($entry->has_flag("no-$flag")) {
154 0           $self->__conflict($entry, $overlay,
155             __x"conflicting flags");
156             }
157 0 0         $entry->add_flag($flag) if !$entry->has_flag($flag);
158             }
159              
160 0           return $self;
161             }
162              
163             sub __conflict {
164 0     0     my ($self, $old, $new, $msg) = @_;
165              
166 0           my $old_ref = $old->reference;
167 0           my $new_ref = $new->reference;
168              
169 0           die __x(< $new_ref, old_ref => $old_ref, msg => $msg);
170             {new_ref}: conflicts with ...
171             {old_ref}: {msg}
172             EOF
173             }
174              
175             1;