1Locale::Po4a::TransTractor(3pm)   Po4a Tools   Locale::Po4a::TransTractor(3pm)
2
3
4

NAME

6       Locale::Po4a::TransTractor - generic trans(lator ex)tractor.
7

DESCRIPTION

9       The po4a (PO for anything) project goal is to ease translations (and
10       more interestingly, the maintenance of translations) using gettext
11       tools on areas where they were not expected like documentation.
12
13       This class is the ancestor of every po4a parser used to parse a
14       document, to search translatable strings, to extract them to a PO file
15       and to replace them by their translation in the output document.
16
17       More formally, it takes the following arguments as input:
18
19       - a document to translate;
20
21       - a PO file containing the translations to use.
22
23       As output, it produces:
24
25       - another PO file, resulting of the extraction of translatable strings
26         from the input document;
27
28       - a translated document, with the same structure than the one in input,
29         but with all translatable strings replaced with the translations
30         found in the PO file provided in input.
31
32       Here is a graphical representation of this:
33
34          Input document --\                             /---> Output document
35                            \                           /       (translated)
36                             +-> parse() function -----+
37                            /                           \
38          Input PO --------/                             \---> Output PO
39                                                                (extracted)
40

FUNCTIONS YOUR PARSER SHOULD OVERRIDE

42       parse()
43           This is where all the work takes place: the parsing of input
44           documents, the generation of output, and the extraction of the
45           translatable strings. This is pretty simple using the provided
46           functions presented in the section INTERNAL FUNCTIONS below. See
47           also the SYNOPSIS, which presents an example.
48
49           This function is called by the process() function below, but if you
50           choose to use the new() function, and to add content manually to
51           your document, you will have to call this function yourself.
52
53       docheader()
54           This function returns the header we should add to the produced
55           document, quoted properly to be a comment in the target language.
56           See the section Educating developers about translations, from
57           po4a(7), for what it is good for.
58

SYNOPSIS

60       The following example parses a list of paragraphs beginning with "<p>".
61       For the sake of simplicity, we assume that the document is well
62       formatted, i.e. that '<p>' tags are the only tags present, and that
63       this tag is at the very beginning of each paragraph.
64
65        sub parse {
66          my $self = shift;
67
68          PARAGRAPH: while (1) {
69              my ($paragraph,$pararef)=("","");
70              my $first=1;
71              my ($line,$lref)=$self->shiftline();
72              while (defined($line)) {
73                  if ($line =~ m/<p>/ && !$first--; ) {
74                      # Not the first time we see <p>.
75                      # Reput the current line in input,
76                      #  and put the built paragraph to output
77                      $self->unshiftline($line,$lref);
78
79                      # Now that the document is formed, translate it:
80                      #   - Remove the leading tag
81                      $paragraph =~ s/^<p>//s;
82
83                      #   - push to output the leading tag (untranslated) and the
84                      #     rest of the paragraph (translated)
85                      $self->pushline(  "<p>"
86                                      . $self->translate($paragraph,$pararef)
87                                      );
88
89                      next PARAGRAPH;
90                  } else {
91                      # Append to the paragraph
92                      $paragraph .= $line;
93                      $pararef = $lref unless(length($pararef));
94                  }
95
96                  # Reinit the loop
97                  ($line,$lref)=$self->shiftline();
98              }
99              # Did not get a defined line? End of input file.
100              return;
101          }
102        }
103
104       Once you've implemented the parse function, you can use your document
105       class, using the public interface presented in the next section.
106

PUBLIC INTERFACE for scripts using your parser

108   Constructor
109       process(%)
110           This function can do all you need to do with a po4a document in one
111           invocation. Its arguments must be packed as a hash. ACTIONS:
112
113           a. Reads all the PO files specified in po_in_name
114
115           b. Reads all original documents specified in file_in_name
116
117           c. Parses the document
118
119           d. Reads and applies all the addenda specified
120
121           e. Writes the translated document to file_out_name (if given)
122
123           f. Writes the extracted PO file to po_out_name (if given)
124
125           ARGUMENTS, beside the ones accepted by new() (with expected type):
126
127           file_in_name (@)
128               List of filenames where we should read the input document.
129
130           file_in_charset ($)
131               Charset used in the input document (if it isn't specified, it
132               will try to detect it from the input document).
133
134           file_out_name ($)
135               Filename where we should write the output document.
136
137           file_out_charset ($)
138               Charset used in the output document (if it isn't specified, it
139               will use the PO file charset).
140
141           po_in_name (@)
142               List of filenames where we should read the input PO files from,
143               containing the translation which will be used to translate the
144               document.
145
146           po_out_name ($)
147               Filename where we should write the output PO file, containing
148               the strings extracted from the input document.
149
150           addendum (@)
151               List of filenames where we should read the addenda from.
152
153           addendum_charset ($)
154               Charset for the addenda.
155
156       new(%)
157           Create a new po4a document. Accepted options (in the hash passed as
158           a parameter):
159
160           verbose ($)
161               Sets the verbosity.
162
163           debug ($)
164               Sets the debugging.
165
166   Manipulating document files
167       read($$)
168           Add another input document data at the end of the existing array
169           "@{$self->{TT}{doc_in}}". The argument is the filename to read. If
170           a second argument is provided, it is the filename to use in the
171           references.
172
173           This array "@{$self->{TT}{doc_in}}" holds this input document data
174           as an array of strings with alternating meanings.
175            * The string $textline holding each line of the input text data.
176            * The string "$filename:$linenum" holding its location and called
177           as
178              "reference" ("linenum" starts with 1).
179
180           Please note that it does not parse anything. You should use the
181           parse() function when you're done with packing input files into the
182           document.
183
184       write($)
185           Write the translated document to the given filename.
186
187           This translated document data are provided by:
188            * "$self->docheader()" holding the header text for the plugin, and
189            * "@{$self->{TT}{doc_out}}" holding each line of the main
190           translated text in the array.
191
192   Manipulating PO files
193       readpo($)
194           Add the content of a file (which name is passed as argument) to the
195           existing input PO. The old content is not discarded.
196
197       writepo($)
198           Write the extracted PO file to the given filename.
199
200       stats()
201           Returns some statistics about the translation done so far. Please
202           note that it's not the same statistics than the one printed by
203           msgfmt --statistic. Here, it's stats about recent usage of the PO
204           file, while msgfmt reports the status of the file. It is a wrapper
205           to the Locale::Po4a::Po::stats_get function applied to the input PO
206           file. Example of use:
207
208               [normal use of the po4a document...]
209
210               ($percent,$hit,$queries) = $document->stats();
211               print "We found translations for $percent\%  ($hit from $queries) of strings.\n";
212
213   Manipulating addenda
214       addendum($)
215           Please refer to po4a(7) for more information on what addenda are,
216           and how translators should write them. To apply an addendum to the
217           translated document, simply pass its filename to this function and
218           you are done ;)
219
220           This function returns a non-null integer on error.
221

INTERNAL FUNCTIONS used to write derivative parsers

223   Getting input, providing output
224       Four functions are provided to get input and return output. They are
225       very similar to shift/unshift and push/pop of Perl.
226
227        * Perl shift returns the first array item and drop it from the array.
228        * Perl unshift prepends an item to the array as the first array item.
229        * Perl pop returns the last array item and drop it from the array.
230        * Perl push appends an item to the array as the last array item.
231
232       The first pair is about input, while the second is about output.
233       Mnemonic: in input, you are interested in the first line, what shift
234       gives, and in output you want to add your result at the end, like push
235       does.
236
237       shiftline()
238           This function returns the first line to be parsed and its
239           corresponding reference (packed as an array) from the array
240           "@{$self->{TT}{doc_in}}" and drop these first 2 array items.  Here,
241           the reference is provided by a string "$filename:$linenum".
242
243       unshiftline($$)
244           Unshifts the last shifted line of the input document and its
245           corresponding reference back to the head of
246           "{$self->{TT}{doc_in}}".
247
248       pushline($)
249           Push a new line to the end of "{$self->{TT}{doc_out}}".
250
251       popline()
252           Pop the last pushed line from the end of "{$self->{TT}{doc_out}}".
253
254   Marking strings as translatable
255       One function is provided to handle the text which should be translated.
256
257       translate($$$)
258           Mandatory arguments:
259
260           - A string to translate
261
262           - The reference of this string (i.e. position in inputfile)
263
264           - The type of this string (i.e. the textual description of its
265             structural role; used in Locale::Po4a::Po::gettextization(); see
266             also po4a(7), section Gettextization: how does it work?)
267
268           This function can also take some extra arguments. They must be
269           organized as a hash. For example:
270
271             $self->translate("string","ref","type",
272                              'wrap' => 1);
273
274           wrap
275               boolean indicating whether we can consider that whitespaces in
276               string are not important. If yes, the function canonizes the
277               string before looking for a translation or extracting it, and
278               wraps the translation.
279
280           wrapcol
281               the column at which we should wrap (default: 76).
282
283           comment
284               an extra comment to add to the entry.
285
286           Actions:
287
288           - Pushes the string, reference and type to po_out.
289
290           - Returns the translation of the string (as found in po_in) so that
291             the parser can build the doc_out.
292
293           - Handles the charsets to recode the strings before sending them to
294             po_out and before returning the translations.
295
296   Misc functions
297       verbose()
298           Returns if the verbose option was passed during the creation of the
299           TransTractor.
300
301       debug()
302           Returns if the debug option was passed during the creation of the
303           TransTractor.
304
305       detected_charset($)
306           This tells TransTractor that a new charset (the first argument) has
307           been detected from the input document. It can usually be read from
308           the document header. Only the first charset will remain, coming
309           either from the process() arguments or detected from the document.
310
311       get_out_charset()
312           This function will return the charset that should be used in the
313           output document (usually useful to substitute the input document's
314           detected charset where it has been found).
315
316           It will use the output charset specified in the command line. If it
317           wasn't specified, it will use the input PO's charset, and if the
318           input PO has the default "CHARSET", it will return the input
319           document's charset, so that no encoding is performed.
320
321       recode_skipped_text($)
322           This function returns the recoded text passed as argument, from the
323           input document's charset to the output document's one. This isn't
324           needed when translating a string (translate() recodes everything
325           itself), but it is when you skip a string from the input document
326           and you want the output document to be consistent with the global
327           encoding.
328

FUTURE DIRECTIONS

330       One shortcoming of the current TransTractor is that it can't handle
331       translated document containing all languages, like debconf templates,
332       or .desktop files.
333
334       To address this problem, the only interface changes needed are:
335
336       - take a hash as po_in_name (a list per language)
337
338       - add an argument to translate to indicate the target language
339
340       - make a pushline_all function, which would make pushline of its
341         content for all languages, using a map-like syntax:
342
343             $self->pushline_all({ "Description[".$langcode."]=".
344                                   $self->translate($line,$ref,$langcode)
345                                 });
346
347       Will see if it's enough ;)
348

AUTHORS

350        Denis Barbier <barbier@linuxfr.org>
351        Martin Quinson (mquinson#debian.org)
352        Jordi Vilalta <jvprat@gmail.com>
353
354
355
356Po4a Tools                        2023-01-23   Locale::Po4a::TransTractor(3pm)
Impressum