LOCALE:...:XML.3PM(1)
π LOCALE:...:XML.3PM(1) π
okay so bestie, welcome to the User Contributed Perl Documentation page for Locale::Po4a::Xml β yes, the manual name is right there, and this whole page is about making XML translation not a nightmare π
β¨
πΈ NAME πΈ
Locale::Po4a::Xml β convert XML documents and derivates from/to PO files π
π DESCRIPTION π
okay so the po4a (PO for anything) project has one goal: make translations β and, even more interestingly, the maintenance of translations β easier by using gettext tools in places people didn't expect, like documentation π«Ά
Locale::Po4a::Xml is a module to help the translation of XML documents into other [human] languages. It can also be used as a base to build modules for XML-based documents π¦
π¦ TRANSLATING WITH PO4A::XML π¦
You can point this module straight at generic XML documents. It will extract all tag's content, and no attributes, since that's where the text is written in most XML based documents β¨
There are some options (described in the next section) that can customize this behavior. If this doesn't fit to your document format you're encouraged to write your own module derived from this, to describe your format's details. See the section WRITING DERIVATE MODULES below, for the process description π
π OPTIONS ACCEPTED BY THIS MODULE π
The global debug option causes this module to show the excluded strings, in order to see if it skips something important π
These are this module's particular options:
nostripβ Prevents it to strip the spaces around the extracted strings. πwrapβ Canonicalizes the string to translate, considering that whitespaces are not important, and wraps the translated document. This option can be overridden by custom tag options. See thetranslatedoption below. πΈunwrap_attributesβ Attributes are wrapped by default. This option disables wrapping. π¦caseinsensitiveβ It makes the tags and attributes searching to work in a case insensitive way. If it's defined, it will treat<BooK>laNGand<BOOK>Langas<book>lang. πescapequotesβ Escape quotes in output strings. Necessary, for example, for creating string resources for use by Android build tools. See also:https://developer.android.com/guide/topics/resources/string-resource.htmlπ₯includeexternalβ When defined, external entities are included in the generated (translated) document, and for the extraction of strings. If it's not defined, you will have to translate external entities separately as independent documents. πontagerrorβ This option defines the behavior of the module when it encounters invalid XML syntax (a closing tag which does not match the last opening tag). It can take the following values:failβ This is the default value. The module will exit with an error. πwarnβ The module will continue, and will issue a warning. πsilentβ The module will continue without any warnings. π€«
Be careful when using this option. It is generally recommended to fix the input file. π¨
tagsonlyβ Note: This option is deprecated. Extracts only the specified tags in thetagsoption. Otherwise, it will extract all the tags except the ones specified. πdoctypeβ String that will try to match with the first line of the document's doctype (if defined). If it doesn't, a warning will indicate that the document might be of a bad type. π§addlangβ String indicating the path (e.g.<bbb><aaa>) of a tag where alang="..."attribute shall be added. The language will be defined as the basename of the PO file without any.poextension. πoptionalclosingtagβ Boolean indicating whether closing tags are optional (as in HTML). By default, missing closing tags raise an error handled according toontagerror. πtagsβ Note: This option is deprecated. You should use thetranslatedanduntranslatedoptions instead. Space-separated list of tags you want to translate or skip. By default, the specified tags will be excluded, but if you use thetagsonlyoption, the specified tags will be the only ones included. The tags must be in the form<aaa>, but you can join some (<bbb><aaa>) to say that the content of the tag<aaa>will only be translated when it's into a<bbb>tag. You can also specify some tag options by putting some characters in front of the tag hierarchy. For example, you can putw(wrap) orW(don't wrap) to override the default behavior specified by the globalwrapoption. Example:W<chapter><title>π«attributesβ Space-separated list of tag's attributes you want to translate. You can specify the attributes by their name (for example,"lang"), but you can prefix it with a tag hierarchy, to specify that this attribute will only be translated when it's in the specified tag. For example:<bbb><aaa>langspecifies that thelangattribute will only be translated if it's in an<aaa>tag, and it's in a<bbb>tag. π·οΈfoldattributesβ Do not translate attributes in inline tags. Instead, replace all attributes of a tag bypo4a-id=<id>. This is useful when attributes shall not be translated, as this simplifies the strings for translators, and avoids typos. πͺcustomtagβ Space-separated list of tags which should not be treated as tags. These tags are treated as inline, and do not need to be closed. πbreakβ Space-separated list of tags which should break the sequence. By default, all tags break the sequence. The tags must be in the form<aaa>, but you can join some (<bbb><aaa>), if a tag (<aaa>) should only be considered when it's within another tag (<bbb>). Please note a tag should be listed in only one of thebreak,inline,placeholder, orcustomtagsetting string. βοΈinlineβ Space-separated list of tags which should be treated as inline. By default, all tags break the sequence. The tags must be in the form<aaa>, but you can join some (<bbb><aaa>), if a tag (<aaa>) should only be considered when it's within another tag (<bbb>). π§΅placeholderβ Space-separated list of tags which should be treated as placeholders. Placeholders do not break the sequence, but the content of placeholders is translated separately. The location of the placeholder in its block will be marked with a string similar to:
<placeholder type=\"footnote\" id=\"0\"/>
The tags must be in the form <aaa>, but you can join some (<bbb><aaa>), if a tag (<aaa>) should only be considered when it's within another tag (<bbb>). π
break-piβ By default, Processing Instructions (i.e.,<? ... ?>tags) are handled as inline tags. Pass this option if you want the PI to be handled as breaking tag. Note that unprocessed PHP tags are handled as Processing Instructions by the parser. πnodefaultβ Space separated list of tags that the module should not try to set by default in any category. If you have a tag which has its default setting by the subclass of this module but you want to set alternative setting, you need to list that tag as a part of thenodefaultsetting string. π«cppβ Support C preprocessor directives. When this option is set,po4awill consider preprocessor directives as paragraph separators. This is important if the XML file must be preprocessed because otherwise the directives may be inserted in the middle of lines ifpo4aconsider it belong to the current paragraph, and they won't be recognized by the preprocessor. Note: the preprocessor directives must only appear between tags (they must not break a tag). βοΈtranslatedβ Space-separated list of tags you want to translate. The tags must be in the form<aaa>, but you can join some (<bbb><aaa>), if a tag (<aaa>) should only be considered when it's within another tag (<bbb>). You can also specify some tag options by putting some characters in front of the tag hierarchy. This overrides the default behavior specified by the globalwrapanddefaulttranslateoptionoption.wβ Tags should be translated and content can be re-wrapped. πWβ Tags should be translated and content should not be re-wrapped. π«iβ Tags should be translated inline. π§pβ Tags should be translated as placeholders. π
Internally, the XML parser only cares about these four options: w W i p.
- Tags listed in
breakare set toworWdepending on thewrapoption. - Tags listed in
inlineare set toi. - Tags listed in
placeholderare set top. - Tags listed in
untranslatedare without any of these options set.
You can verify actual internal parameter behavior by invoking po4a with --debug option. Example: W<chapter><title>. Please note a tag should be listed in either translated or untranslated setting string. π
untranslatedβ Space-separated list of tags you do not want to translate. The tags must be in the form<aaa>, but you can join some (<bbb><aaa>), if a tag (<aaa>) should only be considered when it's within another tag (<bbb>). Please note a translatable inline tag in an untranslated tag is treated as a translatable breaking tag,isetting is dropped andworWis set depending on thewrapoption. πdefaulttranslateoptionβ The default categories for tags that are not in any of thetranslated,untranslated,break,inline, orplaceholder. This is a set of letters as defined intranslatedand this setting is only valid for translatable tags. π
β¨ WRITING DERIVATIVE MODULES β¨
okay so bestie, you wanna build your own parser on top of this? slay π here's the tea.
π DEFINE WHAT TAGS AND ATTRIBUTES TO TRANSLATE π
The simplest customization is to define which tags and attributes you want the parser to translate. Do this in the initialize function. First call the main initialize to grab the command-line options, then append your custom definitions to the options hash. If you want to treat some new options from command line, define them before calling the main initialize:
$self->{options}{'new_option'}='';
$self->SUPER::initialize(%options);
$self->{options}{'_default_translated'}.=' <p> <head><title>';
$self->{options}{'attributes'}.=' <p>lang id';
$self->{options}{'_default_inline'}.=' <br>';
$self->treat_options;
You should use the _default_inline, _default_break, _default_placeholder, _default_translated, _default_untranslated, and _default_attributes options in derivative modules. This lets users override the default behavior defined in your module with command line options. π
OVERRIDE THE DEFAULT BEHAVIOR WITH COMMAND LINE OPTIONS
If you don't like the default behavior of this xml module and its derivative modules, you can provide command line options to change their behavior.
See Locale::Po4a::Docbook(3pm),
OVERRIDING THE found_string FUNCTION π
Another simple step is overriding the function found_string, which receives the extracted strings from the parser so you can translate them. There you can control which strings you want to translate, and perform transformations to them before or after the translation itself.
It receives the extracted text, the reference on where it was, and a hash that contains extra information to control what strings to translate, how to translate them and to generate the comment.
The content of these options depends on the kind of string it is (specified in an entry of this hash):
type="tag"β the found string is the content of a translatable tag. The entrytag_optionscontains the option characters in front of the tag hierarchy in the moduletagsoption.type="attribute"β means that the found string is the value of a translatable attribute. The entryattributehas the name of the attribute.
It must return the text that will replace the original in the translated document. Here's a basic example of this function:
sub found_string {
my ($self,$text,$ref,$options)=@_;
$text = $self->translate($text,$ref,"type ".$options->{'type'},
'wrap'=>$self->{options}{'wrap'});
return $text;
}
There's another simple example in the new Dia module, which only filters some strings. π¦
MODIFYING TAG TYPES (TODO)
This is a more complex one, but it enables an (almost) total customization. It's based on a list of hashes, each one defining a tag type's behavior. The list should be sorted so that the most general tags are after the most concrete ones (sorted first by the beginning and then by the end keys). To define a tag type you'll have to make a hash with the following keys:
beginningβ specifies the beginning of the tag, after the<.endβ specifies the end of the tag, before the>.breakingβ says if this is a breaking tag class. A non-breaking (inline) tag is one that can be taken as part of the content of another tag. It can take the values false (0), true (1) or undefined. If you leave this undefined, you'll have to define thef_breakingfunction that will say whether a concrete tag of this class is a breaking tag or not.f_breakingβ a function that will tell if the next tag is a breaking one or not. It should be defined if thebreakingoption is not.f_extractβ if you leave this key undefined, the generic extraction function will have to extract the tag itself. It's useful for tags that can have other tags or special structures in them, so that the main parser doesn't get mad. This function receives a boolean that says if the tag should be removed from the input stream or not.f_translateβ this function receives the tag (in theget_string_until()format) and returns the translated tag (translated attributes or all needed transformations) as a single string.
π§ INTERNAL FUNCTIONS used to write derivative parsers π§
WORKING WITH TAGS
get_path()β returns the path to the current tag from the document's root, in the form<html><body><p>. An additional array of tags (without brackets) can be passed as argument. These path elements are added to the end of the current path.tag_type()β returns the index from thetag_typeslist that fits to the next tag in the input stream, or-1if it's at the end of the input file. Here, the tag has structure started by<and end by>and it can contain multiple lines. This works on the array@{$self->{TT}{doc_in}}holding input document data and reference indirectly via$self->shiftline()and$self->unshiftline($$).extract_tag($$)β returns the next tag from the input stream without the beginning and end, in an array form, to maintain the references from the input file. It has two parameters: the type of the tag (as returned bytag_type) and a boolean, that indicates if it should be removed from the input stream. This works on the array@{$self->{TT}{doc_in}}holding input document data and reference indirectly via$self->shiftline()and$self->unshiftline($$).get_tag_name(@)β returns the name of the tag passed as an argument, in the array form returned byextract_tag.breaking_tag()β returns a boolean that says if the next tag in the input stream is a breaking tag or not (inline tag). It leaves the input stream intact.treat_tag()β translates the next tag from the input stream. Using each tag type's custom translation functions. This works on the array@{$self->{TT}{doc_in}}holding input document data and reference indirectly via$self->shiftline()and$self->unshiftline($$).tag_in_list($@)β returns a string value that says if the first argument (a tag hierarchy) matches any of the tags from the second argument (a list of tags or tag hierarchies). If it doesn't match, it returns0. Else, it returns the matched tag's options (the characters in front of the tag) or1(if that tag doesn't have options).
WORKING WITH ATTRIBUTES
treat_attributes(@)β handles the translation of the tags' attributes. It receives the tag without the beginning / end marks, and then it finds the attributes, and it translates the translatable ones (specified by the module optionattributes). This returns a plain string with the translated tag.
WORKING WITH TAGGED CONTENTS
treat_content()β gets the text until the next breaking tag (not inline) from the input stream. Translate it using each tag type's custom translation functions. This works on the array@{$self->{TT}{doc_in}}holding input document data and reference indirectly via$self->shiftline()and$self->unshiftline($$).
WORKING WITH THE MODULE OPTIONS
treat_options()β fills the internal structures that contain the tags, attributes and inline data with the options of the module (specified in the command-line or in theinitializefunction).
GETTING TEXT FROM THE INPUT DOCUMENT
get_string_until($%)β returns an array with the lines (and references) from the input document until it finds the first argument. The second argument is an options hash. Value0means disabled (the default) and1, enabled. The valid options are:includeβ this makes the returned array contain the searched text.removeβ this removes the returned stream from the input.unquotedβ this ensures that the searched text is outside any quotes.regexβ this denotes that the first argument is a regular expression rather than an plain string.
skip_spaces(\@)β receives as argument the reference to a paragraph (in the format returned byget_string_until), skips his heading spaces and returns them as a simple string.join_lines(@)β returns a simple string with the text from the argument array (discarding the references).
π STATUS OF THIS MODULE π
This module can translate tags and attributes.
π¦ TODO LIST π¦
DOCTYPE (ENTITIES)
There is a minimal support for the translation of entities. They are translated as a whole, and tags are not taken into account. Multilines entities are not supported and entities are always rewrapped during the translation.
MODIFY TAG TYPES FROM INHERITED MODULES (move the tag_types structure inside the $self hash?)
π SEE ALSO π
Locale::Po4a::TransTractor(3pm), po4a(7)
π AUTHORS π
Jordi Vilalta <jvprat@gmail.com>Nicolas FranΓ§ois <nicolas.francois@centraliens.net>
π COPYRIGHT AND LICENSE π
Copyright Β© 2004 Jordi Vilalta <jvprat@gmail.com>
Copyright Β© 2008-2009 Nicolas FranΓ§ois <nicolas.francois@centraliens.net>
This program is free software; you may redistribute it and/or modify it under the terms of GPL v2.0 or later (see the COPYING file).
That's the whole vibe bestie β brought to you by perl v5.42.0, dated 2025-07-13. mwah πβ¨
π¦ 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