i18n, RT#12515
[freeside.git] / FS / FS / Msgcat.pm
1 package FS::Msgcat;
2
3 use strict;
4 use vars qw( @ISA @EXPORT_OK $conf $def_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; #wtf?  causes dependency loops too.
10 use FS::msgcat;
11
12 @ISA = qw(Exporter);
13 @EXPORT_OK = qw( gettext geterror );
14
15 FS::UID->install_callback( sub {
16   eval "use FS::Conf;";
17   die $@ if $@;
18   $conf = new FS::Conf;
19   $def_locale = $conf->config('locale') || 'en_US';
20   $debug = $conf->exists('show-msgcat-codes')
21 });
22
23 =head1 NAME
24
25 FS::Msgcat - Message catalog functions
26
27 =head1 SYNOPSIS
28
29   use FS::Msgcat qw(gettext geterror);
30
31   #simple interface for retreiving messages...
32   $message = gettext('msgcode');
33   #or errors (includes the error code)
34   $message = geterror('msgcode');
35
36 =head1 DESCRIPTION
37
38 FS::Msgcat provides functions to use the message catalog.  If you want to
39 maintain the message catalog database, see L<FS::msgcat> instead.
40
41 =head1 SUBROUTINES
42
43 =over 4
44
45 =item gettext MSGCODE
46
47 Returns the full message for the supplied message code.
48
49 =cut
50
51 sub gettext {
52   $debug ? geterror(@_) : _gettext(@_);
53 }
54
55 #though i guess we don't really have to cache here since we do it in
56 # FS::L10N::DBI
57 our %cache;
58
59 sub _gettext {
60   my $msgcode = shift;
61   my $locale =  (@_ && shift)
62              || $FS::CurrentUser::CurrentUser->option('locale')
63              || $def_locale;
64
65   return $cache{$locale}->{$msgcode} if exists $cache{$locale}->{$msgcode};
66
67   my $msgcat = FS::Record::qsearchs('msgcat', {
68     'msgcode' => $msgcode,
69     'locale'  => $locale,
70   } );
71   if ( $msgcat ) {
72     $cache{$locale}->{$msgcode} = $msgcat->msg;
73   } else {
74     warn "WARNING: message for msgcode $msgcode in locale $locale not found"
75       unless $locale eq 'en_US';
76     $cache{$locale}->{$msgcode} = $msgcode;
77   }
78
79 }
80
81 =item geterror MSGCODE
82
83 Returns the full message for the supplied message code, including the message
84 code.
85
86 =cut
87
88 sub geterror {
89   my $msgcode = shift;
90   my $msg = _gettext($msgcode);
91   if ( $msg eq $msgcode ) {
92     my $locale = $FS::CurrentUser::CurrentUser->option('locale') || $def_locale;
93     "Error code $msgcode (message for locale $locale not found)";
94   } else {
95     "$msg (error code $msgcode)";
96   }
97 }
98
99 =back
100
101 =head1 BUGS
102
103 i18n/l10n, eek
104
105 =head1 SEE ALSO
106
107 L<FS::Locales>, L<FS::msgcat>
108
109 =cut
110
111 1;
112