CLI/L10N.pm

filter: Statementstmt 100% 28 / 28 Branchbran 50.0% 2 / 4 Conditioncond 50.0% 3 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 100% 10 / 10 Podpod 100% 3 / 3 totaltotal 85.1% 46 / 54 CC 5 SCAR 8.0
CC
5
Cov
87%
_init_handle
4.8
set_language
2.0
BEGIN
1.0
CRAP 2.2
« CLI/Argument/TypeTiny.pm ↑ Summary CLI/Argument.pm »
1 #=============================================================================
2 #
3 # Module: Term::CLI::L10N
4 #
5 # Author: Steven Bakker (SBAKKER), <sbakker@cpan.org>
6 # Created: 27/02/18
7 #
8 # Copyright (c) 2018-2022 Steven Bakker; All rights reserved.
9 #
10 # This module is free software; you can redistribute it and/or modify
11 # it under the same terms as Perl itself. See "perldoc perlartistic."
12 #
13 # This software is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16 #
17 #=============================================================================
18  
19 package Term::CLI::L10N 0.061000;
20  
21 20 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 20 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 20 ▶use parent 0.225 qw( Locale::Maketext Exporter );
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 20 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 20 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 BEGIN {
31 20 ▶ our @EXPORT_OK = qw( __ loc );
Subroutine BEGIN: called CC=1 SCAR=0.0
32 20 our %EXPORT_TAGS = ( 'all' => \@EXPORT_OK );
33 }
34  
35 20 my $lh;
36  
37 sub _init_handle {
38 222 ▶ $lh //= __PACKAGE__->get_handle() || __PACKAGE__->get_handle('en');
Subroutine _init_handle: called CC=4 SCAR=15.6
Condition50%3/6
$lh //= __PACKAGE__->get_handle || __PACKAGE__->get_handle("en")
l!l&&r!l&&!r
22110
__PACKAGE__->get_handle || __PACKAGE__->get_handle('en')
l!l&&r!l&&!r
100
MC/DC0%0/3
$lh //= __PACKAGE__->get_handle || __PACKAGE__->get_handle("en")
$lh __PACKAGE__->get_handle __PACKAGE__->get_handle('en')
Truth table0%0/4
A //= B || C
A= $lhB= __PACKAGE__->get_handleC= __PACKAGE__->get_handle('en')
ABCresult
1XX1
01X1
0011
0000
39 222 ▶ $lh or croak "No language files for 'en'";
Branch: 1 branch
BranchTrueFalseTotal
$lh or croak("No language files for 'en'") 0 222 222
40 222 return $lh;
41 }
42  
43 20 *__ = \&loc; # Alias __ to loc().
44  
45 sub handle {
46 150 ▶ return _init_handle();
Subroutine handle: called CC=1 SCAR=0.0
Pod documented
47 }
48  
49 sub loc {
50 72 ▶ _init_handle();
Subroutine loc: called CC=1 SCAR=0.0
Pod documented
51 72 return $lh->SUPER::maketext(@_);
52 }
53  
54 sub set_language {
55 15 ▶ my ( $self, @args ) = @_;
Subroutine set_language: called CC=2 SCAR=7.1
Pod documented
56  
57 15 ▶ $lh = __PACKAGE__->get_handle(@args)
Branch: 1 branch
BranchTrueFalseTotal
$lh = __PACKAGE__->get_handle(@args) or croak("No language files for (@args)") 0 15 15
58 or croak "No language files for (@args)";
59 15 return $lh;
60 }
61  
62 20 1;
63  
64 __END__
65  
66 =pod
67  
68 =head1 NAME
69  
70 Term::CLI::L10N - localizations for Term::CLI
71  
72 =head1 VERSION
73  
74 version 0.061000
75  
76 =head1 SYNOPSIS
77  
78 use Term::CLI::L10N qw( :all );
79  
80 say loc("invalid value"); # "loc" is imported by default.
81  
82 say __("invalid value"); # "__" is not imported by default.
83  
84  
85 my $lh = Term::CLI::L10N->handle();
86  
87 say $lh->maketext("invalid value"); # "maketext" is not imported by default.
88  
89 =head1 DESCRIPTION
90  
91 The C<Term::CLI::L10N> module implements a localization mechanism based
92 on L<Locale::Maketext>(3p).
93  
94 =head1 FUNCTIONS
95  
96 The module can export a few utility routines.
97  
98 =over
99  
100 =item B<__> ( I<Str> [, I<Str> ... ] )
101 X<__>
102  
103 Synonym for L<loc|/loc>.
104  
105 =item B<loc> ( I<Str> [, I<Str> ... ] )
106 X<loc>
107  
108 Call L<Locale::Maketext>'s C<maketext> function
109 on the arguments.
110  
111 =back
112  
113 =head1 CLASS METHODS
114  
115 =over
116  
117 =item B<handle>
118  
119 Return the module's L<Locale::Maketext> handle.
120  
121 =item B<set_language> ( I<Str> [, ... ] )
122  
123 Set the language to I<Str>, trying multiple if a list is given.
124  
125 Dies with an error if no language can be loaded.
126  
127 =back
128  
129 =head1 EXAMPLES
130  
131 use Term::CLI::L10N qw( loc ); # Initialise using current locale.
132  
133 Term::CLI::L10N->set_language('nl'); # Force "nl" language.
134  
135 say loc("ERROR"); # Should print "FOUT".
136  
137 =head1 SEE ALSO
138  
139 L<Term::CLI::L10N::en>(3p),
140 L<Term::CLI::L10N::nl>(3p),
141 L<Locale::Maketext>(3p),
142 L<Locale::Maketext::Lexicon>(3p).
143  
144 =head1 AUTHOR
145  
146 Steven Bakker E<lt>sbakker@cpan.orgE<gt>.
147  
148 =head1 COPYRIGHT AND LICENSE
149  
150 Copyright (c) 2018 Steven Bakker; All rights reserved.
151  
152 This module is free software; you can redistribute it and/or modify
153 it under the same terms as Perl itself. See "perldoc perlartistic."
154  
155 This software is distributed in the hope that it will be useful,
156 but WITHOUT ANY WARRANTY; without even the implied warranty of
157 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
158  
159 =cut
« CLI/Argument/TypeTiny.pm ↑ Summary CLI/Argument.pm »