Po file manipulation module
use Locale::Po4a::Po; my $pofile=Locale::Po4a::Po->new(); # Read PO file $pofile->read('file.po'); # Add an entry $pofile->push('msgid' => 'Hello', 'msgstr' => 'bonjour', 'flags' => "wrap", 'reference'=>'file.c:46'); # Extract a translation $pofile->gettext("Hello"); # returns 'bonjour' # Write back to a file $pofile->write('otherfile.po');
Locale::Po4a::Po is a module that allows you to manipulate message catalogs. You can load and write from/to a file (which extension is often po), you can build new entries on the fly or request for the translation of a string.
For a more complete description of message catalogs in the \s-1PO\s0 format and their use, please refer to the documentation of the gettext program.
This module is part of the po4a project, which objective is to use \s-1PO\s0 files (designed at origin to ease the translation of program messages) to translate everything, including documentation (man page, info manual), package description, debconf templates, and everything which may benefit from this.
Specify the reference format. Argument type can be one of none to not produce any reference, noline to not specify the line number (more accurately all line numbers are replaced by 1), counter to replace line number by an increasing counter, and full to include complete references. Argument can be followed by a comma and either wrap or nowrap keyword. References are written by default on a single line. The wrap option wraps references on several lines, to mimic gettext tools (xgettext and msgmerge). This option will become the default in a future release, because it is more sensible. The nowrap option is available so that users who want to keep the old behavior can do so.
Set the report address for msgid bugs. By default, the created \s-1POT\s0 files have no Report-Msgid-Bugs-To fields.
Set the copyright holder in the \s-1POT\s0 header. The default value is \*(L"Free Software Foundation, Inc.\*(R"
Set the package name for the \s-1POT\s0 header. The default is \*(L"\s-1PACKAGE\s0\*(R".
Set the package version for the \s-1POT\s0 header. The default is \*(L"\s-1VERSION\s0\*(R".
Creates a new message catalog. If an argument is provided, it's the name of a \s-1PO\s0 file we should load.
Reads a \s-1PO\s0 file (which name is given as argument). Previously existing entries in self are not removed, the new ones are added to the end of the catalog.
Writes the current catalog to the given file.
Like write, but if the \s-1PO\s0 or \s-1POT\s0 file already exists, the object will be written in a temporary file which will be compared with the existing file to check if the update is needed (this avoids to change a \s-1POT\s0 just to update a line reference or the POT-Creation-Date field).
This function produces one translated message catalog from two catalogs, an original and a translation. This process is described in po4a\|(7), section Gettextization: how does it work?.
This function extracts a catalog from an existing one. Only the entries having a reference in the given file will be placed in the resulting catalog. This function parses its argument, converts it to a Perl function definition, evals this definition and filters the fields for which this function returns true. I love Perl sometimes ;)
Recodes to \s-1UTF-8\s0 the \s-1PO\s0's msgstrs. Does nothing if the charset is not specified in the \s-1PO\s0 file (\*(L"\s-1CHARSET\s0\*(R" value), or if it's already \s-1UTF-8\s0 or \s-1ASCII\s0.
Request the translation of the string given as argument in the current catalog. The function returns the original (untranslated) string if the string was not found. After the string to translate, you can pass a hash of extra arguments. Here are the valid entries:
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, and wraps the result.
the column at which we should wrap (default: 76).
Returns statistics about the hit ratio of gettext since the last time that stats_clear() was called. Please note that it's not the same statistics than the one printed by msgfmt --statistic. Here, it's statistics about recent usage of the \s-1PO\s0 file, while msgfmt reports the status of the file. Example of use: [some use of the PO file to translate stuff]
($percent,$hit,$queries) = $pofile->stats_get(); print "So far, we found translations for $percent\% ($hit of $queries) of strings.\n";
Clears the statistics about gettext hits.
Push a new entry at the end of the current catalog. The arguments should form a hash table. The valid keys are:
the string in original language.
the translation.
an indication of where this string was found. Example: file.c:46 (meaning in 'file.c' at line 46). It can be a space-separated list in case of multiple occurrences.
a comment added here manually (by the translators). The format here is free.
a comment which was automatically added by the string extraction program. See the --add-comments option of the xgettext program for more information.
space-separated list of all defined flags for this entry. Valid flags are: c-text, python-text, lisp-text, elisp-text, librep-text, smalltalk-text, java-text, awk-text, object-pascal-text, ycp-text, tcl-text, wrap, no-wrap and fuzzy. See the gettext documentation for their meaning.
this is mostly an internal argument: it is used while gettextizing documents. The idea here is to parse both the original and the translation into a \s-1PO\s0 object, and merge them, using one's msgid as msgid and the other's msgid as msgstr. To make sure that things get ok, each msgid in \s-1PO\s0 objects are given a type, based on their structure (like \*(L"chapt\*(R", \*(L"sect1\*(R", \*(L"p\*(R" and so on in DocBook). If the types of strings are not the same, that means that both files do not share the same structure, and the process reports an error. This information is written as automatic comment in the \s-1PO\s0 file since this gives to translators some context about the strings to translate.
boolean indicating whether whitespaces can be mangled in cosmetic reformattings. If true, the string is canonized before use. This information is written to the \s-1PO\s0 file using the wrap or no-wrap flag.
the column at which we should wrap (default: 76). This information is not written to the \s-1PO\s0 file.
Returns the number of entries in the catalog (without the header).
Returns the number of entries in document. If a string appears multiple times in the document, it will be counted multiple times
Returns the msgid of the given number.
Returns the msgid with the given position in the document.
Returns the character set specified in the \s-1PO\s0 header. If it hasn't been set, it will return \*(L"\s-1CHARSET\s0\*(R".
This sets the character set of the \s-1PO\s0 header to the value specified in its first argument. If you never call this function (and no file with a specified character set is read), the default value is left to \*(L"\s-1CHARSET\s0\*(R". This value doesn't change the behavior of this module, it's just used to fill that field in the header, and to return it in get_charset().
Denis Barbier <[email protected]> Martin Quinson (mquinson#debian.org)