1Locale::Maketext::CookbUosoekr(3C)ontributed Perl DocumeLnotcaatlieo:n:Maketext::Cookbook(3)
2
3
4

NAME

6       Locale::Maketext::Cookbook - recipes for using Locale::Maketext
7

INTRODUCTION

9       This is a work in progress. Not much progress by now :-)
10

ONESIDED LEXICONS

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

DECIMAL PLACES IN NUMBER FORMATTING

60       After CPAN RT #36136 (https://rt.cpan.org/Ticket/Display.html?id=36136)
61
62       The documentation of Locale::Maketext advises that the standard bracket
63       method "numf" is limited and that you must override that for better
64       results. It even suggests the use of Number::Format.
65
66       One such defect of standard "numf" is to not be able to use a certain
67       decimal precision.  For example,
68
69           $lh->maketext('pi is [numf,_1]', 355/113);
70
71       outputs
72
73           pi is 3.14159292035398
74
75       Since pi ≈ 355/116 is only accurate to 6 decimal places, you would want
76       to say:
77
78           $lh->maketext('pi is [numf,_1,6]', 355/113);
79
80       and get "pi is 3.141592".
81
82       One solution for that could use "Number::Format" like that:
83
84           package Wuu;
85
86           use base qw(Locale::Maketext);
87
88           use Number::Format;
89
90           # can be overridden according to language conventions
91           sub _numf_params {
92               return (
93                   -thousands_sep  => '.',
94                   -decimal_point  => ',',
95                   -decimal_digits => 2,
96               );
97           }
98
99           # builds a Number::Format
100           sub _numf_formatter {
101               my ($lh, $scale) = @_;
102               my @params = $lh->_numf_params;
103               if ($scale) { # use explicit scale rather than default
104                   push @params, (-decimal_digits => $scale);
105               }
106               return Number::Format->new(@params);
107           }
108
109           sub numf {
110               my ($lh, $n, $scale) = @_;
111               # get the (cached) formatter
112               my $nf = $lh->{__nf}{$scale} ||= $lh->_numf_formatter($scale);
113               # format the number itself
114               return $nf->format_number($n);
115           }
116
117           package Wuu::pt;
118
119           use base qw(Wuu);
120
121       and then
122
123           my $lh = Wuu->get_handle('pt');
124           $lh->maketext('A [numf,_1,3] km de distância', 1550.2222);
125
126       would return "A 1.550,222 km de distância".
127
128       Notice that the standard utility methods of "Locale::Maketext" are
129       irremediably limited because they could not aim to do everything that
130       could be expected from them in different languages, cultures and
131       applications. So extending "numf", "quant", and "sprintf" is natural as
132       soon as your needs exceed what the standard ones do.
133
134
135
136perl v5.28.0                      2014-10-13     Locale::Maketext::Cookbook(3)
Impressum