LOCALE:...TOR.3PM(1)

๐Ÿ’… NAME ๐Ÿ’…

okay bestie, welcome to the LOCALE:...TOR.3PM(1) page from User Contributed Perl Documentation. the main character here is Locale::Po4a::TransTractor โ€” the generic trans(lator ex)tractor. yes, that is literally the name. no cap. ๐Ÿ’–


โœจ DESCRIPTION โœจ

the whole po4a (PO for anything) project is obsessed with one goal: making translations easier โ€” and even more interestingly, keeping those translations maintained โ€” by using gettext tools on places where people did not expect them, like documentation. itโ€™s giving โ€œgettext but make it everywhereโ€ energy. ๐Ÿฆ‹

This class is the ancestor of every po4a parser used to parse a document, to search translatable strings, to extract them to a PO file and to replace them by their translation in the output document. mother of parsers, honestly. ๐Ÿ‘‘

More formally, it takes the following arguments as input:

  • a document to translate;
  • a PO file containing the translations to use.

As output, it produces:

  • another PO file, resulting of the extraction of translatable strings from the input document;
  • a translated document, with the same structure than the one in input, but with all translatable strings replaced with the translations found in the PO file provided in input.

Here is a graphical representation of this:

          Input document --\                             /---> Output document
                            \                           /       (translated)
                             +-> parse() function -----+
                            /                           \
          Input PO --------/                             \---> Output PO
                                                                (extracted)

๐Ÿ› ๏ธ FUNCTIONS YOUR PARSER SHOULD OVERRIDE ๐Ÿ› ๏ธ

๐Ÿงš parse()

This is where all the work takes place: the parsing of input documents, the generation of output, and the extraction of the translatable strings. This is pretty simple using the provided functions presented in the section INTERNAL FUNCTIONS below. See also the SYNOPSIS, which presents an example. literally the main character function. ๐Ÿ’…

This function is called by the process() function below, but if you choose to use the new() function, and to add content manually to your document, you will have to call this function yourself.

๐Ÿงš docheader()

This function returns the header we should add to the produced document, quoted properly to be a comment in the target language. See the section Educating developers about translations, from po4a(7), for what it is good for.


๐ŸŒธ SYNOPSIS ๐ŸŒธ

The following example parses a list of paragraphs beginning with "<p>". For the sake of simplicity, we assume that the document is well formatted, i.e. that '<p>' tags are the only tags present, and that this tag is at the very beginning of each paragraph.

        sub parse {
          my $self = shift;

          PARAGRAPH: while (1) {
              my ($paragraph,$pararef)=("","");
              my $first=1;
              my ($line,$lref)=$self->shiftline();
              while (defined($line)) {
                  if ($line =~ m/<p>/ && !$first--; ) {
                      # Not the first time we see <p>.
                      # Reput the current line in input,
                      #  and put the built paragraph to output
                      $self->unshiftline($line,$lref);

                      # Now that the document is formed, translate it:
                      #   - Remove the leading tag
                      $paragraph =~ s/^<p>//s;

                      #   - push to output the leading tag (untranslated) and the
                      #     rest of the paragraph (translated)
                      $self->pushline(  "<p>"
                                      . $self->translate($paragraph,$pararef)
                                      );

                      next PARAGRAPH;
                  } else {
                      # Append to the paragraph
                      $paragraph .= $line;
                      $pararef = $lref unless(length($pararef));
                  }

                  # Reinit the loop
                  ($line,$lref)=$self->shiftline();
              }
              # Did not get a defined line? End of input file.
              return;
          }
        }

Once youโ€™ve implemented the parse function, you can use your document class, using the public interface presented in the next section. ๐Ÿซถ


๐ŸŒˆ PUBLIC INTERFACE for scripts using your parser ๐ŸŒˆ

๐ŸŽ€ Constructor ๐ŸŽ€

๐ŸŒŸ process(%) ๐ŸŒŸ

This function can do all you need to do with a po4a document in one invocation. Its arguments must be packed as a hash. ACTIONS:

  1. Reads all the PO files specified in po_in_name
  2. Reads all original documents specified in file_in_name
  3. Parses the document
  4. Reads and applies all the addenda specified
  5. Writes the translated document to file_out_name (if given)
  6. Writes the extracted PO file to po_out_name (if given)

ARGUMENTS, beside the ones accepted by new() (with expected type):

  • file_in_name (@) โ€” List of filenames where we should read the input document.
  • file_in_charset ($) โ€” Charset used in the input document (if it isnโ€™t specified, use UTF-8).
  • file_out_name ($) โ€” Filename where we should write the output document.
  • file_out_charset ($) โ€” Charset used in the output document (if it isnโ€™t specified, use UTF-8).
  • po_in_name (@) โ€” List of filenames where we should read the input PO files from, containing the translation which will be used to translate the document.
  • po_out_name ($) โ€” Filename where we should write the output PO file, containing the strings extracted from the input document.
  • addendum (@) โ€” List of filenames where we should read the addenda from.
  • addendum_charset ($) โ€” Charset for the addenda.

๐ŸŒŸ new(%) ๐ŸŒŸ

Create a new po4a document. Accepted options (in the hash passed as a parameter):

  • verbose ($) โ€” Sets the verbosity.
  • debug ($) โ€” Sets the debugging.
  • wrapcol ($) โ€” The column at which we should wrap text in output document (default: 76).

The negative value means not to wrap lines at all.

Also it accepts next options for underlying Po-files: porefs, copyright-holder, msgid-bugs-address, package-name, package-version, wrap-po.


๐Ÿ“‚ Manipulating document files ๐Ÿ“‚

read($$$)

Add another input document data at the end of the existing array "@{$self->{TT}{doc_in}}".

This function takes two mandatory arguments and an optional one.

  • The filename to read on disk;
  • The name to use as filename when building the reference in the PO file;
  • The charset to use to read that file (UTF-8 by default)

This array "@{$self->{TT}{doc_in}}" holds this input document data as an array of strings with alternating meanings.

  • The string $textline holding each line of the input text data.
  • The string "$filename:$linenum" holding its location and called as โ€œreferenceโ€ (linenum starts with 1).

Please note that it does not parse anything. You should use the parse() function when youโ€™re done with packing input files into the document.

write($)

Write the translated document to the given filename.

This translated document data are provided by:

  • "$self->docheader()" holding the header text for the plugin, and
  • "@{$self->{TT}{doc_out}}" holding each line of the main translated text in the array.

๐Ÿ’Œ Manipulating PO files ๐Ÿ’Œ

readpo($)

Add the content of a file (which name is passed as argument) to the existing input PO. The old content is not discarded.

writepo($)

Write the extracted PO file to the given filename.

stats()

Returns some statistics about the translation done so far. Please note that itโ€™s not the same statistics than the one printed by msgfmt --statistic. Here, itโ€™s stats about recent usage of the PO file, while msgfmt reports the status of the file. It is a wrapper to the Locale::Po4a::Po::stats_get function applied to the input PO file. Example of use:

               [normal use of the po4a document...]

               ($percent,$hit,$queries) = $document->stats();
               print "We found translations for $percent\%  ($hit from $queries) of strings.\n";

๐Ÿงฉ Manipulating addenda ๐Ÿงฉ

addendum($)

Please refer to po4a(7) for more information on what addenda are, and how translators should write them. To apply an addendum to the translated document, simply pass its filename to this function and you are done ;)

This function returns a non-null integer on error.

๐Ÿ’– INTERNAL FUNCTIONS used to write derivative parsers ๐Ÿ’–

๐Ÿ“ฅ Getting input, providing output ๐Ÿ“ค

Okay so bestie, there are four functions that get input and return output, and they're lowkey cousins of Perl's shift/unshift and push/pop:

  • Perl shift returns the first array item and drops it from the array.
  • Perl unshift prepends an item to the array as the first array item.
  • Perl pop returns the last array item and drops it from the array.
  • Perl push appends an item to the array as the last array item.

The first pair is about input, and the second pair is about output. Mnemonic time: for input you care about the first line, which is what shift gives you; for output you wanna add your result at the end, like push does. Slay. โœจ

๐ŸŒธ shiftline()

This function returns the first line to be parsed and its corresponding reference (packed as an array) from the array "@{$self->{TT}{doc_in}}", and drops those first 2 array items. Here, the reference is provided by a string "$filename:$linenum".

๐Ÿฆ‹ unshiftline($$)

Unshifts the last shifted line of the input document and its corresponding reference back to the head of "{$self->{TT}{doc_in}}".

๐ŸŽ€ pushline($)

Push a new line to the end of "{$self->{TT}{doc_out}}".

โญ popline()

Pop the last pushed line from the end of "{$self->{TT}{doc_out}}".


๐Ÿท๏ธ Marking strings as translatable ๐Ÿท๏ธ

One function handles the text that should be translated. Iconic behavior, honestly.

๐Ÿ’… translate($$$)

Mandatory arguments:

  • A string to translate
  • The reference of this string (i.e. position in inputfile)
  • The type of this string (i.e. the textual description of its structural role; used in Locale::Po4a::Po::gettextization(); see also po4a(7), section Gettextization: how does it work?)

This function can also take some extra arguments. They must be organized as a hash. For example:

$self->translate("string","ref","type",
                 'wrap' => 1);
  • wrap โ€” boolean indicating whether we can consider that whitespaces in string are not important. If yes, the function canonizes the string before looking for a translation or extracting it, and wraps the translation.
  • wrapcol โ€” the column at which we should wrap (default: the value of wrapcol specified during creation of the TransTractor or 76). The negative value will be substracted from the default.
  • comment โ€” an extra comment to add to the entry.

Actions:

  • Pushes the string, reference and type to po_out.
  • Returns the translation of the string (as found in po_in) so that the parser can build the doc_out.
  • Handles the charsets to recode the strings before sending them to po_out and before returning the translations.

๐Ÿงš Misc functions ๐Ÿงš

๐Ÿ”ฅ verbose()

Returns if the verbose option was passed during the creation of the TransTractor.

๐Ÿ’€ debug()

Returns if the debug option was passed during the creation of the TransTractor.

๐ŸŒˆ get_in_charset()

This function return the charset that was provided as master charset.

๐Ÿ‘‘ get_out_charset()

This function will return the charset that should be used in the output document (usually useful to substitute the input document's detected charset where it has been found).

It will use the output charset specified in the command line. If it wasn't specified, it will use the input PO's charset, and if the input PO has the default "CHARSET", it will return the input document's charset, so that no encoding is performed.


๐Ÿ”ฎ FUTURE DIRECTIONS ๐Ÿ”ฎ

One shortcoming of the current TransTractor is that it can't handle translated document containing all languages, like debconf templates, or .desktop files.

To address this problem, the only interface changes needed are:

  • take a hash as po_in_name (a list per language)
  • add an argument to translate to indicate the target language
  • make a pushline_all function, which would make pushline of its content for all languages, using a map-like syntax:
$self->pushline_all({ "Description[".$langcode."]=".
                      $self->translate($line,$ref,$langcode)
                    });

Will see if it's enough ;)


๐Ÿ‘‘ AUTHORS ๐Ÿ‘‘

  • Denis Barbier <barbier@linuxfr.org>
  • Martin Quinson (mquinson#debian.org)
  • Jordi Vilalta <jvprat@gmail.com>

โœจ This page came from the perl v5.42.0 docs and is dated 2025-07-13 โ€” that's the source/version and date, served cute, periodt. ๐Ÿ’…๐Ÿ’–

๐Ÿ“ฆ where this page comes from