Debian Codegraph @

debconf-getlang

raw
1 #!/usr/bin/perl
2
3 =head1 NAME
4
5 debconf-getlang - extract a language from a templates file
6
7 =cut
8
9 use warnings;
10 use strict;
11 use Debconf::Template::Transient;
12 use Getopt::Long;
13
14 print STDERR "debconf-getlang: This utility is deprecated; you should switch to using the po-debconf package.\n";
15
16 =head1 SYNOPSIS
17
18 debconf-getlang lang master [translation]
19 debconf-getlang --stats master translation [...]
20
21 =head1 DESCRIPTION
22
23 Note: This utility is deprecated; you should switch to using the po-debconf package.
24
25 This program helps make and manage translations of debconf templates.
26 There are basically three situations in which this program might be called:
27
28 =over 4
29
30 =item A translation is just being started.
31
32 You want to provide the translator with a file they can work on that has the
33 English fields from your templates file, plus blank Field-ll fields for the
34 target language that they can fill in.
35
36 To do this, run the program with first parameter being the code for the
37 language that is being translated to, and the second parameter being the
38 filename of the English templates file.
39
40 =item A translation is well under way.
41
42 You have changed some English text, or added more items to your templates
43 file, and you want to send the translators a file with the English text plus
44 their current translations (or you are the translator, and you want to
45 generate such a file for your own use).
46
47 To accomplish this, run the program with the first parameter being the the
48 code for the language that is being translated to, the second parameter
49 being the filename of the master English templates file, and the third
50 parameter being the filename of the current translated file.
51
52 When run this way, the program is smart enough to notice fuzzy
53 translations. For example a fuzzy Description will be output as
54 Description-<lang>-fuzzy, and a new, blank Description-<lang> will be added.
55 Translators should remove the -fuzzy fields as they correct the fuzzy
56 translations.
57
58 =item Checking the status of a translation
59
60 To check the status of a translation, use the --status flag, and pass
61 the english template file as the first parameter, and all the other
62 translated templates after that. It will output statistics for each of
63 them. For example:
64
65 debconf-getlang --stats debian/templates debian/templates.*
66
67 =back
68
69 =head1 NOTE
70
71 Note that the text in the generated templates may be word-wrapped by
72 debconf.
73
74 =head1 SEE ALSO
75
76 L<debconf-mergetemplate(1)>
77
78 =cut
79
80 sub usage {
81 print STDERR <<EOF;
82 Usage:
83 debconf-getlang lang master [translation]
84 debconf-getlang --stats master translation [...]
85 EOF
86 exit 1;
87 }
88
89 # Ignore the users locale settings.
90 Debconf::Template::Transient->i18n(0);
91
92 my $stats=0;
93 GetOptions(
94 'stats|s' => \$stats,
95 ) or usage;
96
97 if ($stats) {
98 my $master=shift || usage;
99 foreach my $fn (@ARGV) {
100 my ($lang)=$fn=~m/\.([^.]+)$/;
101 getlang($lang, $master, $fn);
102 }
103 }
104 else {
105 my $lang=shift || usage;
106 my $fn=shift || usage;
107 my $lfn=shift;
108 getlang($lang, $fn, $lfn);
109 }
110
111 sub getlang {
112 my $lang=lc(shift);
113 my $fn=shift;
114 my $lfn=shift;
115
116 # First read in the master file.
117 my %master;
118 my @out;
119 foreach my $in (Debconf::Template::Transient->load($fn)) {
120 my $out=Debconf::Template::Transient->new($in->template);
121
122 foreach my $field ($in->fields) {
123 next if $field eq 'template';
124 if ($field =~ m/-(..(_..)?)$/) {
125 if ($1 eq $lang) {
126 $out->$field($in->$field());
127 }
128 # Skip fields that are for some other language.
129 }
130 else {
131 $out->$field($in->$field());
132 }
133 }
134
135 $master{$out->template}=$out;
136 push @out, $out;
137 }
138
139 # Now read in the localized file, if there is one.
140 my %localized;
141 if ($lfn) {
142 foreach my $in (Debconf::Template::Transient->load($lfn)) {
143 $localized{$in->template}=$in;
144 }
145 }
146
147 # Now go merge together the master template and any corresponding
148 # localized template. Determine which translations are good, and
149 # which are fuzzy. Finally, stub in any fields that should exist
150 # for the target language, but do not yet exist. This gets complex
151 # because only some fields need to be translated, depending on
152 # template type.
153
154 my $good=0;
155 my $fuzzy=0;
156 my $bad=0;
157
158 foreach my $master (@out) {
159 my $localized=$localized{$master->template};
160
161 # Work out what fields need to be translated.
162 my @needfields=qw{description};
163 if ($master->type !~ /^((multi)?select|note|error|boolean)$/) {
164 push @needfields, 'default' if defined $master->default;
165 }
166 if ($master->type =~ /(multi)?select/) {
167 push @needfields, 'choices';
168 }
169
170 # Merge in localized, including fuzzy translations.
171 #
172 # Note that extended_ fields are looked for, and if they exist,
173 # compared along with the field they extend. That's what makes
174 # the code ugly... it probably points out that Templates need
175 # to handle extended values better, allowing either the short
176 # part only, or the extended part only, or both, to be retreived
177 # easily. TODO
178 if ($localized) {
179 foreach my $field (@needfields) {
180 my $field_lang="$field-$lang";
181 my $field_ext="extended_$field";
182 my $field_lang_ext="extended_$field_lang";
183
184 my $value=$master->$field;
185 next unless defined $value;
186 next unless defined $localized->$field_lang;
187 $value.="\n".$master->$field_ext
188 if defined $master->$field_ext;
189 my $lvalue='';
190 $lvalue=$localized->$field
191 if defined $localized->$field;
192 $lvalue.="\n".$localized->$field_ext
193 if defined $localized->$field_ext;
194
195 # Ignore leading/trailing whitespace,
196 # and collapse other whitespace.
197 $value=~s/^\s+//gm;
198 $value=~s/\s+$//gm;
199 $value=~tr/ \t\n/ /s;
200 $lvalue=~s/^\s+//gm;
201 $lvalue=~s/\s+$//gm;
202 $lvalue=~tr/ \t\n/ /s;
203
204 my $old="$field_lang-fuzzy";
205 my $old_ext="extended_$old";
206 if (defined $localized->$old &&
207 $localized->$field_lang eq "") {
208 # Old fuzzy field exists, and no
209 # translation has been updated, so
210 # preserve as-is.
211 $fuzzy++;
212 $master->$old($localized->$old);
213 $master->$old_ext($localized->$old_ext)
214 if defined $localized->$old_ext;
215 $master->$field_lang("");
216 }
217 elsif ($value eq $lvalue) {
218 $good++;
219 $master->$field_lang($localized->$field_lang);
220 $master->$field_lang_ext($localized->$field_lang_ext)
221 if defined $localized->$field_lang_ext;
222 }
223 else {
224 $fuzzy++;
225 $master->$old($localized->$field_lang);
226 $master->$old_ext($localized->$field_lang_ext)
227 if defined $localized->$field_lang_ext;
228 $master->$field_lang("");
229 }
230 }
231 }
232
233 # Now make sure that at least stubs exist for every needed
234 # translated field.
235 foreach my $field (@needfields) {
236 my $field_lang="$field-$lang";
237 unless (defined $master->$field_lang) {
238 $bad++;
239 $master->$field_lang("");
240 }
241 }
242 }
243
244 if (! $stats) {
245 print Debconf::Template::Transient->stringify(@out) unless $stats;
246 }
247 else {
248 print "$lang: ".join(', ', grep { $_ ne "" }
249 $good ? "$good translated strings" : "",
250 $fuzzy ? "$fuzzy fuzzy translations" : "",
251 $bad ? "$bad untranslated strings" : "",
252 )."\n";
253 }
254 }
255
256 =head1 AUTHOR
257
258 Joey Hess <[email protected]>
259
260 =cut