Debian Codegraph @

upstream/opensuse-tumbleweed/man3/Locale::Maketext::Cookbook.3pm

raw
1 .\" -*- mode: troff; coding: utf-8 -*-
2 .\" Automatically generated by Pod::Man v6.0.2 (Pod::Simple 3.45)
3 .\"
4 .\" Standard preamble:
5 .\" ========================================================================
6 .de Sp \" Vertical space (when we can't use .PP)
7 .if t .sp .5v
8 .if n .sp
9 ..
10 .de Vb \" Begin verbatim text
11 .ft CW
12 .nf
13 .ne \\$1
14 ..
15 .de Ve \" End verbatim text
16 .ft R
17 .fi
18 ..
19 .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>.
20 .ie n \{\
21 . ds C` ""
22 . ds C' ""
23 'br\}
24 .el\{\
25 . ds C`
26 . ds C'
27 'br\}
28 .\"
29 .\" Escape single quotes in literal strings from groff's Unicode transform.
30 .ie \n(.g .ds Aq \(aq
31 .el .ds Aq '
32 .\"
33 .\" If the F register is >0, we'll generate index entries on stderr for
34 .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
35 .\" entries marked with X<> in POD. Of course, you'll have to process the
36 .\" output yourself in some meaningful fashion.
37 .\"
38 .\" Avoid warning from groff about undefined register 'F'.
39 .de IX
40 ..
41 .nr rF 0
42 .if \n(.g .if rF .nr rF 1
43 .if (\n(rF:(\n(.g==0)) \{\
44 . if \nF \{\
45 . de IX
46 . tm Index:\\$1\t\\n%\t"\\$2"
47 ..
48 . if !\nF==2 \{\
49 . nr % 0
50 . nr F 2
51 . \}
52 . \}
53 .\}
54 .rr rF
55 .\"
56 .\" Required to disable full justification in groff 1.23.0.
57 .if n .ds AD l
58 .\" ========================================================================
59 .\"
60 .IX Title "Locale::Maketext::Cookbook 3pm"
61 .TH Locale::Maketext::Cookbook 3pm 2026-03-09 "perl v5.42.1" "Perl Programmers Reference Guide"
62 .\" For nroff, turn off justification. Always turn off hyphenation; it makes
63 .\" way too many mistakes in technical documents.
64 .if n .ad l
65 .nh
66 .SH NAME
67 Locale::Maketext::Cookbook \- recipes for using Locale::Maketext
68 .SH INTRODUCTION
69 .IX Header "INTRODUCTION"
70 This is a work in progress. Not much progress by now :\-)
71 .SH "ONESIDED LEXICONS"
72 .IX Header "ONESIDED LEXICONS"
73 \&\fIAdapted from a suggestion by Dan Muey\fR
74 .PP
75 It may be common (for example at your main lexicon) that
76 the hash keys and values coincide. Like that
77 .PP
78 .Vb 2
79 \& q{Hello, tell me your name}
80 \& => q{Hello, tell me your name}
81 .Ve
82 .PP
83 It would be nice to just write:
84 .PP
85 .Vb 1
86 \& q{Hello, tell me your name} => \*(Aq\*(Aq
87 .Ve
88 .PP
89 and have this magically inflated to the first form.
90 Among the advantages of such representation, that would
91 lead to
92 smaller files, less prone to mistyping or mispasting,
93 and handy to someone translating it which can simply
94 copy the main lexicon and enter the translation
95 instead of having to remove the value first.
96 .PP
97 That can be achieved by overriding \f(CW\*(C`init\*(C'\fR
98 in your class and working on the main lexicon
99 with code like that:
100 .PP
101 .Vb 2
102 \& package My::I18N;
103 \& ...
104 \&
105 \& sub init {
106 \& my $lh = shift; # a newborn handle
107 \& $lh\->SUPER::init();
108 \& inflate_lexicon(\e%My::I18N::en::Lexicon);
109 \& return;
110 \& }
111 \&
112 \& sub inflate_lexicon {
113 \& my $lex = shift;
114 \& while (my ($k, $v) = each %$lex) {
115 \& $v = $k if !defined $v || $v eq \*(Aq\*(Aq;
116 \& }
117 \& }
118 .Ve
119 .PP
120 Here we are assuming \f(CW\*(C`My::I18N::en\*(C'\fR to own the
121 main lexicon.
122 .PP
123 There are some downsides here: the size economy
124 will not stand at runtime after this \f(CWinit()\fR
125 runs. But it should not be that critical, since
126 if you don\*(Aqt have space for that, you won\*(Aqt have
127 space for any other language besides the main one
128 as well. You could do that too with ties,
129 expanding the value at lookup time which
130 should be more time expensive as an option.
131 .SH "DECIMAL PLACES IN NUMBER FORMATTING"
132 .IX Header "DECIMAL PLACES IN NUMBER FORMATTING"
133 \&\fIAfter CPAN RT #36136 (<https://rt.cpan.org/Ticket/Display.html?id=36136>)\fR
134 .PP
135 The documentation of Locale::Maketext advises that
136 the standard bracket method \f(CW\*(C`numf\*(C'\fR is limited and that
137 you must override that for better results. It even
138 suggests the use of Number::Format.
139 .PP
140 One such defect of standard \f(CW\*(C`numf\*(C'\fR is to not be
141 able to use a certain decimal precision.
142 For example,
143 .PP
144 .Vb 1
145 \& $lh\->maketext(\*(Aqpi is [numf,_1]\*(Aq, 355/113);
146 .Ve
147 .PP
148 outputs
149 .PP
150 .Vb 1
151 \& pi is 3.14159292035398
152 .Ve
153 .PP
154 Since pi ≈ 355/116 is only accurate
155 to 6 decimal places, you would want to say:
156 .PP
157 .Vb 1
158 \& $lh\->maketext(\*(Aqpi is [numf,_1,6]\*(Aq, 355/113);
159 .Ve
160 .PP
161 and get "pi is 3.141592".
162 .PP
163 One solution for that could use \f(CW\*(C`Number::Format\*(C'\fR
164 like that:
165 .PP
166 .Vb 1
167 \& package Wuu;
168 \&
169 \& use base qw(Locale::Maketext);
170 \&
171 \& use Number::Format;
172 \&
173 \& # can be overridden according to language conventions
174 \& sub _numf_params {
175 \& return (
176 \& \-thousands_sep => \*(Aq.\*(Aq,
177 \& \-decimal_point => \*(Aq,\*(Aq,
178 \& \-decimal_digits => 2,
179 \& );
180 \& }
181 \&
182 \& # builds a Number::Format
183 \& sub _numf_formatter {
184 \& my ($lh, $scale) = @_;
185 \& my @params = $lh\->_numf_params;
186 \& if ($scale) { # use explicit scale rather than default
187 \& push @params, (\-decimal_digits => $scale);
188 \& }
189 \& return Number::Format\->new(@params);
190 \& }
191 \&
192 \& sub numf {
193 \& my ($lh, $n, $scale) = @_;
194 \& # get the (cached) formatter
195 \& my $nf = $lh\->{_\|_nf}{$scale} ||= $lh\->_numf_formatter($scale);
196 \& # format the number itself
197 \& return $nf\->format_number($n);
198 \& }
199 \&
200 \& package Wuu::pt;
201 \&
202 \& use base qw(Wuu);
203 .Ve
204 .PP
205 and then
206 .PP
207 .Vb 2
208 \& my $lh = Wuu\->get_handle(\*(Aqpt\*(Aq);
209 \& $lh\->maketext(\*(AqA [numf,_1,3] km de distância\*(Aq, 1550.2222);
210 .Ve
211 .PP
212 would return "A 1.550,222 km de distância".
213 .PP
214 Notice that the standard utility methods of
215 \&\f(CW\*(C`Locale::Maketext\*(C'\fR are irremediably limited
216 because they could not aim to do everything
217 that could be expected from them in different languages,
218 cultures and applications. So extending \f(CW\*(C`numf\*(C'\fR,
219 \&\f(CW\*(C`quant\*(C'\fR, and \f(CW\*(C`sprintf\*(C'\fR is natural as soon
220 as your needs exceed what the standard ones do.