LOCALE:...:PO.3PM(1)
β¨ NAME β¨
Locale::Po4a::Po β PO file manipulation module π
okay bestie, this is the LOCALE:...:PO.3PM(1) page from the User Contributed Perl Documentation, and it's all about Locale::Po4a::Po. π
πΈ SYNOPSIS πΈ
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');
π¦ DESCRIPTION π¦
Locale::Po4a::Po is a module that lets you manipulate message catalogs. You can load and write from/to a file (the extension is often po), you can build new entries on the fly, or you can request the translation of a string. β¨
For a more complete description of message catalogs in the PO format and their use, please refer to the info documentation of the gettext program (node "`PO Files'"). π
This module is part of the po4a project, which objective is to use PO 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. literally everything, bestie π
π OPTIONS ACCEPTED BY THIS MODULE π
--porefs typeβ specify the reference format. The argumenttypecan be one ofneverto not produce any reference,fileto only specify the file without the line number,counterto replace line number by an increasing counter, andfullto include complete references (default:full). π--wrap-po no|newlines|numberβ specify how the po file should be wrapped (default:76). This gives the choice between either files that are nicely wrapped but could lead to git conflicts, or files that are easier to handle automatically, but harder to read for humans. Historically, thegettextsuite has reformatted the po files at the 77th column for cosmetics. This option specifies the behavior ofpo4a. If set to a numerical value,po4awill wrap the po file after this column and after newlines in the content. If set tonewlines,po4awill only split themsgidandmsgstrafter newlines in the content. If set tono,po4awill not wrap the po file at all. The reference comments are always wrapped by thegettexttools that we use internally. Note that this option has no impact on how themsgidandmsgstrare wrapped, i.e. on how newlines are added to the content of these strings. π§--msgid-bugs-address email@addressβ set the report address formsgidbugs. By default, the created POT files have noReport-Msgid-Bugs-Tofields. π¬--copyright-holder stringβ set the copyright holder in the POT header. The default value is"Free Software Foundation, Inc."π--package-name stringβ set the package name for the POT header. The default is"PACKAGE". π¦--package-version stringβ set the package version for the POT header. The default is"VERSION". π·οΈ
π οΈ Functions concerning entire message catalogs π οΈ
new()
Creates a new message catalog. If an argument is provided, it's the name of a PO file we should load. π
read($)
Reads a PO 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. π
write($)
Writes the current catalog to the given file. βοΈ
write_if_needed($$)
Like write, but if the PO or POT 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 changing a POT just to update a line reference or the POT-Creation-Date field). π§
filter($)
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 ;) πͺ
π₯ Functions to use a message catalog for translations π₯
gettext($%)
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:
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, and wraps the result. πwrapcolβ the column at which we should wrap (default:76). π
stats_get()
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 PO 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";
stats_clear()
Clears the statistics about gettext hits. π§Ή
π§ Functions to build a message catalog π§
push(%)
Push a new entry at the end of the current catalog. The arguments should form a hash table. The valid keys are:
msgidβ the string in original language. π£οΈmsgstrβ the translation. π¬referenceβ an indication of where this string was found. Example:file.c:46(meaning infile.cat line 46). It can be a space-separated list in case of multiple occurrences. πcommentβ a comment added here manually (by the translators). The format here is free. πautomaticβ a comment which was automatically added by the string extraction program. See the--add-commentsoption of thexgettextprogram for more information. π€flagsβ 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-wrapandfuzzy. See thegettextdocumentation for their meaning. π©typeβ 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 PO object, and merge them, using one'smsgidasmsgidand the other'smsgidasmsgstr. To make sure that things get ok, eachmsgidin PO objects are given a type, based on their structure (like"chapt","sect1","p"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 PO file since this gives to translators some context about the strings to translate. π§©wrapβ boolean indicating whether whitespaces can be mangled in cosmetic reformattings. If true, the string is canonized before use. This information is written to the PO file using thewraporno-wrapflag. πwrapcolβ ignored; the key is kept for backward computability. π«
π Miscellaneous functions π
count_entries()
Returns the number of entries in the catalog (without the header). π’
count_entries_doc()
Returns the number of entries in document. If a string appears multiple times in the document, it will be counted multiple times. π
msgid($)
Returns the msgid of the given number. π
msgid_doc($)
Returns the msgid with the given position in the document. π
type_doc($)
Returns the type of the msgid with the given position in the document. This is probably only useful to gettextization, and it's stored separately from {$msgid}{'type'} because the later location may be overwritten by another type when the $msgid is duplicated in the master document. π§
get_charset()
Returns the character set specified in the PO header. If it hasn't been set, it will return "UTF-8". π€
π AUTHORS π
- Denis Barbier
<barbier@linuxfr.org>π - Martin Quinson
(mquinson#debian.org)π
mwah π β this page is from perl v5.42.0, dated 2025-07-13, and it lives under LOCALE:...:PO.3PM(1). periodt. π«Ά
π¦ where this page comes from
- alpine 3.23: po4a-doc 0.74-r0 Β· GPL-2.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms