| 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 | 10 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 26 | 10 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 27 | 10 | ▶ | no warnings 'uninitialized'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||
| 29 | 10 | ▶ | use CGI::FormBuilder::Util; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||
| 31 | 10 | our $VERSION = '3.10'; | |||||||||||||||||||||||||||||||||||||||||
| 32 | 10 | our $AUTOLOAD; | |||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||
| 34 | sub new { | ||||||||||||||||||||||||||||||||||||||||||
| 35 | 123 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=24 SCAR=43.5
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 36 | 123 | ▶ | my $class = ref($self) || $self; | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
ref $self || $self
MC/DC0%0/2
ref $self || $self
ref $self
$self
Truth table33%1/3
A || B
A= ref $selfB= $self
| |||||||||||||||||||||||||||||||||||||||||||
| 37 | 123 | my $src = shift; | |||||||||||||||||||||||||||||||||||||||||
| 38 | 123 | ▶ | debug 1, "creating Messages object from ", $src || '(default)'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$src || '(default)'
MC/DC100%1/1
$src || '(default)'
$src
Truth table100%2/2
A
A= $src
| |||||||||||||||||||||||||||||||||||||||||||
| 39 | 123 | my %hash; | |||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | 123 | ▶ | if (my $ref = ref $src) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 42 | # hashref, get values directly | ||||||||||||||||||||||||||||||||||||||||||
| 43 | 2 | ▶ | puke "Argument to 'messages' option must be a \$file, \\\%hash, or ':locale'" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$ref eq "ARRAY" or $ref eq "SCALAR"
MC/DC0%0/2
$ref eq "ARRAY" or $ref eq "SCALAR"
$ref eq "ARRAY"
$ref eq "SCALAR"
Truth table33%1/3
A or B
A= $ref eq "ARRAY"B= $ref eq "SCALAR"
| |||||||||||||||||||||||||||||||||||||||||||
| 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 | 2 | require CGI::FormBuilder::Messages::default; | |||||||||||||||||||||||||||||||||||||||||
| 49 | 2 | %hash = CGI::FormBuilder::Messages::default->messages; | |||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||
| 51 | 2 | ▶ | while(my($k,$v) = each %$src) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 52 | 43 | $hash{$k} = $v; # just override individual messages | |||||||||||||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||||||||||||
| 54 | } elsif ($src =~ s/^:+//) { | ||||||||||||||||||||||||||||||||||||||||||
| 55 | # A manual ":locale" specification ("auto" is handled by FB->new) | ||||||||||||||||||||||||||||||||||||||||||
| 56 | 2 | ▶ | puke "Bad FormBuilder locale specification ':$src'" unless $src && $src ne ''; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$src and $src ne ""
MC/DC0%0/2
$src and $src ne ""
$src
$src ne ""
Truth table33%1/3
A and B
A= $srcB= $src ne ""
| |||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||
| 58 | # load defaults from English, in case we can't find translators | ||||||||||||||||||||||||||||||||||||||||||
| 59 | # as we add new features | ||||||||||||||||||||||||||||||||||||||||||
| 60 | 2 | require CGI::FormBuilder::Messages::default; | |||||||||||||||||||||||||||||||||||||||||
| 61 | 2 | %hash = CGI::FormBuilder::Messages::default->messages; | |||||||||||||||||||||||||||||||||||||||||
| 62 | 2 | my %h2 = (); | |||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||
| 64 | # Note that the $src may be comma-separated, since this is the | ||||||||||||||||||||||||||||||||||||||||||
| 65 | # way that browsers present it | ||||||||||||||||||||||||||||||||||||||||||
| 66 | 2 | for (split /\s*,\s*/, $src) { | |||||||||||||||||||||||||||||||||||||||||
| 67 | 2 | debug 2, "trying to load '$_.pm' for messages"; | |||||||||||||||||||||||||||||||||||||||||
| 68 | 2 | my $mod = __PACKAGE__.'::'.$_; | |||||||||||||||||||||||||||||||||||||||||
| 69 | 2 | eval "require $mod"; | |||||||||||||||||||||||||||||||||||||||||
| 70 | 2 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 71 | # try locale's "basename" | ||||||||||||||||||||||||||||||||||||||||||
| 72 | 0 | debug 2, "not found; trying locale basename"; | |||||||||||||||||||||||||||||||||||||||||
| 73 | 0 | $mod = __PACKAGE__.'::'.substr($_,0,2); | |||||||||||||||||||||||||||||||||||||||||
| 74 | 0 | eval "require $mod"; | |||||||||||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||||||||||||
| 76 | 2 | ▶ | next if $@; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 77 | 2 | debug 2, "loading messages from $mod"; | |||||||||||||||||||||||||||||||||||||||||
| 78 | 2 | %h2 = CGI::FormBuilder::Messages::locale->messages; | |||||||||||||||||||||||||||||||||||||||||
| 79 | 2 | last; | |||||||||||||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||||||||||||
| 81 | 2 | ▶ | belch "Could not load messages module '$src.pm': $@" unless %h2; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 82 | 2 | ▶ | while (my($k,$v) = each %h2) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 83 | 80 | $hash{$k} = $v; | |||||||||||||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||||||||||||
| 85 | } elsif ($src) { | ||||||||||||||||||||||||||||||||||||||||||
| 86 | # filename, just *warn* on missing, and use defaults | ||||||||||||||||||||||||||||||||||||||||||
| 87 | 0 | debug 2, "trying to open the '$src' file for messages"; | |||||||||||||||||||||||||||||||||||||||||
| 88 | 0 | ▶ | if (-f $src && -r _ && open(M, "<$src")) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
-f $src and -r _ and open M, "<$src"
-f $src and -r _
MC/DC0%0/3
-f $src and -r _ and open M, "<$src"
-f $src
-r _
open M, "<$src"
Truth table0%0/4
A and B and C
A= -f $srcB= -r _C= open M, "<$src"
| |||||||||||||||||||||||||||||||||||||||||||
| 89 | # load defaults from English | ||||||||||||||||||||||||||||||||||||||||||
| 90 | 0 | require CGI::FormBuilder::Messages::default; | |||||||||||||||||||||||||||||||||||||||||
| 91 | 0 | %hash = CGI::FormBuilder::Messages::default->messages; | |||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||
| 93 | 0 | ▶ | while(<M>) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 94 | 0 | ▶ | next if /^\s*#/ || /^\s*$/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
/^\s*#/ or /^\s*$/
MC/DC0%0/2
/^\s*#/ or /^\s*$/
/^\s*#/
/^\s*$/
Truth table0%0/3
A or B
A= /^\s*#/B= /^\s*$/
| |||||||||||||||||||||||||||||||||||||||||||
| 95 | 0 | chomp; | |||||||||||||||||||||||||||||||||||||||||
| 96 | 0 | my($k,$v) = split ' ', $_, 2; | |||||||||||||||||||||||||||||||||||||||||
| 97 | 0 | $hash{$k} = $v; | |||||||||||||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||||||||||||
| 99 | 0 | close M; | |||||||||||||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||||||||||||
| 101 | 0 | ▶ | belch "Could not read messages file '$src': $!" unless %hash; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||||||||||||
| 103 | # Load default messages if no/invalid source given | ||||||||||||||||||||||||||||||||||||||||||
| 104 | 123 | ▶ | unless (%hash) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 105 | 119 | require CGI::FormBuilder::Messages::default; | |||||||||||||||||||||||||||||||||||||||||
| 106 | 119 | %hash = CGI::FormBuilder::Messages::default->messages; | |||||||||||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||
| 109 | 123 | return bless \%hash, $class; | |||||||||||||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||
| 112 | 10 | *messages = \&message; | |||||||||||||||||||||||||||||||||||||||||
| 113 | sub message { | ||||||||||||||||||||||||||||||||||||||||||
| 114 | 662 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
message: called CC=9 SCAR=29.5
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 115 | 662 | my $key = shift; | |||||||||||||||||||||||||||||||||||||||||
| 116 | 662 | ▶ | unless ($key) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 117 | 0 | ▶ | if (ref $self) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 118 | 0 | ▶ | return wantarray ? %$self : $self; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 119 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 120 | # requesting a byname dump | ||||||||||||||||||||||||||||||||||||||||||
| 121 | 0 | for my $k (sort keys %$self) { | |||||||||||||||||||||||||||||||||||||||||
| 122 | 0 | printf " %-20s\t%s\n", $k, $self->{$k}; | |||||||||||||||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||||||||||||||
| 124 | 0 | exit; | |||||||||||||||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||||||||||||||||
| 127 | 662 | ▶ | $self->{$key} = shift if @_; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 128 | 662 | ▶ | unless (exists $self->{$key}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 129 | 0 | my @keys = sort keys %$self; | |||||||||||||||||||||||||||||||||||||||||
| 130 | 0 | puke "No message string found for '$key' (keys: @keys)"; | |||||||||||||||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||||||||||||||||
| 132 | 662 | ▶ | if (ref $self->{$key} eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 133 | # hack catch for external file | ||||||||||||||||||||||||||||||||||||||||||
| 134 | 1 | $self->{$key} = "@{$self->{$key}}"; | |||||||||||||||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||||||||||||||||
| 136 | 662 | ▶ | return $self->{$key} || ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{$key} || ''
MC/DC0%0/1
$self->{$key} || ''
$self->{$key}
Truth table50%1/2
A
A= $self->{$key}
| |||||||||||||||||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||
| 139 | 120 | ▶ | sub DESTROY { 1 } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 140 | sub AUTOLOAD { | ||||||||||||||||||||||||||||||||||||||||||
| 141 | # This allows direct addressing by name, for subclassable usage | ||||||||||||||||||||||||||||||||||||||||||
| 142 | 662 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
AUTOLOAD: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 143 | 662 | my($name) = $AUTOLOAD =~ /.*::(.+)/; | |||||||||||||||||||||||||||||||||||||||||
| 144 | 662 | return $self->message($name, @_); | |||||||||||||||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||||||
| 147 | 10 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 148 | __END__ | ||||||||||||||||||||||||||||||||||||||||||