Chameleon

Chameleon Svn Source Tree

Root/tags/2.3/package/bin/po4a/lib/Locale/Po4a/Debconf.pm

Source at commit 2862 created 7 years 24 days ago.
By ifabio, Tag 2.3 release, bump svn to 2.4
1#!/usr/bin/perl -w
2
3# Po4a::Debconf.pm
4#
5# extract and translate translatable strings from debconf templates
6#
7# This program is free software; you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation; either version 2 of the License, or
10# (at your option) 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
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program; if not, write to the Free Software
19# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20#
21########################################################################
22
23=encoding UTF-8
24
25=head1 NAME
26
27Locale::Po4a::Debconf - convert debconf templates from/to PO files
28
29=head1 DESCRIPTION
30
31The po4a (PO for anything) project goal is to ease translations (and more
32interestingly, the maintenance of translations) using gettext tools on
33areas where they were not expected like documentation.
34
35Locale::Po4a::Debconf is a module to help the translation of the debconf
36templates into other [human] languages.
37
38=head1 OPTIONS ACCEPTED BY THIS MODULE
39
40NONE.
41
42=head1 STATUS OF THIS MODULE
43
44Not tested.
45
46
47DO NOT USE THIS MODULE TO PRODUCE TEMPLATES. It's only good to extract data.
48
49
50=cut
51
52# Note that the following works. It may help to write a multi-translate
53
54# sub toto {
55# do shift;
56# }
57# toto({print "ok"});
58
59
60package Locale::Po4a::Debconf;
61
62use 5.006;
63use strict;
64use warnings;
65
66require Exporter;
67use vars qw(@ISA @EXPORT);
68@ISA = qw(Locale::Po4a::TransTractor);
69@EXPORT = qw();
70
71use Locale::Po4a::TransTractor;
72use Locale::Po4a::Common;
73
74
75sub initialize {}
76
77sub parse {
78 my $self = shift;
79
80 my ($line,$lref);
81
82 my ($field, $value, $extended,$ref,$type)=('', '', '', '','');
83 my $verb = 0; # whether we are in verbatim mode
84
85 my $escape = sub {
86my $str=shift;
87$str =~ s/"/\\"/g;
88return $str;
89 };
90
91 # function in charge of pushing the accumulated material to output
92 my $handle_field = sub {
93my $field=shift;
94my $value=shift;
95my $extended=shift;
96my $ref = shift;
97my $type = shift;
98
99$field =~ s/^(_*)(.*)$/$2/;
100my $undercount = length($1) || 0; # number of _ leading the field name
101
102# Only one leading _: regular translated field
103if ($undercount == 1) {
104
105 # the untranslated field
106 $self->pushline("$field: $value");
107 map {$self->pushline(' '.($_||'.'))} split (/\n/,$extended);
108
109
110 my $eval='$self->pushline("'.$field.'[FIXME:LANGCODE.ENCODING]: "'; # what to multi-eval
111 $eval .= '.$self->translate("'.$escape->($value)."\",\"$ref\",\"$type/$field\",wrap=>1)".'."\n".'."\n";
112
113 my $count = 0;
114 foreach my $para (split(/\n\n/, $extended)) {
115my $wrap = 1;
116if ($para =~ /(^|\n)\s/m) {
117 $wrap = 0;
118}
119$eval .= ($count?'.':'');
120$count ++;
121$eval .= '$self->translate("'.$escape->($para)."\",\"$ref\",\"$type/$field\[$count\]\",wrap=>$wrap)"."\n";
122 }
123
124 $eval .= ")\n";
125 print STDERR $eval if $self->debug();
126 eval $eval;
127 print STDERR "XXXXXXXXXXXXXXXXX\n" if $self->debug();
128
129
130# two leading _: split on coma and multi-translate each part. No extended value.
131} elsif ($undercount == 2) {
132 $self->pushline("$field: $value"); # the untranslated field
133
134 my $eval='$self->pushline("'.$field.'FIXME[LANGCODE]: "'; # what to multi-eval
135
136 my $first = 1;
137 for my $part (split(/(?<!\\), */, $value, 0))
138 {
139$part =~ s/\\,/,/g;
140$eval .= ($first?'':'.", "').'.$self->translate("'.$escape->($part)."\",\"$ref\",\"$type/$field chunk\",wrap=>1)";
141$first = 0;
142 }
143 $eval .= ")\n";
144
145 print $eval if $self->debug();
146 eval $eval;
147
148# no leading _: don't touch it
149} else {
150 $self->pushline("$field: $value");
151 map {$self->pushline(' '.($_||'.'))} split (/\n/,$extended);
152}
153 };
154
155 # main loop
156 ($line,$lref)=$self->shiftline();
157
158 while (defined($line)) {
159# a new field (within a stanza)
160if ($line=~/^([-_.A-Za-z0-9]*):\s?(.*)/) {
161
162 $handle_field->($field, $value, $extended, $ref,$type); # deal with previously accumulated
163 ($field, $value, $extended,$verb)=('', '', '', 0);
164
165 $field=$1;
166 $value=$2;
167 $value=~s/\s*$//;
168 $extended='';
169 $ref=$lref;
170
171 $type = $value if $field eq 'Type';
172
173 die wrap_mod("po4a::debconf", dgettext("po4a", "Translated field in master document: %s"), $field)
174 if $field =~ m/-/;
175
176# paragraph separator within extended value
177} elsif ($line=~/^\s\.$/) {
178 $extended.="\n\n";
179
180# continuation of extended value
181} elsif ($line=~/^\s(.*)/) {
182
183 my $bit=$1;
184 $verb = 1 if ($bit =~ m/^\s/);
185
186 $bit=~s/\s*$//;
187
188 $extended .= ($verb ? "\n" : ' ') if length $extended && $extended !~ /[\n ]$/;
189 $extended .= $bit.($verb ? "\n" : "");
190
191# this may be an empty line closing the stanza, a comment or even a parse error (if file not DebConf-clean).
192} else {
193
194 $handle_field->($field, $value, $extended, $ref,$type);
195 ($field, $value, $extended,$verb)=('', '', '', 0);
196
197 $self->pushline($line);
198
199}
200
201($line,$lref)=$self->shiftline();
202 }
203
204 $handle_field->($field, $value, $extended, $ref,$type);
205}
206
2071;
208
209=head1 AUTHORS
210
211This module is loosely inspired from both po-debconf and debconf code. The
212adaptation for po4a was done by:
213
214 Martin Quinson (mquinson#debian.org)
215
216=head1 COPYRIGHT AND LICENSE
217
218 Copyright 2005 by SPI, inc.
219
220This program is free software; you may redistribute it and/or modify it
221under the terms of GPL (see the COPYING file).
222

Archive Download this file

Revision: 2862