10 @EXPORT = qw(parse_po_file getLineSortedKeys);
13 my ($status, $foundline, $msgid, $msgstr, $fuzzy);
28 my ($file, $rMessages) = @_;
29 if (open(FI, '<', $file)) {
33 while (my $line = <FI>) {
35 &parse_po_line($line, $lineno, $rMessages, \@result, \$resindex);
39 &parse_po_line("", $lineno + 1, $rMessages, \@result, \$resindex);
41 $result[$resindex] = ["zzzzzzzzzzzz", \@entr1];
47 sub parse_po_line($$$$$)
49 my ($line, $lineno, $rMessages, $rresult, $rresindex) = @_;
52 if ($status eq "normal") {
53 if ($line =~ /^#, fuzzy/) {
56 elsif ($line =~ s/^msgid\s+//) {
57 die("line alternate") if ($alternative);
61 &parse_po_line($line, $lineno, $rMessages, $rresult, $rresindex);
63 elsif ($line =~ s/^\#\~ msgid\s+//) {
68 &parse_po_line($line, $lineno, $rMessages, $rresult, $rresindex);
71 elsif ($status eq "msgid") {
72 if ($line =~ /^\s*"(.*)"\s*/) {
75 elsif ($line =~ /^\#\~\s*"(.*)"\s*/) {
76 die("line not alternate") if (! $alternative);
79 elsif ($line =~ s/^msgstr\s+//) {
83 &parse_po_line($line, $lineno, $rMessages, $rresult, $rresindex);
85 elsif ($line =~ s/^\#\~ msgstr\s+//) {
89 &parse_po_line($line, $lineno, $rMessages, $rresult, $rresindex);
92 elsif ($status eq "msgstr") {
93 if ($line =~ /^\s*"(.*)"\s*/) {
96 elsif ($line =~ /^\#\~\s+"(.*)"\s*/) {
97 die("line not alternate") if (! $alternative);
101 if (!defined($entries{$msgid})) {
103 $rresult->[${$rresindex}] = [$msgid, \@entr1];
104 $entries{$msgid} = $msgstr;
108 print "duplicated alternate entry: \"$msgid\"\n";
111 print "duplicated entry: \"$msgid\"\n";
113 print " on line: $foundline\n";
114 print " original on line: $rMessages->{$msgid}->{line}\n\n";
117 $rMessages->{$msgid}->{line} = $foundline;
118 $rMessages->{$msgid}->{fuzzy} = $fuzzy;
119 $rMessages->{$msgid}->{msgstr} = $msgstr;
120 $rMessages->{$msgid}->{alternative} = $alternative;
121 $rMessages->{$msgid}->{entryidx} = ${$rresindex};
122 ${$rresindex} = ${$rresindex}+1;
128 die("invalid status");
132 sub getLineSortedKeys($)
134 my ($rMessages) = @_;
136 return sort {$rMessages->{$a}->{line} <=> $rMessages->{$b}->{line};} keys %{$rMessages};
154 use parsePoLine; #imports functions 'parse_po_file() and getLineSortedKeys()'
157 my @entries = parse_po_file("sk.po", \%Messages);
161 This is used to parse a single po-file.
164 %Messages The keys in this hash are the msgid-strings
165 the value is reference to a hash wit following values:
166 msgstr: the translated string
167 line: the line-no in the po-file
168 fuzzy: boolean, if the string is fuzzy
169 alternative: if set, so this entry is part of help-strings
170 entryidx: The index in the correspondig @entries array
171 @entries List of references to 2-valued arrays
173 [1]: The sequence of lines from the po-file
174 belonging to this entry.
176 To print the whole po-file:
177 for my $entry (@entries) {
178 print @{$entry->[1]};
181 To get the index to a known $msgid:
182 my $entriesidx = $Messages{$msgid}->{entryidx};
186 Kornel Benko <Kornel.Benko@berlin.de>