blib/lib/Lingua/Text.pm

filter: Statementstmt 97.7% 87 / 89 Branchbran 89.1% 41 / 46 Conditioncond 83.3% 15 / 18 MC/DCmcdc 72.7% 8 / 11 Subroutinesub 100% 14 / 14 Podpod 100% 4 / 4 totaltotal 92.8% 169 / 182 CC 31 SCAR 17.8
CC
31
Cov
93%
new
10.1
_get_language
8.3
as_string
6.0
CRAP 6.0
↑ Summary
1 package Lingua::Text;
2  
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use HTML::Entities;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use Params::Get;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use Scalar::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # TODO: Investigate Locale::Maketext
12  
13 =head1 NAME
14  
15 Lingua::Text - Class to contain text many different languages
16  
17 =head1 VERSION
18  
19 Version 0.07
20  
21 =cut
22  
23 5 our $VERSION = '0.07';
24  
25 use overload (
26 # '==' => \&equal,
27 # '!=' => \&not_equal,
28 '""' => \&as_string,
29 62 ▶ bool => sub { 1 },
Subroutine __ANON__: called CC=1 SCAR=0.0
30 5 fallback => 1 # So that boolean tests don't cause as_string to be called
31 5 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 =head1 SYNOPSIS
34  
35 Hold many texts in one object,
36 thereby encapsulating internationalized text.
37  
38 use Lingua::Text;
39  
40 my $str = Lingua::Text->new();
41  
42 $str->fr('Bonjour Tout le Monde');
43 $str->en('Hello, World');
44  
45 $ENV{'LANG'} = 'en_GB';
46 print "$str\n"; # Prints Hello, World
47 $ENV{'LANG'} = 'fr_FR';
48 print "$str\n"; # Prints Bonjour Tout le Monde
49 $ENV{'LANG'} = 'de_DE';
50 print "$str\n"; # Prints nothing
51  
52 my $text = Lingua::Text->new('hello'); # Initialises the 'current' language
53  
54 =cut
55  
56 =head1 METHODS
57  
58 =head2 new
59  
60 Create a Lingua::Text object.
61  
62 use Lingua::Text;
63  
64 my $str = Lingua::Text->new({ 'en' => 'Here', 'fr' => 'Ici' });
65  
66 Accepts various input formats, e.g. HASH or reference to a HASH.
67 Clones existing objects with or without modifications.
68 Uses Carp::carp to log warnings for incorrect usage or potential mistakes.
69  
70 =cut
71  
72 sub new {
73 15 ▶ my $class = shift;
Subroutine new: called CC=10 SCAR=23.1
Pod documented
74  
75 # Handle hash or hashref arguments
76 15 my %args;
77 15 ▶ if((scalar(@_) == 1) && (!ref($_[0])) && (my $lang = _get_language())) {
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @_ == 1 and not ref $_[0] and my $lang = _get_language()) { } 2 13 15
elsif (my $params = Params::Get::get_params(undef, @_)) { } 6 7 13
Condition83%5/6
scalar @_ == 1 and not ref $_[0] and my $lang = _get_language()
!ll&&!rl&&r
1302
scalar @_ == 1 and not ref $_[0]
!ll&&!rl&&r
1032
MC/DC66%2/3
scalar @_ == 1 and not ref $_[0] and my $lang = _get_language()
scalar @_ == 1 not ref $_[0] my $lang = _get_language()
Truth table75%3/4
A and not(B) and C
A= scalar @_ == 1B= not ref $_[0]C= my $lang = _get_language()
ABCresult
0XX0
10X0
1100
1111
78 2 $args{$lang} = $_[0];
79 } elsif(my $params = Params::Get::get_params(undef, @_)) {
80 6 %args = %{$params};
81 }
82  
83 15 ▶ if(!defined($class)) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $class) { } 1 14 15
elsif (builtin::blessed($class)) { } 2 12 14
84 1 ▶ if((scalar keys %args) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %args > 0) 0 1 1
85 # Using Lingua::Text->new(), not Lingua::Text::new()
86 0 carp(__PACKAGE__, ' use ->new() not ::new() to instantiate');
87 0 return;
88 }
89  
90 # FIXME: this only works when no arguments are given
91 1 $class = __PACKAGE__;
92 } elsif(Scalar::Util::blessed($class)) {
93 # If $class is an object, clone it with new arguments
94 2 ▶ if(scalar(%args)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar %args) 1 1 2
95 1 return bless { texts => {%{$class->{'texts'}}, %args} }, ref($class);
96 }
97 1 return bless { %{$class} }, ref($class);
98 }
99  
100 # Return the blessed object
101 13 ▶ if(scalar(%args)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar %args) 7 6 13
102 7 return bless { texts => \%args }, $class;
103 }
104  
105 6 return bless { }, $class;
106 }
107  
108 =head2 set
109  
110 Sets a text in a language.
111  
112 $str->set({ text => 'House', lang => 'en' });
113  
114 Autoload will do this for you as
115  
116 $str->en('House');
117  
118 =cut
119  
120 sub set
121 {
122 9 ▶ my $self = shift;
Subroutine set: called CC=4 SCAR=13.9
Pod documented
123 9 my $params = Params::Get::get_params('text', @_);
124  
125 8 ▶ my $lang = $params->{'lang'} || $self->_get_language();
Condition100%3/3
$params->{'lang'} || $self->_get_language
l!l&&r!l&&!r
521
MC/DC100%2/2
$params->{'lang'} || $self->_get_language
$params->{'lang'} $self->_get_language
Truth table100%3/3
A || B
A= $params->{'lang'}B= $self->_get_language
ABresult
1X1
011
000
126 8 ▶ if(!defined($lang)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $lang) 1 7 8
127 1 Carp::carp(__PACKAGE__, ': usage: set(text => text, lang => $language)');
128 1 return;
129 }
130  
131 7 my $text = $params->{'text'};
132 7 ▶ if(!defined($text)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $text) 2 5 7
133 2 Carp::carp(__PACKAGE__, ': usage: set(text => text, lang => $language)');
134 2 return;
135 }
136  
137 5 $self->{'texts'}->{$lang} = $text;
138  
139 5 return $self;
140 }
141  
142 # https://www.gnu.org/software/gettext/manual/html_node/Locale-Environment-Variables.html
143 # https://www.gnu.org/software/gettext/manual/html_node/The-LANGUAGE-variable.html
144 sub _get_language
145 {
146 23 ▶ if(($ENV{'LANGUAGE'}) && ($ENV{'LANGUAGE'} =~ /^([a-z]{2})/i)) {
Subroutine _get_language: called CC=8 SCAR=21.2
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"LANGUAGE"} and $ENV{"LANGUAGE"} =~ /^([a-z]{2})/i) 2 21 23
Condition100%3/3
$ENV{"LANGUAGE"} and $ENV{"LANGUAGE"} =~ /^([a-z]{2})/i
!ll&&!rl&&r
2012
MC/DC100%2/2
$ENV{"LANGUAGE"} and $ENV{"LANGUAGE"} =~ /^([a-z]{2})/i
$ENV{"LANGUAGE"} $ENV{"LANGUAGE"} =~ /^([a-z]{2})/i
Truth table100%3/3
A and B
A= $ENV{"LANGUAGE"}B= $ENV{"LANGUAGE"} =~ /^([a-z]{2})/i
ABresult
0X0
100
111
147 2 return lc($1);
148 }
149  
150 21 foreach my $variable('LC_ALL', 'LC_MESSAGES', 'LANG') {
151 47 my $val = $ENV{$variable};
152 47 ▶ next unless(defined($val));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 28 19 47
153  
154 19 ▶ if($val =~ /^([a-z]{2})/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val =~ /^([a-z]{2})/i) 19 0 19
155 19 return lc($1);
156 }
157 }
158  
159 # if(defined($ENV{'LANG'}) && (($ENV{'LANG'} =~ /^C\./) || ($ENV{'LANG'} eq 'C'))) {
160 # return 'en';
161 # }
162 2 ▶ return 'en' if defined $ENV{'LANG'} && $ENV{'LANG'} =~ /^C(\.|$)/;
Branch: 1 branch
BranchTrueFalseTotal
if defined $ENV{"LANG"} and $ENV{"LANG"} =~ /^C(\.|$)/ 0 2 2
Condition33%1/3
defined $ENV{"LANG"} and $ENV{"LANG"} =~ /^C(\.|$)/
!ll&&!rl&&r
200
MC/DC0%0/2
defined $ENV{"LANG"} and $ENV{"LANG"} =~ /^C(\.|$)/
defined $ENV{"LANG"} $ENV{"LANG"} =~ /^C(\.|$)/
Truth table33%1/3
A and B
A= defined $ENV{"LANG"}B= $ENV{"LANG"} =~ /^C(\.|$)/
ABresult
0X0
100
111
163 2 return; # undef
164 }
165  
166 =head2 as_string
167  
168 Returns the text in the language requested in the parameter.
169 If that parameter is not given, the system language is used.
170  
171 my $text = Lingua::Text->new(en => 'boat', fr => 'bateau');
172 print $text->as_string(), "\n";
173 print $text->as_string('fr'), "\n";
174 print $text->as_string({ lang => 'en' }), "\n";
175  
176 =cut
177  
178 sub as_string {
179 24 ▶ my $self = shift;
Subroutine as_string: called CC=6 SCAR=17.9
Pod documented
180 24 my %params;
181  
182 24 ▶ if(ref($_[0]) eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $_[0] eq 'HASH') { } 3 21 24
elsif (scalar(@_) % 2 == 0) { } 19 2 21
183 3 %params = %{$_[0]};
184 } elsif((scalar(@_) % 2) == 0) {
185 19 ▶ if(defined($_[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0]) 1 18 19
186 1 %params = @_;
187 }
188 } else {
189 2 $params{'lang'} = shift;
190 }
191  
192 24 ▶ if(my $lang = ($params{'lang'} || $self->_get_language())) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $lang = $params{"lang"} || $self->_get_language) 23 1 24
Condition100%3/3
$params{'lang'} || $self->_get_language
l!l&&r!l&&!r
6171
MC/DC100%2/2
$params{'lang'} || $self->_get_language
$params{'lang'} $self->_get_language
Truth table100%3/3
A || B
A= $params{'lang'}B= $self->_get_language
ABresult
1X1
011
000
193 23 return $self->{'texts'}->{$lang};
194 }
195 1 Carp::carp(__PACKAGE__, ': usage: as_string(lang => $language)');
196 }
197  
198 =head2 encode
199  
200 =encoding utf-8
201  
202 Turns the encapsulated texts into HTML entities
203  
204 my $text = Lingua::Text->new(en => 'study', fr => 'étude')->encode();
205 print $text->fr(), "\n"; # Prints étude
206  
207 =cut
208  
209 sub encode {
210 3 ▶ my $self = shift;
Subroutine encode: called CC=3 SCAR=11.0
Pod documented
211  
212 3 ▶ while(my($k, $v) = each(%{$self->{'texts'}})) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{"texts"};}) 6 3 9
213 6 ▶ utf8::decode($v) unless utf8::is_utf8($v); # Only decode if not already UTF-8
Branch: 1 branch
BranchTrueFalseTotal
unless utf8::is_utf8($v) 5 1 6
214 6 $self->{'texts'}->{$k} = HTML::Entities::encode_entities($v);
215 }
216 3 return $self;
217 }
218  
219 sub AUTOLOAD
220 {
221 31 ▶ our $AUTOLOAD;
Subroutine AUTOLOAD: called CC=5 SCAR=16.2
222 31 ▶ my $self = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or return 0 31 31
223  
224 # Extract the key name from the AUTOLOAD variable
225 31 my ($key) = $AUTOLOAD =~ /::(\w+)$/;
226  
227 # Skip if called on destruction
228 31 ▶ return if $key eq 'DESTROY';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "DESTROY" 15 16 31
229  
230 # Ensure the key is called on the correct package object
231 16 ▶ return unless ref($self) eq __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self eq __PACKAGE__ 0 16 16
232  
233 16 ▶ if(my $value = shift) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $value = shift()) 5 11 16
234 # Set the requested language ($key) to the given text ($value)
235 5 $self->{'texts'}->{$key} = $value;
236 }
237  
238 # Get the requested language ($key)
239 16 return $self->{'texts'}->{$key};
240 }
241  
242 =head1 AUTHOR
243  
244 Nigel Horne, C<< <njh at nigelhorne.com> >>
245  
246 =head1 BUGS
247  
248 There's no decode() (yet) so you'll have to be extra careful to avoid
249 double encoding.
250  
251 =head1 SEE ALSO
252  
253 =head1 SUPPORT
254  
255 This module is provided as-is without any warranty.
256  
257 You can find documentation for this module with the perldoc command.
258  
259 perldoc Lingua::Text
260  
261 You can also look for information at:
262  
263 =over 4
264  
265 =item * MetaCPAN
266  
267 L<https://metacpan.org/release/Lingua-Text>
268  
269 =item * RT: CPAN's request tracker
270  
271 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Lingua-Text>
272  
273 =item * CPANTS
274  
275 L<http://cpants.cpanauthors.org/dist/Lingua-Text>
276  
277 =item * CPAN Testers' Matrix
278  
279 L<http://matrix.cpantesters.org/?dist=Lingua-Text>
280  
281 =item * CPAN Testers Dependencies
282  
283 L<http://deps.cpantesters.org/?module=Lingua-Text>
284  
285 =back
286  
287 =head1 LICENCE AND COPYRIGHT
288  
289 Copyright 2021-2025 Nigel Horne.
290  
291 This program is released under the following licence: GPL2 for personal use on
292 a single computer.
293 All other users (for example Commercial, Charity, Educational, Government)
294 must apply in writing for a licence for use from Nigel Horne at `<njh at nigelhorne.com>`.
295  
296 =cut
297  
298 5 1;
↑ Summary