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
|
|
70
|
use strict; |
|
11
|
|
|
|
|
22
|
|
|
11
|
|
|
|
|
461
|
|
26
|
11
|
|
|
11
|
|
61
|
use warnings; |
|
11
|
|
|
|
|
24
|
|
|
11
|
|
|
|
|
435
|
|
27
|
11
|
|
|
11
|
|
57
|
no warnings 'uninitialized'; |
|
11
|
|
|
|
|
22
|
|
|
11
|
|
|
|
|
474
|
|
28
|
|
|
|
|
|
|
|
29
|
11
|
|
|
11
|
|
63
|
use CGI::FormBuilder::Util; |
|
11
|
|
|
|
|
22
|
|
|
11
|
|
|
|
|
15612
|
|
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
our $VERSION = '3.09'; |
32
|
|
|
|
|
|
|
our $AUTOLOAD; |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
sub new { |
35
|
135
|
|
|
135
|
0
|
379
|
my $self = shift; |
36
|
135
|
|
33
|
|
|
823
|
my $class = ref($self) || $self; |
37
|
135
|
|
|
|
|
495
|
my $src = shift; |
38
|
135
|
|
100
|
|
|
955
|
debug 1, "creating Messages object from ", $src || '(default)'; |
39
|
135
|
|
|
|
|
205
|
my %hash; |
40
|
|
|
|
|
|
|
|
41
|
135
|
100
|
|
|
|
973
|
if (my $ref = ref $src) { |
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
42
|
|
|
|
|
|
|
# hashref, get values directly |
43
|
3
|
50
|
33
|
|
|
43
|
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
|
|
|
|
|
23
|
require CGI::FormBuilder::Messages::default; |
49
|
3
|
|
|
|
|
34
|
%hash = CGI::FormBuilder::Messages::default->messages; |
50
|
|
|
|
|
|
|
|
51
|
3
|
|
|
|
|
37
|
while(my($k,$v) = each %$src) { |
52
|
56
|
|
|
|
|
162
|
$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
|
|
|
74
|
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
|
|
|
|
|
78
|
require CGI::FormBuilder::Messages::default; |
61
|
12
|
|
|
|
|
121
|
%hash = CGI::FormBuilder::Messages::default->messages; |
62
|
12
|
|
|
|
|
75
|
my %h2 = (); |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
# Note that the $src may be comma-separated, since this is the |
65
|
|
|
|
|
|
|
# way that browsers present it |
66
|
12
|
|
|
|
|
59
|
for (split /\s*,\s*/, $src) { |
67
|
12
|
|
|
|
|
59
|
debug 2, "trying to load '$_.pm' for messages"; |
68
|
12
|
|
|
|
|
42
|
my $mod = __PACKAGE__.'::'.$_; |
69
|
12
|
|
|
|
|
1358
|
eval "require $mod"; |
70
|
12
|
50
|
|
|
|
71
|
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
|
|
|
|
36
|
next if $@; |
77
|
12
|
|
|
|
|
68
|
debug 2, "loading messages from $mod"; |
78
|
12
|
|
|
|
|
94
|
%h2 = CGI::FormBuilder::Messages::locale->messages; |
79
|
12
|
|
|
|
|
73
|
last; |
80
|
|
|
|
|
|
|
} |
81
|
12
|
50
|
|
|
|
43
|
belch "Could not load messages module '$src.pm': $@" unless %h2; |
82
|
12
|
|
|
|
|
53
|
while (my($k,$v) = each %h2) { |
83
|
480
|
|
|
|
|
1401
|
$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
|
|
|
86
|
if (-f $src && -r _ && open(M, "<$src")) { |
|
|
|
33
|
|
|
|
|
89
|
|
|
|
|
|
|
# load defaults from English |
90
|
1
|
|
|
|
|
6
|
require CGI::FormBuilder::Messages::default; |
91
|
1
|
|
|
|
|
6
|
%hash = CGI::FormBuilder::Messages::default->messages; |
92
|
|
|
|
|
|
|
|
93
|
1
|
|
|
|
|
19
|
while() { |
94
|
13
|
50
|
33
|
|
|
58
|
next if /^\s*#/ || /^\s*$/; |
95
|
13
|
|
|
|
|
13
|
chomp; |
96
|
13
|
|
|
|
|
25
|
my($k,$v) = split ' ', $_, 2; |
97
|
13
|
|
|
|
|
35
|
$hash{$k} = $v; |
98
|
|
|
|
|
|
|
} |
99
|
1
|
|
|
|
|
12
|
close M; |
100
|
|
|
|
|
|
|
} |
101
|
1
|
50
|
|
|
|
4
|
belch "Could not read messages file '$src': $!" unless %hash; |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
# Load default messages if no/invalid source given |
104
|
135
|
100
|
|
|
|
470
|
unless (%hash) { |
105
|
119
|
|
|
|
|
14549
|
require CGI::FormBuilder::Messages::default; |
106
|
119
|
|
|
|
|
891
|
%hash = CGI::FormBuilder::Messages::default->messages; |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
|
109
|
135
|
|
|
|
|
1597
|
return bless \%hash, $class; |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
*messages = \&message; |
113
|
|
|
|
|
|
|
sub message { |
114
|
1093
|
|
|
1093
|
0
|
1313
|
my $self = shift; |
115
|
1093
|
|
|
|
|
1306
|
my $key = shift; |
116
|
1093
|
50
|
|
|
|
2361
|
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
|
|
|
|
2161
|
$self->{$key} = shift if @_; |
128
|
1093
|
50
|
|
|
|
3052
|
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
|
|
|
|
2932
|
if (ref $self->{$key} eq 'ARRAY') { |
133
|
|
|
|
|
|
|
# hack catch for external file |
134
|
2
|
|
|
|
|
7
|
$self->{$key} = "@{$self->{$key}}"; |
|
2
|
|
|
|
|
15
|
|
135
|
|
|
|
|
|
|
} |
136
|
1093
|
|
50
|
|
|
6389
|
return $self->{$key} || ''; |
137
|
|
|
|
|
|
|
} |
138
|
|
|
|
|
|
|
|
139
|
132
|
|
|
132
|
|
4157
|
sub DESTROY { 1 } |
140
|
|
|
|
|
|
|
sub AUTOLOAD { |
141
|
|
|
|
|
|
|
# This allows direct addressing by name, for subclassable usage |
142
|
1093
|
|
|
1093
|
|
5522
|
my $self = shift; |
143
|
1093
|
|
|
|
|
5426
|
my($name) = $AUTOLOAD =~ /.*::(.+)/; |
144
|
1093
|
|
|
|
|
2936
|
return $self->message($name, @_); |
145
|
|
|
|
|
|
|
} |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
1; |
148
|
|
|
|
|
|
|
__END__ |