blib/lib/Locale/Maketext/Simple.pm

filter: Statementstmt 35.5% 32 / 90 Branchbran 10.0% 5 / 50 Conditioncond 23.3% 7 / 30 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 37.5% 6 / 16 Podpod 0.0% 0 / 4 totaltotal 24.0% 50 / 208 CC 39 SCAR 41.4
CC
39
Cov
26%
load_loc
161.0
_default_gettext
72.0
auto_path
42.0
CRAP 63.0
↑ Summary
1 package Locale::Maketext::Simple;
2 2 $Locale::Maketext::Simple::VERSION = '0.21';
3  
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use 5.005;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 =head1 NAME
8  
9 Locale::Maketext::Simple - Simple interface to Locale::Maketext::Lexicon
10  
11 =head1 VERSION
12  
13 This document describes version 0.18 of Locale::Maketext::Simple,
14 released Septermber 8, 2006.
15  
16 =head1 SYNOPSIS
17  
18 Minimal setup (looks for F<auto/Foo/*.po> and F<auto/Foo/*.mo>):
19  
20 package Foo;
21 use Locale::Maketext::Simple; # exports 'loc'
22 loc_lang('fr'); # set language to French
23 sub hello {
24 print loc("Hello, [_1]!", "World");
25 }
26  
27 More sophisticated example:
28  
29 package Foo::Bar;
30 use Locale::Maketext::Simple (
31 Class => 'Foo', # search in auto/Foo/
32 Style => 'gettext', # %1 instead of [_1]
33 Export => 'maketext', # maketext() instead of loc()
34 Subclass => 'L10N', # Foo::L10N instead of Foo::I18N
35 Decode => 1, # decode entries to unicode-strings
36 Encoding => 'locale', # but encode lexicons in current locale
37 # (needs Locale::Maketext::Lexicon 0.36)
38 );
39 sub japh {
40 print maketext("Just another %1 hacker", "Perl");
41 }
42  
43 =head1 DESCRIPTION
44  
45 This module is a simple wrapper around B<Locale::Maketext::Lexicon>,
46 designed to alleviate the need of creating I<Language Classes> for
47 module authors.
48  
49 The language used is chosen from the loc_lang call. If a lookup is not
50 possible, the i-default language will be used. If the lookup is not in the
51 i-default language, then the key will be returned.
52  
53 If B<Locale::Maketext::Lexicon> is not present, it implements a
54 minimal localization function by simply interpolating C<[_1]> with
55 the first argument, C<[_2]> with the second, etc. Interpolated
56 function like C<[quant,_1]> are treated as C<[_1]>, with the sole
57 exception of C<[tense,_1,X]>, which will append C<ing> to C<_1> when
58 X is C<present>, or appending C<ed> to <_1> otherwise.
59  
60 =head1 OPTIONS
61  
62 All options are passed either via the C<use> statement, or via an
63 explicit C<import>.
64  
65 =head2 Class
66  
67 By default, B<Locale::Maketext::Simple> draws its source from the
68 calling package's F<auto/> directory; you can override this behaviour
69 by explicitly specifying another package as C<Class>.
70  
71 =head2 Path
72  
73 If your PO and MO files are under a path elsewhere than C<auto/>,
74 you may specify it using the C<Path> option.
75  
76 =head2 Style
77  
78 By default, this module uses the C<maketext> style of C<[_1]> and
79 C<[quant,_1]> for interpolation. Alternatively, you can specify the
80 C<gettext> style, which uses C<%1> and C<%quant(%1)> for interpolation.
81  
82 This option is case-insensitive.
83  
84 =head2 Export
85  
86 By default, this module exports a single function, C<loc>, into its
87 caller's namespace. You can set it to another name, or set it to
88 an empty string to disable exporting.
89  
90 =head2 Subclass
91  
92 By default, this module creates an C<::I18N> subclass under the
93 caller's package (or the package specified by C<Class>), and stores
94 lexicon data in its subclasses. You can assign a name other than
95 C<I18N> via this option.
96  
97 =head2 Decode
98  
99 If set to a true value, source entries will be converted into
100 utf8-strings (available in Perl 5.6.1 or later). This feature
101 needs the B<Encode> or B<Encode::compat> module.
102  
103 =head2 Encoding
104  
105 Specifies an encoding to store lexicon entries, instead of
106 utf8-strings. If set to C<locale>, the encoding from the current
107 locale setting is used. Implies a true value for C<Decode>.
108  
109 =cut
110  
111 2 ▶sub import {
Subroutine import: called CC=8 SCAR=23.6
112 2 my ($class, %args) = @_;
113  
114 2 ▶ $args{Class} ||= caller;
Condition33%1/3
$args{"Class"} ||= caller
l!l&&r!l&&!r
020
MC/DC0%0/2
$args{"Class"} ||= caller
$args{"Class"} caller
Truth table33%1/3
A ||= B
A= $args{"Class"}B= caller
ABresult
1X1
011
000
115 2 ▶ $args{Style} ||= 'maketext';
Condition50%1/2
$args{"Style"} ||= "maketext"
l!l
20
MC/DC0%0/1
$args{"Style"} ||= "maketext"
$args{"Style"}
Truth table50%1/2
A
A= $args{"Style"}
Aresult
11
00
116 2 ▶ $args{Export} ||= 'loc';
Condition50%1/2
$args{"Export"} ||= "loc"
l!l
02
MC/DC0%0/1
$args{"Export"} ||= "loc"
$args{"Export"}
Truth table50%1/2
A
A= $args{"Export"}
Aresult
11
00
117 2 ▶ $args{Subclass} ||= 'I18N';
Condition50%1/2
$args{"Subclass"} ||= "I18N"
l!l
02
MC/DC0%0/1
$args{"Subclass"} ||= "I18N"
$args{"Subclass"}
Truth table50%1/2
A
A= $args{"Subclass"}
Aresult
11
00
118  
119 2 my ($loc, $loc_lang) = $class->load_loc(%args);
120 2 ▶ $loc ||= $class->default_loc(%args);
Condition33%1/3
$loc ||= $class->default_loc(%args)
l!l&&r!l&&!r
020
MC/DC0%0/2
$loc ||= $class->default_loc(%args)
$loc $class->default_loc(%args)
Truth table33%1/3
A ||= B
A= $locB= $class->default_loc(%args)
ABresult
1X1
011
000
121  
122 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
123 2 ▶ *{caller(0) . "::$args{Export}"} = $loc if $args{Export};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"Export"} 2 0 2
124 0 ▶ *{caller(0) . "::$args{Export}_lang"} = $loc_lang || sub { 1 };
Subroutine __ANON__: not called CC=1 SCAR=1.2
Condition33%1/3
$loc_lang || sub { 1; }
l!l&&r!l&&!r
020
MC/DC0%0/2
$loc_lang || sub { 1; }
$loc_lang sub { 1; }
Truth table33%1/3
A || B
A= $loc_langB= sub { 1; }
ABresult
1X1
011
000
125 }
126  
127 2 my %Loc;
128  
129 0 ▶sub reload_loc { %Loc = () }
Subroutine reload_loc: not called CC=1 SCAR=6.9
Pod undocumented
130  
131 2 ▶sub load_loc {
Subroutine load_loc: called CC=15 SCAR=50.8
Pod undocumented
132 2 my ($class, %args) = @_;
133  
134 2 ▶ my $pkg = join('::', grep { defined and length } $args{Class}, $args{Subclass});
Condition33%1/3
defined $_ && length $_
!ll&&!rl&&r
004
MC/DC0%0/2
defined $_ && length $_
defined $_ length $_
Truth table33%1/3
A && B
A= defined $_B= length $_
ABresult
0X0
100
111
135 2 ▶ return $Loc{$pkg} if exists $Loc{$pkg};
Branch: 1 branch
BranchTrueFalseTotal
if exists $Loc{$pkg} 0 2 2
136  
137 0 ▶ eval { require Locale::Maketext::Lexicon; 1 } or return;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require Locale::Maketext::Lexicon; 1 } } or return 0 2 2
138 0 ▶ $Locale::Maketext::Lexicon::VERSION > 0.20 or return;
Branch: 1 branch
BranchTrueFalseTotal
$Locale::Maketext::Lexicon::VERSION > "0.2" or return 0 0 0
139 0 ▶ eval { require File::Spec; 1 } or return;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require File::Spec; 1 } } or return 0 0 0
140  
141 0 ▶ my $path = $args{Path} || $class->auto_path($args{Class}) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $path = $args{"Path"} || $class->auto_path($args{"Class"}) or return 0 0 0
Condition0%0/3
$args{'Path'} || $class->auto_path($args{'Class'})
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{'Path'} || $class->auto_path($args{'Class'})
$args{'Path'} $class->auto_path($args{'Class'})
Truth table0%0/3
A || B
A= $args{'Path'}B= $class->auto_path($args{'Class'})
ABresult
1X1
011
000
142 0 my $pattern = File::Spec->catfile($path, '*.[pm]o');
143 0 ▶ my $decode = $args{Decode} || 0;
Condition0%0/2
$args{'Decode'} || 0
l!l
00
MC/DC0%0/1
$args{'Decode'} || 0
$args{'Decode'}
Truth table0%0/2
A
A= $args{'Decode'}
Aresult
11
00
144 0 ▶ my $encoding = $args{Encoding} || undef;
Condition0%0/2
$args{'Encoding'} || undef
l!l
00
MC/DC0%0/1
$args{'Encoding'} || undef
$args{'Encoding'}
Truth table0%0/2
A
A= $args{'Encoding'}
Aresult
11
00
145  
146 0 ▶ $decode = 1 if $encoding;
Branch: 1 branch
BranchTrueFalseTotal
if $encoding 0 0 0
147  
148 0 $pattern =~ s{\\}{/}g; # to counter win32 paths
149  
150 0 ▶ eval "
Branch: 1 branch
BranchTrueFalseTotal
eval "\n\tpackage $pkg;\n\tuse base 'Locale::Maketext';\n\tLocale::Maketext::Lexicon->import({\n\t 'i-default' => [ 'Auto' ],\n\t '*'\t=> [ Gettext => \$pattern ],\n\t _decode => \$decode,\n\t _encoding => \$encoding,\n\t});\n\t*${pkg}::Lexicon = \\%${pkg}::i_default::Lexicon;\n\t*tense = sub { \$_[1] . ((\$_[2] eq 'present') ? 'ing' : 'ed') }\n\t unless defined &tense;\n\n\t1;\n " or die $@ 0 0 0
151 package $pkg;
152 use base 'Locale::Maketext';
153 Locale::Maketext::Lexicon->import({
154 'i-default' => [ 'Auto' ],
155 '*' => [ Gettext => \$pattern ],
156 _decode => \$decode,
157 _encoding => \$encoding,
158 });
159 *${pkg}::Lexicon = \\%${pkg}::i_default::Lexicon;
160 *tense = sub { \$_[1] . ((\$_[2] eq 'present') ? 'ing' : 'ed') }
161 unless defined &tense;
162  
163 1;
164 " or die $@;
165  
166 0 ▶ my $lh = eval { $pkg->get_handle } or return;
Branch: 1 branch
BranchTrueFalseTotal
my $lh = eval { do { $pkg->get_handle } } or return 0 0 0
167 0 my $style = lc($args{Style});
168 0 ▶ if ($style eq 'maketext') {
Branch: 2 branches
BranchTrueFalseTotal
if ($style eq 'maketext') { } 0 0 0
elsif ($style eq 'gettext') { } 0 0 0
169 0 ▶ $Loc{$pkg} = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
170 0 $lh->maketext(@_)
171 0 };
172 }
173 elsif ($style eq 'gettext') {
174 0 ▶ $Loc{$pkg} = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
175 0 my $str = shift;
176 0 $str =~ s{([\~\[\]])}{~$1}g;
177 0 $str =~ s{
178 ([%\\]%) # 1 - escaped sequence
179 |
180 % (?:
181 ([A-Za-z#*]\w*) # 2 - function call
182 \(([^\)]*)\) # 3 - arguments
183 |
184 ([1-9]\d*|\*) # 4 - variable
185 )
186 }{
187 0 ▶ $1 ? $1
Branch: 2 branches
BranchTrueFalseTotal
$1 ? : 0 0 0
$2 ? : 0 0 0
188 : $2 ? "\[$2,"._unescape($3)."]"
189 : "[_$4]"
190 }egx;
191 0 return $lh->maketext($str, @_);
192 0 };
193 }
194 else {
195 0 die "Unknown Style: $style";
196 }
197  
198 0 ▶ return $Loc{$pkg}, sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
199 0 $lh = $pkg->get_handle(@_);
200 0 };
201 }
202  
203 2 ▶sub default_loc {
Subroutine default_loc: called CC=3 SCAR=16.0
Pod undocumented
204 2 my ($self, %args) = @_;
205 2 my $style = lc($args{Style});
206 2 ▶ if ($style eq 'maketext') {
Branch: 2 branches
BranchTrueFalseTotal
if ($style eq 'maketext') { } 0 2 2
elsif ($style eq 'gettext') { } 2 0 2
207 0 ▶ return sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
208 0 my $str = shift;
209 0 $str =~ s{((?<!~)(?:~~)*)\[_([1-9]\d*|\*)\]}
210 {$1%$2}g;
211 0 $str =~ s{((?<!~)(?:~~)*)\[([A-Za-z#*]\w*),([^\]]+)\]}
212 0 {"$1%$2(" . _escape($3) . ')'}eg;
213 0 _default_gettext($str, @_);
214 0 };
215 }
216 elsif ($style eq 'gettext') {
217 2 return \&_default_gettext;
218 }
219 else {
220 0 die "Unknown Style: $style";
221 }
222 }
223  
224 0 ▶sub _default_gettext {
Subroutine _default_gettext: not called CC=8 SCAR=42.8
225 0 my $str = shift;
226 0 $str =~ s{
227 % # leading symbol
228 (?: # either one of
229 \d+ # a digit, like %1
230 | # or
231 (\w+)\( # a function call -- 1
232 (?: # either
233 %\d+ # an interpolation
234 | # or
235 ([^,]*) # some string -- 2
236 ) # end either
237 (?: # maybe followed
238 , # by a comma
239 ([^),]*) # and a param -- 3
240 )? # end maybe
241 (?: # maybe followed
242 , # by another comma
243 ([^),]*) # and a param -- 4
244 )? # end maybe
245 [^)]* # and other ignorable params
246 \) # closing function call
247 ) # closing either one of
248 }{
249 0 ▶ my $digit = $2 || shift;
Condition0%0/3
$2 || shift()
l!l&&r!l&&!r
000
MC/DC0%0/2
$2 || shift()
$2 shift()
Truth table0%0/3
A || B
A= $2B= shift()
ABresult
1X1
011
000
250 0 ▶ $digit . (
Branch: 5 branches
BranchTrueFalseTotal
$1 ? : 0 0 0
$1 eq 'tense' ? : 0 0 0
$3 eq 'present' ? : 0 0 0
$1 eq 'quant' ? : 0 0 0
$digit > 1 ? : 0 0 0
Condition0%0/2
$4 or "$3s"
l!l
00
MC/DC0%0/1
$4 or "$3s"
$4
Truth table0%0/2
A
A= $4
Aresult
11
00
251 $1 ? (
252 ($1 eq 'tense') ? (($3 eq 'present') ? 'ing' : 'ed') :
253 ($1 eq 'quant') ? ' ' . (($digit > 1) ? ($4 || "$3s") : $3) :
254 ''
255 ) : ''
256 );
257 }egx;
258 0 return $str;
259 };
260  
261 0 ▶sub _escape {
Subroutine _escape: not called CC=1 SCAR=6.9
262 0 my $text = shift;
263 0 $text =~ s/\b_([1-9]\d*)/%$1/g;
264 0 return $text;
265 }
266  
267 0 ▶sub _unescape {
Subroutine _unescape: not called CC=2 SCAR=17.9
268 join(',', map {
269 0 ▶ /\A(\s*)%([1-9]\d*|\*)(\s*)\z/ ? "$1_$2$3" : $_
Branch: 1 branch
BranchTrueFalseTotal
/\A(\s*)%([1-9]\d*|\*)(\s*)\z/ ? : 0 0 0
270 } split(/,/, $_[0]));
271 }
272  
273 0 ▶sub auto_path {
Subroutine auto_path: not called CC=6 SCAR=37.4
Pod undocumented
274 0 my ($self, $calldir) = @_;
275 0 $calldir =~ s#::#/#g;
276 0 ▶ my $path = $INC{$calldir . '.pm'} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $path = $INC{$calldir . '.pm'} or return 0 0 0
277  
278 # Try absolute path name.
279 0 ▶ if ($^O eq 'MacOS') {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq 'MacOS') { } 0 0 0
280 0 (my $malldir = $calldir) =~ tr#/#:#;
281 0 $path =~ s#^(.*)$malldir\.pm\z#$1auto:$malldir:#s;
282 } else {
283 0 $path =~ s#^(.*)$calldir\.pm\z#$1auto/$calldir/#;
284 }
285  
286 0 ▶ return $path if -d $path;
Branch: 1 branch
BranchTrueFalseTotal
if -d $path 0 0 0
287  
288 # If that failed, try relative path with normal @INC searching.
289 0 $path = "auto/$calldir/";
290 0 foreach my $inc (@INC) {
291 0 ▶ return "$inc/$path" if -d "$inc/$path";
Branch: 1 branch
BranchTrueFalseTotal
if -d "$inc/$path" 0 0 0
292 }
293  
294 0 return;
295 }
296  
297 2 1;
298  
299 =head1 ACKNOWLEDGMENTS
300  
301 Thanks to Jos I. Boumans for suggesting this module to be written.
302  
303 Thanks to Chia-Liang Kao for suggesting C<Path> and C<loc_lang>.
304  
305 =head1 SEE ALSO
306  
307 L<Locale::Maketext>, L<Locale::Maketext::Lexicon>
308  
309 =head1 AUTHORS
310  
311 Audrey Tang E<lt>cpan@audreyt.orgE<gt>
312  
313 =head1 COPYRIGHT
314  
315 Copyright 2003, 2004, 2005, 2006 by Audrey Tang E<lt>cpan@audreyt.orgE<gt>.
316  
317 This software is released under the MIT license cited below. Additionally,
318 when this software is distributed with B<Perl Kit, Version 5>, you may also
319 redistribute it and/or modify it under the same terms as Perl itself.
320  
321 =head2 The "MIT" License
322  
323 Permission is hereby granted, free of charge, to any person obtaining a copy
324 of this software and associated documentation files (the "Software"), to deal
325 in the Software without restriction, including without limitation the rights
326 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
327 copies of the Software, and to permit persons to whom the Software is
328 furnished to do so, subject to the following conditions:
329  
330 The above copyright notice and this permission notice shall be included in
331 all copies or substantial portions of the Software.
332  
333 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
334 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
335 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
336 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
337 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
338 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
339 DEALINGS IN THE SOFTWARE.
340  
341 =cut
↑ Summary