11 @EXPORT = qw(parse_po_file getLineSortedKeys);
15 sub parse_po_file($\%);
16 sub parse_po_line($$$$$ );
17 sub getLineSortedKeys(\%);
20 my ($status, $foundline, $msgid, $msgstr, $fuzzy);
27 sub parse_po_file($\%)
35 my ($file, $rMessages) = @_;
36 if (open(FI, '<', $file)) {
40 while (my $line = <FI>) {
42 parse_po_line($line, $lineno, $rMessages, \@result, \$resindex);
46 parse_po_line("", $lineno + 1, $rMessages, \@result, \$resindex);
48 $result[$resindex] = ["zzzzzzzzzzzz", \@entr1];
54 sub parse_po_line($$$$$)
56 my ($line, $lineno, $rMessages, $rresult, $rresindex) = @_;
59 if ($status eq "normal") {
60 if ($line =~ /^#, fuzzy/) {
63 elsif ($line =~ s/^msgid\s+//) {
64 die("line alternate") if ($alternative);
68 parse_po_line($line, $lineno, $rMessages, $rresult, $rresindex);
70 elsif ($line =~ s/^\#\~ msgid\s+//) {
75 parse_po_line($line, $lineno, $rMessages, $rresult, $rresindex);
78 elsif ($status eq "msgid") {
79 if ($line =~ /^\s*"(.*)"\s*/) {
82 elsif ($line =~ /^\#\~\s*"(.*)"\s*/) {
83 die("line not alternate") if (! $alternative);
86 elsif ($line =~ s/^msgstr\s+//) {
90 parse_po_line($line, $lineno, $rMessages, $rresult, $rresindex);
92 elsif ($line =~ s/^\#\~ msgstr\s+//) {
96 parse_po_line($line, $lineno, $rMessages, $rresult, $rresindex);
99 elsif ($status eq "msgstr") {
100 if ($line =~ /^\s*"(.*)"\s*/) {
103 elsif ($line =~ /^\#\~\s+"(.*)"\s*/) {
104 die("line not alternate") if (! $alternative);
108 if (!defined($entries{$msgid})) {
110 $rresult->[${$rresindex}] = [$msgid, \@entr1];
111 $entries{$msgid} = $msgstr;
115 print "duplicated alternate entry: \"$msgid\"\n";
118 print "duplicated entry: \"$msgid\"\n";
120 print " on line: $foundline\n";
121 print " original on line: $rMessages->{$msgid}->{line}\n\n";
124 $rMessages->{$msgid}->{line} = $foundline;
125 $rMessages->{$msgid}->{fuzzy} = $fuzzy;
126 $rMessages->{$msgid}->{msgstr} = $msgstr;
127 $rMessages->{$msgid}->{alternative} = $alternative;
128 $rMessages->{$msgid}->{entryidx} = ${$rresindex};
129 ${$rresindex} = ${$rresindex}+1;
135 die("invalid status");
139 sub getLineSortedKeys(\%)
141 my ($rMessages) = @_;
143 return sort {$rMessages->{$a}->{line} <=> $rMessages->{$b}->{line};} keys %{$rMessages};
161 use parsePoLine; #imports functions 'parse_po_file() and getLineSortedKeys()'
164 my @entries = parse_po_file("sk.po", %Messages);
168 This is used to parse a single po-file.
171 %Messages The keys in this hash are the msgid-strings
172 the value is reference to a hash wit following values:
173 msgstr: the translated string
174 line: the line-no in the po-file
175 fuzzy: boolean, if the string is fuzzy
176 alternative: if set, so this entry is part of help-strings
177 entryidx: The index in the correspondig @entries array
178 @entries List of references to 2-valued arrays
180 [1]: The sequence of lines from the po-file
181 belonging to this entry.
183 To print the whole po-file:
184 for my $entry (@entries) {
185 print @{$entry->[1]};
188 To get the index to a known $msgid:
189 my $entriesidx = $Messages{$msgid}->{entryidx};
193 Kornel Benko <Kornel.Benko@berlin.de>