Locale:...:Extract(3)
πΈ Locale::Maketext::Extract πΈ
okay so this is the manual for Locale::Maketext::Extract, a Perl module filed under User Contributed Perl Documentation, and bestie it is so good β it pulls translatable strings out of your source files and writes them back into PO files. Let's get into it π
β¨ NAME β¨
Locale::Maketext::Extract β Extract translatable strings from source π
π¦ VERSION π¦
version 1.00
π SYNOPSIS π
my $Ext = Locale::Maketext::Extract->new;
$Ext->read_po('messages.po');
$Ext->extract_file($_) for <*.pl>;
# Set $entries_are_in_gettext_format if the .pl files above use
# loc('%1') instead of loc('[_1]')
$Ext->compile($entries_are_in_gettext_format);
$Ext->write_po('messages.po');
-----------------------------------
### Specifying parser plugins ###
my $Ext = Locale::Maketext::Extract->new(
# Specify which parser plugins to use
plugins => {
# Use Perl parser, process files with extension .pl .pm .cgi
perl => [],
# Use YAML parser, process all files
yaml => ['*'],
# Use TT2 parser, process files with extension .tt2 .tt .html
# or which match the regex
tt2 => [
'tt2',
'tt',
'html',
qr/\.tt2?\./
],
# Use My::Module as a parser for all files
'My::Module' => ['*'],
},
# Warn if a parser can't process a file or problems loading a plugin
warnings => 1,
# List processed files
verbose => 1,
);
π DESCRIPTION π
So this module's whole thing is extracting translatable strings from files and writing them back out to PO files. It can also read in PO files you already have and merge their contents with freshly extracted strings. Iconic behavior, no cap π
There's also a command-line utility, xgettext.pl, installed alongside this module as well. Very much giving "I have a CLI cousin and she's cute".
The format parsers are loaded as plugins, which means β yes bestie β you can define your own parsers. Main character energy π
Following formats of input files are supported:
π Perl source files (plugin: perl)
Valid localization function names are: translate, maketext, gettext, l, loc, x, _ and __.
For a slightly more accurate, but much slower Perl parser, you can use the PPI plugin. This does not have a short name (like perl), but must be specified in full.
π§ HTML::Mason (Mason 1) and Mason (Mason 2) (plugin: mason)
HTML::Mason (aka Mason 1) β strings inside <&|/l>...</&> and <&|/loc>...</&> are extracted.
Mason (aka Mason 2) β strings inside <% $.floc { %>...</%> or <% $.fl { %>...</%> or <% $self->floc { %>...</%> or <% $self->fl { %>...</%> are extracted.
πΈ Template Toolkit (plugin: tt2)
Valid forms are:
[% | l(arg1,argn) %]string[% END %]
[% 'string' | l(arg1,argn) %]
[% l('string',arg1,argn) %]
FILTER and | are interchangeable
l and loc are interchangeable
args are optional
π¦ Text::Template (plugin: text)
Sentences between STARTxxx and ENDxxx are extracted individually.
β YAML (plugin: yaml)
Valid forms are _"string" or _'string', eg:
title: _"My title"
desc: _'My "quoted" string'
Quotes do not have to be escaped, so you could also do:
desc: _"My "quoted" string"
π HTML::FormFu (plugin: formfu)
HTML::FormFu uses a config-file to generate forms, with built in support for localizing errors, labels etc.
We extract the text after "_loc: ":
content_loc: this is the string
message_loc: ['Max string length: [_1]', 10]
π Generic Template (plugin: generic)
Strings inside {{...}} are extracted.
π«Ά METHODS π«Ά
π Constructor
new()
new(
plugins => {...},
warnings => 1 | 0,
verbose => 0 | 1 | 2 | 3,
)
See "Plugins", "Warnings" and "Verbose" for details.
π Plugins
$ext->plugins({...});
Locale::Maketext::Extract uses plugins (see below for the list) to parse different formats.
Each plugin can also specify which file types it can parse.
# use only the YAML plugin
# only parse files with the default extension list defined in the plugin
# ie .yaml .yml .conf
$ext->plugins({
yaml => [],
})
# use only the Perl plugin
# parse all file types
$ext->plugins({
perl => '*'
})
$ext->plugins({
tt2 => [
'tt', # matches base filename against /\.tt$/
qr/\.tt2?\./, # matches base filename against regex
\&my_filter, # codref called
]
})
sub my_filter {
my ($base_filename,$path_to_file) = @_;
return 1 | 0;
}
# Specify your own parser
# only parse files with the default extension list defined in the plugin
$ext->plugins({
'My::Extract::Parser' => []
})
By default, if no plugins are specified, it first tries to determine which plugins are intended specifically for the file type and uses them. If no such plugins are found, it then uses all of the builtin plugins, overriding the file types specified in each.
π Available plugins
perl:Locale::Maketext::Extract::Plugin::Perlβ for a slightly more accurate but much slower Perl parser, you can use the PPI plugin. This does not have a short name, but must be specified in full, ie:Locale::Maketext::Extract::Plugin::PPItt2:Locale::Maketext::Extract::Plugin::TT2yaml:Locale::Maketext::Extract::Plugin::YAMLformfu:Locale::Maketext::Extract::Plugin::FormFumason:Locale::Maketext::Extract::Plugin::Masontext:Locale::Maketext::Extract::Plugin::TextTemplategeneric:Locale::Maketext::Extract::Plugin::Generic
Also, see Locale::Maketext::Extract::Plugin::Base for details of how to write your own plugin. So. Many. Options. We love to see it π
π Warnings
Because the YAML and TT2 plugins use proper parsers, rather than just regexes, if a source file is not valid and it is unable to parse the file, then the parser will throw an error and abort parsing.
The next enabled plugin will be tried.
By default, you will not see these errors. If you would like to see them, then enable warnings via new(). All parse errors will be printed to STDERR.
Also, if developing your own plugin, turn on warnings to see any errors that result from loading your plugin. (You got this, bestie π«Ά)
π₯ Verbose
If you would like to see which files have been processed, which plugins were used, and which strings were extracted, then enable "verbose". If no acceptable plugin was found, or no strings were extracted, then the file is not listed:
$ext = Locale::Extract->new( verbose => 1 | 2 | 3);
OR
xgettext.pl ... -v # files reported
xgettext.pl ... -v -v # files and plugins reported
xgettext.pl ... -v -v -v # files, plugins and strings reported
π Accessors
header, set_header
lexicon, set_lexicon, msgstr, set_msgstr
entries, set_entries, entry, add_entry, del_entry
compiled_entries, set_compiled_entries, compiled_entry,
add_compiled_entry, del_compiled_entry
clear
π¦ PO File manipulation
method read_po ($file)
method write_po ($file, $add_format_marker?)
π Extraction
extract
extract_file
β Compilation
compile($entries_are_in_gettext_style?)
Merges the "entries" into "compiled_entries".
If $entries_are_in_gettext_style is true, the previously extracted entries are assumed to be in the Gettext style (e.g. %1).
Otherwise they are assumed to be in Maketext style (e.g. "[_1]") and are converted into Gettext style before merging into "compiled_entries".
The "entries" are not cleared after each compilation; use set_entries() to clear them if you need to extract from sources with varying styles. Important lil caveat, don't forget it π
normalize_space
π Lexicon accessors
msgids, has_msgid,
msgstr, set_msgstr
msg_positions, msg_variables, msg_format, msg_out
π§ Internal utilities
_default_header
_maketext_to_gettext
_escape
_format
_plugins_specifically_for_file
πΈ ACKNOWLEDGMENTS πΈ
Thanks to Jesse Vincent for contributing to an early version of this module. π«Ά
Also to Alain Barbet, who effectively re-wrote the source parser with a flex-like algorithm. Actual legend behavior.
π SEE ALSO π
xgettext.pl, Locale::Maketext, Locale::Maketext::Lexicon
π AUTHORS π
Audrey Tang <cpan@audreyt.org>
π COPYRIGHT π
Copyright 2003-2013 by Audrey Tang <cpan@audreyt.org>.
This software is released under the MIT license cited below.
π The "MIT" License π
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
okay yes that's the whole legal bit and yes you have to read it, it's literally the license π but it's important!!
β¨ AUTHORS β¨
- Clinton Gormley <drtech@cpan.org>
- Audrey Tang <cpan@audreyt.org>
π¦ COPYRIGHT AND LICENSE π¦
This software is Copyright (c) 2014 by Audrey Tang.
This is free software, licensed under:
The MIT (X11) License
and that's the whole page bestie, you ate that π
β¨ β this was Locale:...:Extract(3) from User Contributed Perl Documentation, perl v5.40.2, dated 2025-01-03. go forth and localize literally everything ππ«Ά
π¦ where this page comes from
- alpine 3.22: perl-locale-maketext-lexicon-doc 1.00-r6 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms