line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Lingua::String; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
250747
|
use strict; |
|
2
|
|
|
|
|
14
|
|
|
2
|
|
|
|
|
57
|
|
4
|
2
|
|
|
2
|
|
11
|
use warnings; |
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
48
|
|
5
|
2
|
|
|
2
|
|
13
|
use Carp; |
|
2
|
|
|
|
|
13
|
|
|
2
|
|
|
|
|
201
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
=head1 NAME |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
Lingua::String - Class to contain a string in many different languages |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
=head1 VERSION |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
Version 0.01 |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
=cut |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
our $VERSION = '0.01'; |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
use overload ( |
20
|
|
|
|
|
|
|
# '==' => \&equal, |
21
|
|
|
|
|
|
|
# '!=' => \¬_equal, |
22
|
|
|
|
|
|
|
'""' => \&as_string, |
23
|
0
|
|
|
0
|
|
0
|
bool => sub { 1 }, |
24
|
2
|
|
|
|
|
28
|
fallback => 1 # So that boolean tests don't cause as_string to be called |
25
|
2
|
|
|
2
|
|
15
|
); |
|
2
|
|
|
|
|
3
|
|
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
=head1 SYNOPSIS |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
Hold many strings in one object. |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
use Lingua::String; |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
my $str = Lingua::String->new(); |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
$str->fr('Bonjour Tout le Monde'); |
36
|
|
|
|
|
|
|
$str->en('Hello, World'); |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
$ENV{'LANG'} = 'en_GB'; |
39
|
|
|
|
|
|
|
print "$str\n"; # Prints Hello, World |
40
|
|
|
|
|
|
|
$ENV{'LANG'} = 'fr_FR'; |
41
|
|
|
|
|
|
|
print "$str\n"; # Prints Bonjour Tout le Monde |
42
|
|
|
|
|
|
|
$LANG{'LANG'} = 'de_DE'; |
43
|
|
|
|
|
|
|
print "$str\n"; # Prints nothing |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
=cut |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
=head1 METHODS |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
=head2 new |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
Create a Lingua::String object. |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
=cut |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
sub new { |
56
|
1
|
|
|
1
|
1
|
171
|
my $proto = shift; |
57
|
1
|
|
33
|
|
|
8
|
my $class = ref($proto) || $proto; |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
# Use Lingua::String->new, not Lingua::String::new |
60
|
1
|
50
|
|
|
|
4
|
return unless($class); |
61
|
|
|
|
|
|
|
|
62
|
1
|
|
|
|
|
5
|
return bless { }, $class; |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
=head2 set |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
Sets a string in a language. |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
$str->set({ string => 'House', lang => 'en' }); |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
Autoload will do this for you as |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
$str->en('House'); |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
=cut |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
sub set { |
78
|
1
|
|
|
1
|
1
|
4
|
my $self = shift; |
79
|
|
|
|
|
|
|
|
80
|
1
|
|
|
|
|
2
|
my %params; |
81
|
1
|
50
|
|
|
|
13
|
if(ref($_[0]) eq 'HASH') { |
|
|
50
|
|
|
|
|
|
82
|
0
|
|
|
|
|
0
|
%params = %{$_[0]}; |
|
0
|
|
|
|
|
0
|
|
83
|
|
|
|
|
|
|
} elsif(scalar(@_) % 2 == 0) { |
84
|
1
|
|
|
|
|
5
|
%params = @_; |
85
|
|
|
|
|
|
|
} else { |
86
|
0
|
|
|
|
|
0
|
$params{'string'} = shift; |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
|
89
|
1
|
|
|
|
|
3
|
my $lang = $params{'lang'}; |
90
|
|
|
|
|
|
|
|
91
|
1
|
50
|
|
|
|
4
|
if(!defined($lang)) { |
92
|
0
|
|
0
|
|
|
0
|
$lang ||= $self->_get_language(); |
93
|
0
|
0
|
|
|
|
0
|
if(!defined($lang)) { |
94
|
0
|
|
|
|
|
0
|
Carp::croak(__PACKAGE__, ': usage: set(string => string, lang => $language)'); |
95
|
0
|
|
|
|
|
0
|
return; |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
|
99
|
1
|
|
|
|
|
2
|
my $string = $params{'string'}; |
100
|
|
|
|
|
|
|
|
101
|
1
|
50
|
|
|
|
4
|
if(!defined($string)) { |
102
|
0
|
|
|
|
|
0
|
Carp::croak(__PACKAGE__, ': usage: set(string => string, lang => $language)'); |
103
|
0
|
|
|
|
|
0
|
return; |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
|
106
|
1
|
|
|
|
|
3
|
$self->{$lang} = $string; |
107
|
|
|
|
|
|
|
|
108
|
1
|
|
|
|
|
3
|
return $self; |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
# https://www.gnu.org/software/gettext/manual/html_node/Locale-Environment-Variables.html |
112
|
|
|
|
|
|
|
# https://www.gnu.org/software/gettext/manual/html_node/The-LANGUAGE-variable.html |
113
|
|
|
|
|
|
|
sub _get_language { |
114
|
5
|
50
|
|
5
|
|
11
|
if($ENV{'LANGUAGE'}) { |
115
|
0
|
0
|
|
|
|
0
|
if($ENV{'LANGUAGE'} =~ /^([a-z]{2})/i) { |
116
|
0
|
|
|
|
|
0
|
return lc($1); |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
} |
119
|
5
|
|
|
|
|
11
|
foreach my $variable('LC_ALL', 'LC_MESSAGES', 'LANG') { |
120
|
10
|
|
|
|
|
67
|
my $val = $ENV{$variable}; |
121
|
10
|
100
|
|
|
|
23
|
next unless(defined($val)); |
122
|
|
|
|
|
|
|
|
123
|
5
|
50
|
|
|
|
25
|
if($val =~ /^([a-z]{2})/i) { |
124
|
5
|
|
|
|
|
27
|
return lc($1); |
125
|
|
|
|
|
|
|
} |
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
=head2 as_string |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
Returns the string in the language requested in the parameter. |
132
|
|
|
|
|
|
|
If that parameter is not given, the system language is used. |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
print $string->as_string(), "\n"; |
135
|
|
|
|
|
|
|
print $string->as_string('fr'), "\n"; |
136
|
|
|
|
|
|
|
print $string->as_string({ lang => 'en' }), "\n"; |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
=cut |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
sub as_string { |
141
|
8
|
|
|
8
|
1
|
221
|
my $self = shift; |
142
|
8
|
|
|
|
|
14
|
my %params; |
143
|
|
|
|
|
|
|
|
144
|
8
|
100
|
|
|
|
31
|
if(ref($_[0]) eq 'HASH') { |
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
145
|
1
|
|
|
|
|
3
|
%params = %{$_[0]}; |
|
1
|
|
|
|
|
4
|
|
146
|
|
|
|
|
|
|
} elsif(scalar(@_) == 0) { |
147
|
|
|
|
|
|
|
# $params{'lang'} = $self->_get_language(); |
148
|
|
|
|
|
|
|
} elsif(scalar(@_) % 2 == 0) { |
149
|
2
|
100
|
|
|
|
6
|
if(defined($_[0])) { |
150
|
1
|
|
|
|
|
4
|
%params = @_; |
151
|
|
|
|
|
|
|
} |
152
|
|
|
|
|
|
|
} else { |
153
|
1
|
|
|
|
|
3
|
$params{'lang'} = shift; |
154
|
|
|
|
|
|
|
} |
155
|
8
|
|
66
|
|
|
29
|
my $lang = $params{'lang'} || $self->_get_language(); |
156
|
|
|
|
|
|
|
|
157
|
8
|
50
|
|
|
|
17
|
if(!defined($lang)) { |
158
|
0
|
|
|
|
|
0
|
Carp::croak(__PACKAGE__, ': usage: as_string(lang => $language)'); |
159
|
0
|
|
|
|
|
0
|
return; |
160
|
|
|
|
|
|
|
} |
161
|
8
|
|
|
|
|
57
|
return $self->{$lang}; |
162
|
|
|
|
|
|
|
} |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
sub AUTOLOAD { |
165
|
4
|
|
|
4
|
|
1157
|
our $AUTOLOAD; |
166
|
4
|
|
|
|
|
8
|
my $key = $AUTOLOAD; |
167
|
|
|
|
|
|
|
|
168
|
4
|
|
|
|
|
27
|
$key =~ s/.*:://; |
169
|
|
|
|
|
|
|
|
170
|
4
|
100
|
|
|
|
156
|
return if($key eq 'DESTROY'); |
171
|
|
|
|
|
|
|
|
172
|
3
|
|
|
|
|
5
|
my $self = shift; |
173
|
|
|
|
|
|
|
|
174
|
3
|
100
|
|
|
|
8
|
if(my $value = shift) { |
175
|
2
|
|
|
|
|
26
|
$self->{$key} = $value; |
176
|
|
|
|
|
|
|
} |
177
|
|
|
|
|
|
|
|
178
|
3
|
|
|
|
|
9
|
return $self->{$key}; |
179
|
|
|
|
|
|
|
} |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
=head1 AUTHOR |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
Nigel Horne, C<< >> |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
=head1 BUGS |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
=head1 SEE ALSO |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
=head1 SUPPORT |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
You can find documentation for this module with the perldoc command. |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
perldoc Lingua::String |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
You can also look for information at: |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
=over 4 |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
=item * MetaCPAN |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
L |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
=item * RT: CPAN's request tracker |
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
L |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
=item * CPANTS |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
L |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
=item * CPAN Testers' Matrix |
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
L |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
=item * CPAN Ratings |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
L |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
=item * CPAN Testers Dependencies |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
L |
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
=back |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
=head1 LICENCE AND COPYRIGHT |
226
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
Copyright 2021 Nigel Horne. |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
This program is released under the following licence: GPL2 |
230
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
=cut |
232
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
1; |