Locale...okbook(3perl)
π Locale::Maketext::Cookbook π
okay bestie, welcome to the Cookbook β it's part of the Perl Programmers Reference Guide, and it's exactly what it sounds like: lil recipes for cooking with Locale::Maketext. π
β¨ NAME β¨
Locale::Maketext::Cookbook β recipes for using Locale::Maketext π§
πΈ INTRODUCTION πΈ
soβ¦ this is a work in progress. and ngl, not much progress has been made by now :-) π
π ONESIDED LEXICONS π
adapted from a suggestion by Dan Muey π«Ά
okay so here's the vibe: it may be super common β like, say, at your main lexicon β that the hash keys and the values are the exact same thing. like this:
q{Hello, tell me your name}
=> q{Hello, tell me your name}
and wouldn't it be so nice to just write:
q{Hello, tell me your name} => ''
and have that magically inflate itself into the first form? β¨ among the advantages of this kind of representation: smaller files, way less prone to mistyping or mispasting, and honestly so handy for someone translating it β they can just copy the main lexicon and drop in the translation instead of having to delete the value first. main character workflow fr fr π
you can pull that off by overriding "init" in your class and doing some work on the main lexicon, like this:
package My::I18N;
...
sub init {
my $lh = shift; # a newborn handle
$lh->SUPER::init();
inflate_lexicon(\%My::I18N::en::Lexicon);
return;
}
sub inflate_lexicon {
my $lex = shift;
while (my ($k, $v) = each %$lex) {
$v = $k if !defined $v || $v eq '';
}
}
here we're just assuming that "My::I18N::en" owns the main lexicon. π
now the downsides, because there are always downsides π β the size economy will not stand at runtime once this init() has run. but honestly that shouldn't be that critical, because if you don't have space for that, you won't have space for any other language besides the main one either. you could also do the same thing with ties, expanding the value at lookup time β which should be more time expensive as an option. pick your poison bestie π
π DECIMAL PLACES IN NUMBER FORMATTING π
after CPAN RT #36136 (https://rt.cpan.org/Ticket/Display.html?id=36136) π
so the docs for Locale::Maketext say the standard bracket method "numf" is limited and that you must override it for better results. it even suggests using Number::Format. iconic of it to admit that tbh.
one such defect of the standard "numf" is that you can't use a specific decimal precision. for example:
$lh->maketext('pi is [numf,_1]', 355/113);
outputs:
pi is 3.14159292035398
and since pi β 355/116 is only accurate to 6 decimal places, you'd lowkey want to say:
$lh->maketext('pi is [numf,_1,6]', 355/113);
and get "pi is 3.141592". slay. β¨
one solution for that could use "Number::Format" like so:
package Wuu;
use base qw(Locale::Maketext);
use Number::Format;
# can be overridden according to language conventions
sub _numf_params {
return (
-thousands_sep => '.',
-decimal_point => ',',
-decimal_digits => 2,
);
}
# builds a Number::Format
sub _numf_formatter {
my ($lh, $scale) = @_;
my @params = $lh->_numf_params;
if ($scale) { # use explicit scale rather than default
push @params, (-decimal_digits => $scale);
}
return Number::Format->new(@params);
}
sub numf {
my ($lh, $n, $scale) = @_;
# get the (cached) formatter
my $nf = $lh->{__nf}{$scale} ||= $lh->_numf_formatter($scale);
# format the number itself
return $nf->format_number($n);
}
package Wuu::pt;
use base qw(Wuu);
and then:
my $lh = Wuu->get_handle('pt');
$lh->maketext('A [numf,_1,3] km de distΓ’ncia', 1550.2222);
would return "A 1.550,222 km de distΓ’ncia". π
notice how the standard utility methods of"Locale::Maketext"are irremediably limited β because they could not aim to do everything that could be expected from them across different languages, cultures and applications. so extending"numf","quant", and"sprintf"is totally natural as soon as your needs exceed what the standard ones do. no cap. π―
and that's the whole cookbook bestie π«Ά this page is from perl v5.44.0, dated 2026-10-17 β go forth and localize, you absolute icon ππ
π¦ where this page comes from
- arch rolling: perl 5.44.0-1 Β· Artistic-1.0-Perl AND GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms