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