| 1 | package Lingua::String; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 4 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 4 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 4 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 4 | ▶ | use HTML::Entities; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | =head1 NAME | ||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | Lingua::String - Class to contain a string in many different languages | ||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | =head1 VERSION | ||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | Version 0.02 | ||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | =cut | ||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | 4 | our $VERSION = '0.02'; | |||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | use overload ( | ||||||||||||||||||||
| 21 | # '==' => \&equal, | ||||||||||||||||||||
| 22 | # '!=' => \¬_equal, | ||||||||||||||||||||
| 23 | '""' => \&as_string, | ||||||||||||||||||||
| 24 | 0 | ▶ | bool => sub { 1 }, | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 25 | 4 | fallback => 1 # So that boolean tests don't cause as_string to be called | |||||||||||||||||||
| 26 | 4 | ▶ | ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | =head1 SYNOPSIS | ||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | Hold many strings in one object. | ||||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | use Lingua::String; | ||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | my $str = Lingua::String->new(); | ||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | $str->fr('Bonjour Tout le Monde'); | ||||||||||||||||||||
| 37 | $str->en('Hello, World'); | ||||||||||||||||||||
| 38 | |||||||||||||||||||||
| 39 | $ENV{'LANG'} = 'en_GB'; | ||||||||||||||||||||
| 40 | print "$str\n"; # Prints Hello, World | ||||||||||||||||||||
| 41 | $ENV{'LANG'} = 'fr_FR'; | ||||||||||||||||||||
| 42 | print "$str\n"; # Prints Bonjour Tout le Monde | ||||||||||||||||||||
| 43 | $LANG{'LANG'} = 'de_DE'; | ||||||||||||||||||||
| 44 | print "$str\n"; # Prints nothing | ||||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | =cut | ||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | =head1 METHODS | ||||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | =head2 new | ||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | Create a Lingua::String object. | ||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | use Lingua::String; | ||||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | my $str = Lingua::String->new({ 'en' => 'Here', 'fr' => 'Ici' }); | ||||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | =cut | ||||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | sub new { | ||||||||||||||||||||
| 61 | 9 | ▶ | my $proto = shift; | ||||||||||||||||||
|
Subroutine
new: called CC=6 SCAR=18.1
Pod
documented
| |||||||||||||||||||||
| 62 | 9 | ▶ | my $class = ref($proto) || $proto; | ||||||||||||||||||
|
Condition66%2/3
ref $proto || $proto
MC/DC50%1/2
ref $proto || $proto
ref $proto
$proto
Truth table66%2/3
A || B
A= ref $protoB= $proto
| |||||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | # Use Lingua::String->new, not Lingua::String::new | ||||||||||||||||||||
| 65 | 9 | ▶ | if(!defined($class)) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 66 | # https://github.com/nigelhorne/Lingua-String/issues/1 | ||||||||||||||||||||
| 67 | 1 | Carp::carp(__PACKAGE__, ' use ->new() not ::new() to instantiate'); | |||||||||||||||||||
| 68 | 1 | return; | |||||||||||||||||||
| 69 | } | ||||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | 8 | my %params; | |||||||||||||||||||
| 72 | 8 | ▶ | if(ref($_[0]) eq 'HASH') { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 73 | 0 | %params = %{$_[0]}; | |||||||||||||||||||
| 74 | } elsif(scalar(@_) % 2 == 0) { | ||||||||||||||||||||
| 75 | 7 | %params = @_; | |||||||||||||||||||
| 76 | } else { | ||||||||||||||||||||
| 77 | 1 | Carp::carp(__PACKAGE__, ': usage: new(%args)'); | |||||||||||||||||||
| 78 | 1 | return; | |||||||||||||||||||
| 79 | } | ||||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | 7 | ▶ | if(scalar(%params)) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 82 | 4 | return bless { strings => \%params }, $class; | |||||||||||||||||||
| 83 | } | ||||||||||||||||||||
| 84 | 3 | return bless { }, $class; | |||||||||||||||||||
| 85 | } | ||||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | =head2 set | ||||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | Sets a string in a language. | ||||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | $str->set({ string => 'House', lang => 'en' }); | ||||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | Autoload will do this for you as | ||||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | $str->en('House'); | ||||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | =cut | ||||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | sub set { | ||||||||||||||||||||
| 100 | 3 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
set: called CC=7 SCAR=20.2
Pod
documented
| |||||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | 3 | my %params; | |||||||||||||||||||
| 103 | 3 | ▶ | if(ref($_[0]) eq 'HASH') { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 104 | 0 | %params = %{$_[0]}; | |||||||||||||||||||
| 105 | } elsif(scalar(@_) % 2 == 0) { | ||||||||||||||||||||
| 106 | 2 | %params = @_; | |||||||||||||||||||
| 107 | } else { | ||||||||||||||||||||
| 108 | 1 | $params{'string'} = shift; | |||||||||||||||||||
| 109 | } | ||||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | 3 | my $lang = $params{'lang'}; | |||||||||||||||||||
| 112 | |||||||||||||||||||||
| 113 | 3 | ▶ | if(!defined($lang)) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 114 | 2 | ▶ | $lang ||= $self->_get_language(); | ||||||||||||||||||
|
Condition66%2/3
$lang ||= $self->_get_language
MC/DC50%1/2
$lang ||= $self->_get_language
$lang
$self->_get_language
Truth table66%2/3
A ||= B
A= $langB= $self->_get_language
| |||||||||||||||||||||
| 115 | 2 | ▶ | if(!defined($lang)) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 116 | 1 | Carp::carp(__PACKAGE__, ': usage: set(string => string, lang => $language)'); | |||||||||||||||||||
| 117 | 1 | return; | |||||||||||||||||||
| 118 | } | ||||||||||||||||||||
| 119 | } | ||||||||||||||||||||
| 120 | |||||||||||||||||||||
| 121 | 2 | my $string = $params{'string'}; | |||||||||||||||||||
| 122 | |||||||||||||||||||||
| 123 | 2 | ▶ | if(!defined($string)) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 124 | 0 | Carp::carp(__PACKAGE__, ': usage: set(string => string, lang => $language)'); | |||||||||||||||||||
| 125 | 0 | return; | |||||||||||||||||||
| 126 | } | ||||||||||||||||||||
| 127 | |||||||||||||||||||||
| 128 | 2 | $self->{'strings'}->{$lang} = $string; | |||||||||||||||||||
| 129 | |||||||||||||||||||||
| 130 | 2 | return $self; | |||||||||||||||||||
| 131 | } | ||||||||||||||||||||
| 132 | |||||||||||||||||||||
| 133 | # https://www.gnu.org/software/gettext/manual/html_node/Locale-Environment-Variables.html | ||||||||||||||||||||
| 134 | # https://www.gnu.org/software/gettext/manual/html_node/The-LANGUAGE-variable.html | ||||||||||||||||||||
| 135 | sub _get_language { | ||||||||||||||||||||
| 136 | 14 | ▶ | if($ENV{'LANGUAGE'}) { | ||||||||||||||||||
|
Subroutine
_get_language: called CC=6 SCAR=18.2
Branch: 1 branch
| |||||||||||||||||||||
| 137 | 1 | ▶ | if($ENV{'LANGUAGE'} =~ /^([a-z]{2})/i) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 138 | 0 | return lc($1); | |||||||||||||||||||
| 139 | } | ||||||||||||||||||||
| 140 | } | ||||||||||||||||||||
| 141 | 14 | foreach my $variable('LC_ALL', 'LC_MESSAGES', 'LANG') { | |||||||||||||||||||
| 142 | 30 | my $val = $ENV{$variable}; | |||||||||||||||||||
| 143 | 30 | ▶ | next unless(defined($val)); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 144 | |||||||||||||||||||||
| 145 | 12 | ▶ | if($val =~ /^([a-z]{2})/i) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 146 | 12 | return lc($1); | |||||||||||||||||||
| 147 | } | ||||||||||||||||||||
| 148 | } | ||||||||||||||||||||
| 149 | 2 | return; # undef | |||||||||||||||||||
| 150 | } | ||||||||||||||||||||
| 151 | |||||||||||||||||||||
| 152 | =head2 as_string | ||||||||||||||||||||
| 153 | |||||||||||||||||||||
| 154 | Returns the string in the language requested in the parameter. | ||||||||||||||||||||
| 155 | If that parameter is not given, the system language is used. | ||||||||||||||||||||
| 156 | |||||||||||||||||||||
| 157 | my $string = Lingua::String->new(en => 'boat', fr => 'bateau'); | ||||||||||||||||||||
| 158 | print $string->as_string(), "\n"; | ||||||||||||||||||||
| 159 | print $string->as_string('fr'), "\n"; | ||||||||||||||||||||
| 160 | print $string->as_string({ lang => 'en' }), "\n"; | ||||||||||||||||||||
| 161 | |||||||||||||||||||||
| 162 | =cut | ||||||||||||||||||||
| 163 | |||||||||||||||||||||
| 164 | sub as_string { | ||||||||||||||||||||
| 165 | 15 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
as_string: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||
| 166 | 15 | my %params; | |||||||||||||||||||
| 167 | |||||||||||||||||||||
| 168 | 15 | ▶ | if(ref($_[0]) eq 'HASH') { | ||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||
| 169 | 1 | %params = %{$_[0]}; | |||||||||||||||||||
| 170 | } elsif(scalar(@_) == 0) { | ||||||||||||||||||||
| 171 | # $params{'lang'} = $self->_get_language(); | ||||||||||||||||||||
| 172 | } elsif(scalar(@_) % 2 == 0) { | ||||||||||||||||||||
| 173 | 6 | ▶ | if(defined($_[0])) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 174 | 1 | %params = @_; | |||||||||||||||||||
| 175 | } | ||||||||||||||||||||
| 176 | } else { | ||||||||||||||||||||
| 177 | 1 | $params{'lang'} = shift; | |||||||||||||||||||
| 178 | } | ||||||||||||||||||||
| 179 | 15 | ▶ | my $lang = $params{'lang'} || $self->_get_language(); | ||||||||||||||||||
|
Condition100%3/3
$params{'lang'} || $self->_get_language
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
| |||||||||||||||||||||
| 180 | |||||||||||||||||||||
| 181 | 15 | ▶ | if(!defined($lang)) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 182 | 1 | Carp::carp(__PACKAGE__, ': usage: as_string(lang => $language)'); | |||||||||||||||||||
| 183 | 1 | return; | |||||||||||||||||||
| 184 | } | ||||||||||||||||||||
| 185 | 14 | return $self->{'strings'}->{$lang}; | |||||||||||||||||||
| 186 | } | ||||||||||||||||||||
| 187 | |||||||||||||||||||||
| 188 | =head2 encode | ||||||||||||||||||||
| 189 | |||||||||||||||||||||
| 190 | =encoding utf-8 | ||||||||||||||||||||
| 191 | |||||||||||||||||||||
| 192 | Turns the encapsulated strings into HTML entities | ||||||||||||||||||||
| 193 | |||||||||||||||||||||
| 194 | my $string = Lingua::String->new(en => 'study', fr => 'étude')->encode(); | ||||||||||||||||||||
| 195 | print $string->fr(), "\n"; # Prints étude | ||||||||||||||||||||
| 196 | |||||||||||||||||||||
| 197 | =cut | ||||||||||||||||||||
| 198 | |||||||||||||||||||||
| 199 | sub encode { | ||||||||||||||||||||
| 200 | 2 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
encode: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 201 | |||||||||||||||||||||
| 202 | 2 | ▶ | while(my($k, $v) = each(%{$self->{'strings'}})) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 203 | 4 | utf8::decode($v); | |||||||||||||||||||
| 204 | 4 | $self->{'strings'}->{$k} = HTML::Entities::encode_entities($v); | |||||||||||||||||||
| 205 | } | ||||||||||||||||||||
| 206 | 2 | return $self; | |||||||||||||||||||
| 207 | } | ||||||||||||||||||||
| 208 | |||||||||||||||||||||
| 209 | sub AUTOLOAD { | ||||||||||||||||||||
| 210 | 13 | ▶ | our $AUTOLOAD; | ||||||||||||||||||
|
Subroutine
AUTOLOAD: called CC=4 SCAR=13.9
| |||||||||||||||||||||
| 211 | 13 | my $key = $AUTOLOAD; | |||||||||||||||||||
| 212 | |||||||||||||||||||||
| 213 | 13 | $key =~ s/.*:://; | |||||||||||||||||||
| 214 | |||||||||||||||||||||
| 215 | 13 | ▶ | return if($key eq 'DESTROY'); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 216 | |||||||||||||||||||||
| 217 | 6 | my $self = shift; | |||||||||||||||||||
| 218 | |||||||||||||||||||||
| 219 | 6 | ▶ | return if(ref($self) ne __PACKAGE__); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 220 | |||||||||||||||||||||
| 221 | 6 | ▶ | if(my $value = shift) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 222 | 2 | $self->{'strings'}->{$key} = $value; | |||||||||||||||||||
| 223 | } | ||||||||||||||||||||
| 224 | |||||||||||||||||||||
| 225 | 6 | return $self->{'strings'}->{$key}; | |||||||||||||||||||
| 226 | } | ||||||||||||||||||||
| 227 | |||||||||||||||||||||
| 228 | =head1 AUTHOR | ||||||||||||||||||||
| 229 | |||||||||||||||||||||
| 230 | Nigel Horne, C<< <njh at bandsman.co.uk> >> | ||||||||||||||||||||
| 231 | |||||||||||||||||||||
| 232 | =head1 BUGS | ||||||||||||||||||||
| 233 | |||||||||||||||||||||
| 234 | There's no decode() (yet) so you'll have to be extra careful to avoid | ||||||||||||||||||||
| 235 | double encoding. | ||||||||||||||||||||
| 236 | |||||||||||||||||||||
| 237 | =head1 SEE ALSO | ||||||||||||||||||||
| 238 | |||||||||||||||||||||
| 239 | =head1 SUPPORT | ||||||||||||||||||||
| 240 | |||||||||||||||||||||
| 241 | You can find documentation for this module with the perldoc command. | ||||||||||||||||||||
| 242 | |||||||||||||||||||||
| 243 | perldoc Lingua::String | ||||||||||||||||||||
| 244 | |||||||||||||||||||||
| 245 | You can also look for information at: | ||||||||||||||||||||
| 246 | |||||||||||||||||||||
| 247 | =over 4 | ||||||||||||||||||||
| 248 | |||||||||||||||||||||
| 249 | =item * MetaCPAN | ||||||||||||||||||||
| 250 | |||||||||||||||||||||
| 251 | L<https://metacpan.org/release/Lingua-String> | ||||||||||||||||||||
| 252 | |||||||||||||||||||||
| 253 | =item * RT: CPAN's request tracker | ||||||||||||||||||||
| 254 | |||||||||||||||||||||
| 255 | L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Lingua-String> | ||||||||||||||||||||
| 256 | |||||||||||||||||||||
| 257 | =item * CPANTS | ||||||||||||||||||||
| 258 | |||||||||||||||||||||
| 259 | L<http://cpants.cpanauthors.org/dist/Lingua-String> | ||||||||||||||||||||
| 260 | |||||||||||||||||||||
| 261 | =item * CPAN Testers' Matrix | ||||||||||||||||||||
| 262 | |||||||||||||||||||||
| 263 | L<http://matrix.cpantesters.org/?dist=Lingua-String> | ||||||||||||||||||||
| 264 | |||||||||||||||||||||
| 265 | =item * CPAN Ratings | ||||||||||||||||||||
| 266 | |||||||||||||||||||||
| 267 | L<http://cpanratings.perl.org/d/Lingua-String> | ||||||||||||||||||||
| 268 | |||||||||||||||||||||
| 269 | =item * CPAN Testers Dependencies | ||||||||||||||||||||
| 270 | |||||||||||||||||||||
| 271 | L<http://deps.cpantesters.org/?module=Lingua-String> | ||||||||||||||||||||
| 272 | |||||||||||||||||||||
| 273 | =back | ||||||||||||||||||||
| 274 | |||||||||||||||||||||
| 275 | =head1 LICENCE AND COPYRIGHT | ||||||||||||||||||||
| 276 | |||||||||||||||||||||
| 277 | Copyright 2021 Nigel Horne. | ||||||||||||||||||||
| 278 | |||||||||||||||||||||
| 279 | This program is released under the following licence: GPL2 for personal use on | ||||||||||||||||||||
| 280 | a single computer. | ||||||||||||||||||||
| 281 | All other users (for example Commercial, Charity, Educational, Government) | ||||||||||||||||||||
| 282 | must apply in writing for a licence for use from Nigel Horne at `<njh at nigelhorne.com>`. | ||||||||||||||||||||
| 283 | |||||||||||||||||||||
| 284 | =cut | ||||||||||||||||||||
| 285 | |||||||||||||||||||||
| 286 | 4 | 1; | |||||||||||||||||||