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