add stack backtrace to fatal problems in virtual field check
[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 ); # wtf?  won't import...
8 use FS::Record;
9 use FS::Conf;
10 use FS::msgcat;
11
12 @ISA = qw(Exporter);
13 @EXPORT_OK = qw( gettext geterror );
14
15 $FS::UID::callback{'Msgcat'} = sub {
16   $conf = new FS::Conf;
17   $locale = $conf->config('locale') || 'en_US';
18   $debug = $conf->exists('show-msgcat-codes')
19 };
20
21 =head1 NAME
22
23 FS::Msgcat - Message catalog functions
24
25 =head1 SYNOPSIS
26
27   use FS::Msgcat qw(gettext geterror);
28
29   #simple interface for retreiving messages...
30   $message = gettext('msgcode');
31   #or errors (includes the error code)
32   $message = geterror('msgcode');
33
34 =head1 DESCRIPTION
35
36 FS::Msgcat provides functions to use the message catalog.  If you want to
37 maintain the message catalog database, see L<FS::msgcat> instead.
38
39 =head1 SUBROUTINES
40
41 =over 4
42
43 =item gettext MSGCODE
44
45 Returns the full message for the supplied message code.
46
47 =cut
48
49 sub gettext {
50   $debug ? geterror(@_) : _gettext(@_);
51 }
52
53 sub _gettext {
54   my $msgcode = shift;
55   my $msgcat = FS::Record::qsearchs('msgcat', {
56     'msgcode' => $msgcode,
57     'locale' => $locale
58   } );
59   if ( $msgcat ) {
60     $msgcat->msg;
61   } else {
62     warn "WARNING: message for msgcode $msgcode in locale $locale not found";
63     $msgcode;
64   }
65
66 }
67
68 =item geterror MSGCODE
69
70 Returns the full message for the supplied message code, including the message
71 code.
72
73 =cut
74
75 sub geterror {
76   my $msgcode = shift;
77   my $msg = _gettext($msgcode);
78   if ( $msg eq $msgcode ) {
79     "Error code $msgcode (message for locale $locale not found)";
80   } else {
81     "$msg (error code $msgcode)";
82   }
83 }
84
85 =back
86
87 =head1 BUGS
88
89 i18n/l10n, eek
90
91 =head1 SEE ALSO
92
93 L<FS::msgcat>, L<FS::Record>, schema.html from the base documentation.
94
95 =cut
96
97 1;
98