line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
|
2
|
|
|
|
|
|
|
########################################################################### |
3
|
|
|
|
|
|
|
# Copyright (c) Nate Wiger http://nateware.com. All Rights Reserved. |
4
|
|
|
|
|
|
|
# Please visit http://formbuilder.org for tutorials, support, and examples. |
5
|
|
|
|
|
|
|
########################################################################### |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
package CGI::FormBuilder::Messages; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
=head1 NAME |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
CGI::FormBuilder::Messages - Localized message support for FormBuilder |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
=head1 SYNOPSIS |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
use CGI::FormBuilder::Messages; |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
my $mesg = CGI::FormBuilder::Messages->new( |
18
|
|
|
|
|
|
|
$file || \%hash || ':locale' |
19
|
|
|
|
|
|
|
); |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
print $mesg->js_invalid_text; |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
=cut |
24
|
|
|
|
|
|
|
|
25
|
11
|
|
|
11
|
|
37
|
use strict; |
|
11
|
|
|
|
|
14
|
|
|
11
|
|
|
|
|
295
|
|
26
|
11
|
|
|
11
|
|
39
|
use warnings; |
|
11
|
|
|
|
|
12
|
|
|
11
|
|
|
|
|
253
|
|
27
|
11
|
|
|
11
|
|
38
|
no warnings 'uninitialized'; |
|
11
|
|
|
|
|
6
|
|
|
11
|
|
|
|
|
291
|
|
28
|
|
|
|
|
|
|
|
29
|
11
|
|
|
11
|
|
39
|
use CGI::FormBuilder::Util; |
|
11
|
|
|
|
|
11
|
|
|
11
|
|
|
|
|
8651
|
|
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
our $VERSION = '3.10'; |
32
|
|
|
|
|
|
|
our $AUTOLOAD; |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
sub new { |
35
|
135
|
|
|
135
|
0
|
148
|
my $self = shift; |
36
|
135
|
|
33
|
|
|
445
|
my $class = ref($self) || $self; |
37
|
135
|
|
|
|
|
184
|
my $src = shift; |
38
|
135
|
|
100
|
|
|
573
|
debug 1, "creating Messages object from ", $src || '(default)'; |
39
|
135
|
|
|
|
|
123
|
my %hash; |
40
|
|
|
|
|
|
|
|
41
|
135
|
100
|
|
|
|
533
|
if (my $ref = ref $src) { |
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
42
|
|
|
|
|
|
|
# hashref, get values directly |
43
|
3
|
50
|
33
|
|
|
16
|
puke "Argument to 'messages' option must be a \$file, \\\%hash, or ':locale'" |
44
|
|
|
|
|
|
|
if $ref eq 'ARRAY' || $ref eq 'SCALAR'; |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
# load defaults from English |
47
|
|
|
|
|
|
|
# anonymize the %hash or we get fucked with refs later |
48
|
3
|
|
|
|
|
16
|
require CGI::FormBuilder::Messages::default; |
49
|
3
|
|
|
|
|
18
|
%hash = CGI::FormBuilder::Messages::default->messages; |
50
|
|
|
|
|
|
|
|
51
|
3
|
|
|
|
|
25
|
while(my($k,$v) = each %$src) { |
52
|
56
|
|
|
|
|
88
|
$hash{$k} = $v; # just override individual messages |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
} elsif ($src =~ s/^:+//) { |
55
|
|
|
|
|
|
|
# A manual ":locale" specification ("auto" is handled by FB->new) |
56
|
12
|
50
|
33
|
|
|
47
|
puke "Bad FormBuilder locale specification ':$src'" unless $src && $src ne ''; |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
# load defaults from English, in case we can't find translators |
59
|
|
|
|
|
|
|
# as we add new features |
60
|
12
|
|
|
|
|
50
|
require CGI::FormBuilder::Messages::default; |
61
|
12
|
|
|
|
|
45
|
%hash = CGI::FormBuilder::Messages::default->messages; |
62
|
12
|
|
|
|
|
48
|
my %h2 = (); |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
# Note that the $src may be comma-separated, since this is the |
65
|
|
|
|
|
|
|
# way that browsers present it |
66
|
12
|
|
|
|
|
32
|
for (split /\s*,\s*/, $src) { |
67
|
12
|
|
|
|
|
34
|
debug 2, "trying to load '$_.pm' for messages"; |
68
|
12
|
|
|
|
|
16
|
my $mod = __PACKAGE__.'::'.$_; |
69
|
12
|
|
|
|
|
716
|
eval "require $mod"; |
70
|
12
|
50
|
|
|
|
35
|
if ($@) { |
71
|
|
|
|
|
|
|
# try locale's "basename" |
72
|
0
|
|
|
|
|
0
|
debug 2, "not found; trying locale basename"; |
73
|
0
|
|
|
|
|
0
|
$mod = __PACKAGE__.'::'.substr($_,0,2); |
74
|
0
|
|
|
|
|
0
|
eval "require $mod"; |
75
|
|
|
|
|
|
|
} |
76
|
12
|
50
|
|
|
|
24
|
next if $@; |
77
|
12
|
|
|
|
|
40
|
debug 2, "loading messages from $mod"; |
78
|
12
|
|
|
|
|
39
|
%h2 = CGI::FormBuilder::Messages::locale->messages; |
79
|
12
|
|
|
|
|
35
|
last; |
80
|
|
|
|
|
|
|
} |
81
|
12
|
50
|
|
|
|
25
|
belch "Could not load messages module '$src.pm': $@" unless %h2; |
82
|
12
|
|
|
|
|
29
|
while (my($k,$v) = each %h2) { |
83
|
480
|
|
|
|
|
698
|
$hash{$k} = $v; |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
} elsif ($src) { |
86
|
|
|
|
|
|
|
# filename, just *warn* on missing, and use defaults |
87
|
1
|
|
|
|
|
5
|
debug 2, "trying to open the '$src' file for messages"; |
88
|
1
|
50
|
33
|
|
|
56
|
if (-f $src && -r _ && open(M, "<$src")) { |
|
|
|
33
|
|
|
|
|
89
|
|
|
|
|
|
|
# load defaults from English |
90
|
1
|
|
|
|
|
6
|
require CGI::FormBuilder::Messages::default; |
91
|
1
|
|
|
|
|
3
|
%hash = CGI::FormBuilder::Messages::default->messages; |
92
|
|
|
|
|
|
|
|
93
|
1
|
|
|
|
|
14
|
while() { |
94
|
13
|
50
|
33
|
|
|
40
|
next if /^\s*#/ || /^\s*$/; |
95
|
13
|
|
|
|
|
11
|
chomp; |
96
|
13
|
|
|
|
|
16
|
my($k,$v) = split ' ', $_, 2; |
97
|
13
|
|
|
|
|
29
|
$hash{$k} = $v; |
98
|
|
|
|
|
|
|
} |
99
|
1
|
|
|
|
|
6
|
close M; |
100
|
|
|
|
|
|
|
} |
101
|
1
|
50
|
|
|
|
2
|
belch "Could not read messages file '$src': $!" unless %hash; |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
# Load default messages if no/invalid source given |
104
|
135
|
100
|
|
|
|
296
|
unless (%hash) { |
105
|
119
|
|
|
|
|
4889
|
require CGI::FormBuilder::Messages::default; |
106
|
119
|
|
|
|
|
444
|
%hash = CGI::FormBuilder::Messages::default->messages; |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
|
109
|
135
|
|
|
|
|
692
|
return bless \%hash, $class; |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
*messages = \&message; |
113
|
|
|
|
|
|
|
sub message { |
114
|
1093
|
|
|
1093
|
0
|
862
|
my $self = shift; |
115
|
1093
|
|
|
|
|
807
|
my $key = shift; |
116
|
1093
|
50
|
|
|
|
1468
|
unless ($key) { |
117
|
0
|
0
|
|
|
|
0
|
if (ref $self) { |
118
|
0
|
0
|
|
|
|
0
|
return wantarray ? %$self : $self; |
119
|
|
|
|
|
|
|
} else { |
120
|
|
|
|
|
|
|
# requesting a byname dump |
121
|
0
|
|
|
|
|
0
|
for my $k (sort keys %$self) { |
122
|
0
|
|
|
|
|
0
|
printf " %-20s\t%s\n", $k, $self->{$k}; |
123
|
|
|
|
|
|
|
} |
124
|
0
|
|
|
|
|
0
|
exit; |
125
|
|
|
|
|
|
|
} |
126
|
|
|
|
|
|
|
} |
127
|
1093
|
50
|
|
|
|
1434
|
$self->{$key} = shift if @_; |
128
|
1093
|
50
|
|
|
|
1787
|
unless (exists $self->{$key}) { |
129
|
0
|
|
|
|
|
0
|
my @keys = sort keys %$self; |
130
|
0
|
|
|
|
|
0
|
puke "No message string found for '$key' (keys: @keys)"; |
131
|
|
|
|
|
|
|
} |
132
|
1093
|
100
|
|
|
|
1709
|
if (ref $self->{$key} eq 'ARRAY') { |
133
|
|
|
|
|
|
|
# hack catch for external file |
134
|
2
|
|
|
|
|
3
|
$self->{$key} = "@{$self->{$key}}"; |
|
2
|
|
|
|
|
8
|
|
135
|
|
|
|
|
|
|
} |
136
|
1093
|
|
50
|
|
|
3574
|
return $self->{$key} || ''; |
137
|
|
|
|
|
|
|
} |
138
|
|
|
|
|
|
|
|
139
|
132
|
|
|
132
|
|
1135
|
sub DESTROY { 1 } |
140
|
|
|
|
|
|
|
sub AUTOLOAD { |
141
|
|
|
|
|
|
|
# This allows direct addressing by name, for subclassable usage |
142
|
1093
|
|
|
1093
|
|
2447
|
my $self = shift; |
143
|
1093
|
|
|
|
|
4089
|
my($name) = $AUTOLOAD =~ /.*::(.+)/; |
144
|
1093
|
|
|
|
|
1881
|
return $self->message($name, @_); |
145
|
|
|
|
|
|
|
} |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
1; |
148
|
|
|
|
|
|
|
__END__ |