1Locale::Maketext::CookbUosoekr(3C)ontributed Perl DocumeLnotcaatlieo:n:Maketext::Cookbook(3)
2
3
4
6 Locale::Maketext::Cookbook - recipes for using Locale::Maketext
7
9 This is a work in progress. Not much progress by now :-)
10
12 Adapted from a suggestion by Dan Muey
13
14 It may be common (for example at your main lexicon) that the hash keys
15 and values coincide. Like that
16
17 q{Hello, tell me your name}
18 => q{Hello, tell me your name}
19
20 It would be nice to just write:
21
22 q{Hello, tell me your name} => ''
23
24 and have this magically inflated to the first form. Among the
25 advantages of such representation, that would lead to smaller files,
26 less prone to mistyping or mispasting, and handy to someone translating
27 it which can simply copy the main lexicon and enter the translation
28 instead of having to remove the value first.
29
30 That can be achieved by overriding "init" in your class and working on
31 the main lexicon with code like that:
32
33 package My::I18N;
34 ...
35
36 sub init {
37 my $lh = shift; # a newborn handle
38 $lh->SUPER::init();
39 inflate_lexicon(\%My::I18N::en::Lexicon);
40 return;
41 }
42
43 sub inflate_lexicon {
44 my $lex = shift;
45 while (my ($k, $v) = each %$lex) {
46 $v = $k if !defined $v || $v eq '';
47 }
48 }
49
50 Here we are assuming "My::I18N::en" to own the main lexicon.
51
52 There are some downsides here: the size economy will not stand at
53 runtime after this init() runs. But it should not be that critical,
54 since if you don't have space for that, you won't have space for any
55 other language besides the main one as well. You could do that too with
56 ties, expanding the value at lookup time which should be more time
57 expensive as an option.
58
60 After CPAN RT #36136
61 (<https://rt.cpan.org/Ticket/Display.html?id=36136>)
62
63 The documentation of Locale::Maketext advises that the standard bracket
64 method "numf" is limited and that you must override that for better
65 results. It even suggests the use of Number::Format.
66
67 One such defect of standard "numf" is to not be able to use a certain
68 decimal precision. For example,
69
70 $lh->maketext('pi is [numf,_1]', 355/113);
71
72 outputs
73
74 pi is 3.14159292035398
75
76 Since pi ≈ 355/116 is only accurate to 6 decimal places, you would want
77 to say:
78
79 $lh->maketext('pi is [numf,_1,6]', 355/113);
80
81 and get "pi is 3.141592".
82
83 One solution for that could use "Number::Format" like that:
84
85 package Wuu;
86
87 use base qw(Locale::Maketext);
88
89 use Number::Format;
90
91 # can be overridden according to language conventions
92 sub _numf_params {
93 return (
94 -thousands_sep => '.',
95 -decimal_point => ',',
96 -decimal_digits => 2,
97 );
98 }
99
100 # builds a Number::Format
101 sub _numf_formatter {
102 my ($lh, $scale) = @_;
103 my @params = $lh->_numf_params;
104 if ($scale) { # use explicit scale rather than default
105 push @params, (-decimal_digits => $scale);
106 }
107 return Number::Format->new(@params);
108 }
109
110 sub numf {
111 my ($lh, $n, $scale) = @_;
112 # get the (cached) formatter
113 my $nf = $lh->{__nf}{$scale} ||= $lh->_numf_formatter($scale);
114 # format the number itself
115 return $nf->format_number($n);
116 }
117
118 package Wuu::pt;
119
120 use base qw(Wuu);
121
122 and then
123
124 my $lh = Wuu->get_handle('pt');
125 $lh->maketext('A [numf,_1,3] km de distância', 1550.2222);
126
127 would return "A 1.550,222 km de distância".
128
129 Notice that the standard utility methods of "Locale::Maketext" are
130 irremediably limited because they could not aim to do everything that
131 could be expected from them in different languages, cultures and
132 applications. So extending "numf", "quant", and "sprintf" is natural as
133 soon as your needs exceed what the standard ones do.
134
135
136
137perl v5.36.0 2023-01-20 Locale::Maketext::Cookbook(3)