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