]> git.lyx.org Git - lyx.git/blob - po/parsePoLine.pm
Polishing diff_po.pl
[lyx.git] / po / parsePoLine.pm
1 package parsePoLine;
2
3 use strict;
4
5 our(@EXPORT, @ISA);
6
7 BEGIN {
8     use Exporter   ();
9     @ISA        = qw(Exporter);
10     @EXPORT     = qw(parse_po_file getLineSortedKeys);
11 }
12
13 my ($status, $foundline, $msgid, $msgstr, $fuzzy);
14
15
16 my $alternative = 0;
17 my @entry = ();
18 my %entries = ();
19
20 sub parse_po_file($$)
21 {
22   $alternative = 0;
23   @entry = ();
24   %entries = ();
25
26   my @result = ();
27   my $resindex = 0;
28   my ($file, $rMessages) = @_;
29   if (open(FI, '<', $file)) {
30     $status = "normal";
31     $fuzzy = 0;
32     my $lineno = 0;
33     while (my $line = <FI>) {
34       $lineno++;
35       &parse_po_line($line, $lineno, $rMessages, \@result, \$resindex);
36       push(@entry, $line);
37
38     }
39     &parse_po_line("", $lineno + 1, $rMessages, \@result, \$resindex);
40     my @entr1 = @entry;
41     $result[$resindex] = ["zzzzzzzzzzzz", \@entr1];
42     close(FI);
43   }
44   return(@result);
45 }
46
47 sub  parse_po_line($$$$$)
48 {
49   my ($line, $lineno, $rMessages, $rresult, $rresindex) = @_;
50   chomp($line);
51
52   if ($status eq "normal") {
53     if ($line =~ /^#, fuzzy/) {
54       $fuzzy = 1;
55     }
56     elsif ($line =~ s/^msgid\s+//) {
57       die("line alternate") if ($alternative);
58       $foundline = $lineno;
59       $status = "msgid";
60       $msgid = "";
61       &parse_po_line($line, $lineno, $rMessages, $rresult, $rresindex);
62     }
63     elsif ($line =~ s/^\#\~ msgid\s+//) {
64       $alternative = 1;
65       $foundline = $lineno;
66       $status = "msgid";
67       $msgid = "";
68       &parse_po_line($line, $lineno, $rMessages, $rresult, $rresindex);
69     }
70   }
71   elsif ($status eq "msgid") {
72     if ($line =~ /^\s*"(.*)"\s*/) {
73       $msgid .= $1;
74     }
75     elsif ($line =~ /^\#\~\s*"(.*)"\s*/) {
76       die("line not alternate") if (! $alternative);
77       $msgid .= $1;
78     }
79     elsif ($line =~ s/^msgstr\s+//) {
80       $alternative = 0;
81       $status = "msgstr";
82       $msgstr = "";
83       &parse_po_line($line, $lineno, $rMessages, $rresult, $rresindex);
84     }
85     elsif ($line =~ s/^\#\~ msgstr\s+//) {
86       $alternative = 1;
87       $status = "msgstr";
88       $msgstr = "";
89       &parse_po_line($line, $lineno, $rMessages, $rresult, $rresindex);
90     }
91   }
92   elsif ($status eq "msgstr") {
93     if ($line =~ /^\s*"(.*)"\s*/) {
94       $msgstr .= $1;
95     }
96     elsif ($line =~ /^\#\~\s+"(.*)"\s*/) {
97       die("line not alternate") if (! $alternative);
98       $msgstr .= $1;
99     }
100     else {
101       if (!defined($entries{$msgid})) {
102         my @entr1 = @entry;
103         $rresult->[${$rresindex}] = [$msgid, \@entr1];
104         $entries{$msgid} = $msgstr;
105       }
106       else {
107         if ($alternative) {
108           print "duplicated alternate entry: \"$msgid\"\n";
109         }
110         else {
111           print "duplicated entry: \"$msgid\"\n";
112         }
113         print " on line:          $foundline\n";
114         print " original on line: $rMessages->{$msgid}->{line}\n\n";
115       }
116       @entry = ();
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;
123       $fuzzy = 0;
124       $status = "normal";
125     }
126   }
127   else {
128     die("invalid status");
129   }
130 }
131
132 sub getLineSortedKeys($)
133 {
134   my ($rMessages) = @_;
135
136   return sort {$rMessages->{$a}->{line} <=> $rMessages->{$b}->{line};} keys %{$rMessages};
137 }
138
139 1;
140
141
142 __END__
143
144 =pod
145
146 =encoding utf8
147
148 =head1 NAME
149
150 parsePoLine
151
152 =head1 SYNOPSIS
153
154   use parsePoLine; #imports functions 'parse_po_file() and getLineSortedKeys()'
155
156   my %Messages = ();
157   my @entries = parse_po_file("sk.po", \%Messages);
158
159 =head1 DESCRIPTION
160
161 This is used to parse a single po-file.
162
163 Results:
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
172         [0]:         msgid-string
173         [1]:         The sequence of lines from the po-file
174                      belonging to this entry.
175
176 To print the whole po-file:
177     for my $entry (@entries) {
178       print @{$entry->[1]};
179     }
180
181 To get the index to a known $msgid:
182     my $entriesidx = $Messages{$msgid}->{entryidx};
183
184 =head1 AUTHOR
185
186 Kornel Benko <Kornel.Benko@berlin.de>