blib/lib/Lingua/String.pm

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