- add message catalog table & beginning of web interface
[freeside.git] / FS / FS / msgcat.pm
1 package FS::msgcat;
2
3 use strict;
4 use vars qw( @ISA @EXPORT_OK $conf $locale );
5 use Exporter;
6 use FS::UID;
7 use FS::Record qw( qsearchs );
8 use FS::Conf;
9
10 @ISA = qw(FS::Record);
11 @EXPORT_OK = qw( gettext geterror );
12
13 $FS::UID::callback{'msgcat'} = sub {
14   $conf = new FS::Conf;
15   $locale = $conf->config('locale') || 'en_US';
16 };
17
18 =head1 NAME
19
20 FS::msgcat - Object methods for message catalog entries
21
22 =head1 SYNOPSIS
23
24   use FS::msgcat qw(gettext);
25
26   #simple interface for retreiving messages...
27   $message = gettext('msgcode');
28   #or errors (includes the error code)
29   $message = geterror('msgcode');
30
31   #maintenance stuff
32   $record = new FS::msgcat \%hash;
33   $record = new FS::msgcat { 'column' => 'value' };
34
35   $error = $record->insert;
36
37   $error = $new_record->replace($old_record);
38
39   $error = $record->delete;
40
41   $error = $record->check;
42
43 =head1 DESCRIPTION
44
45 An FS::msgcat object represents an message catalog entry.  FS::msgcat inherits 
46 from FS::Record.  The following fields are currently supported:
47
48 =over 4
49
50 =item msgnum - primary key
51
52 =item msgcode - Error code
53
54 =item locale - Locale
55
56 =item msg - Message
57
58 =back
59
60 =head1 METHODS
61
62 =over 4
63
64 =item new HASHREF
65
66 Creates a new example.  To add the example to the database, see L<"insert">.
67
68 Note that this stores the hash reference, not a distinct copy of the hash it
69 points to.  You can ask the object for a copy with the I<hash> method.
70
71 =cut
72
73 # the new method can be inherited from FS::Record, if a table method is defined
74
75 sub table { 'msgcat'; }
76
77 =item insert
78
79 Adds this record to the database.  If there is an error, returns the error,
80 otherwise returns false.
81
82 =cut
83
84 # the insert method can be inherited from FS::Record
85
86 =item delete
87
88 Delete this record from the database.
89
90 =cut
91
92 # the delete method can be inherited from FS::Record
93
94 =item replace OLD_RECORD
95
96 Replaces the OLD_RECORD with this one in the database.  If there is an error,
97 returns the error, otherwise returns false.
98
99 =cut
100
101 # the replace method can be inherited from FS::Record
102
103 =item check
104
105 Checks all fields to make sure this is a valid example.  If there is
106 an error, returns the error, otherwise returns false.  Called by the insert
107 and replace methods.
108
109 =cut
110
111 # the check method should currently be supplied - FS::Record contains some
112 # data checking routines
113
114 sub check {
115   my $self = shift;
116
117   my $error =
118     $self->ut_numbern('msgnum')
119     || $self->ut_text('msgcode')
120     || $self->ut_text('msg')
121   ;
122   return $error if $error;
123
124   $self->locale =~ /^([\w\@]+)$/ or return "illegal locale: ". $self->locale;
125   $self->locale($1);
126
127   ''; #no error
128 }
129
130 =back
131
132 =head1 SUBROUTINES
133
134 =over 4
135
136 =item gettext MSGCODE
137
138 Returns the full message for the supplied message code.
139
140 =cut
141
142 sub gettext {
143   my $msgcode = shift;
144   my $msgcat = qsearchs('msgcat', {
145     'msgcode' => $msgcode,
146     'locale' => $locale
147   } );
148   if ( $msgcat ) {
149     $msgcat->msg;
150   } else {
151     warn "WARNING: message for msgcode $msgcode in locale $locale not found";
152     $msgcode;
153   }
154
155 }
156
157 =item geterror MSGCODE
158
159 Returns the full message for the supplied message code, including the message
160 code.
161
162 =cut
163
164 sub geterror {
165   my $msgcode = shift;
166   my $msg = gettext($msgcode);
167   if ( $msg eq $msgcode ) {
168     "Error code $msgcode (message for locale $locale not found)";
169   } else {
170     "$msg (error code $msgcode)";
171   }
172 }
173
174 =back
175
176 =head1 BUGS
177
178 i18n/l10n is a mess.
179
180 =head1 SEE ALSO
181
182 L<FS::Record>, schema.html from the base documentation.
183
184 =cut
185
186 1;
187