add customer fields option with agent, display_custnum, status and name, RT#73721
[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 @translate_auto_insert );
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   @translate_auto_insert = $conf->config('translate-auto-insert');
22 });
23
24 =head1 NAME
25
26 FS::Msgcat - Message catalog functions
27
28 =head1 SYNOPSIS
29
30   use FS::Msgcat qw(gettext geterror);
31
32   #simple interface for retreiving messages...
33   $message = gettext('msgcode');
34   #or errors (includes the error code)
35   $message = geterror('msgcode');
36
37 =head1 DESCRIPTION
38
39 FS::Msgcat provides functions to use the message catalog.  If you want to
40 maintain the message catalog database, see L<FS::msgcat> instead.
41
42 =head1 SUBROUTINES
43
44 =over 4
45
46 =item gettext MSGCODE
47
48 Returns the full message for the supplied message code.
49
50 =cut
51
52 sub gettext {
53   $debug ? geterror(@_) : _gettext(@_);
54 }
55
56 #though i guess we don't really have to cache here since we do it in
57 # FS::L10N::DBI
58 our %cache;
59
60 sub _gettext {
61   my $msgcode = shift;
62   return '' unless defined($msgcode) && length($msgcode) > 0;
63
64   my $locale =  (@_ && shift)
65              || $FS::CurrentUser::CurrentUser->locale
66              || $def_locale;
67
68   return $cache{$locale}->{$msgcode} if exists $cache{$locale}->{$msgcode};
69
70   my $msgcat = FS::Record::qsearchs('msgcat', {
71     'msgcode' => $msgcode,
72     'locale'  => $locale,
73   } );
74   if ( $msgcat ) {
75     $cache{$locale}->{$msgcode} = $msgcat->msg;
76   } else {
77     warn "WARNING: message for msgcode $msgcode in locale $locale not found"
78       unless $locale eq 'en_US';
79     $cache{$locale}->{$msgcode} = $msgcode;
80
81     if ( $locale ne 'en_US' && grep { $_ eq $locale } @translate_auto_insert ) {
82
83         # :(
84         my $newmsgcat = new FS::Record('msgcat', { locale     => $locale,
85                                          msgcode    => $msgcode,
86                                          msg        => $msgcode,
87                                        });
88         warn "WARNING: auto-inserting message for msgcode $msgcode in locale $locale";
89         $newmsgcat->insert;
90     }
91     return $msgcode;
92   }
93
94 }
95
96 =item geterror MSGCODE
97
98 Returns the full message for the supplied message code, including the message
99 code.
100
101 =cut
102
103 sub geterror {
104   my $msgcode = shift;
105   my $msg = _gettext($msgcode);
106   if ( $msg eq $msgcode ) {
107     my $locale = $FS::CurrentUser::CurrentUser->option('locale') || $def_locale;
108     "Error code $msgcode (message for locale $locale not found)";
109   } else {
110     "$msg (error code $msgcode)";
111   }
112 }
113
114 =back
115
116 =head1 BUGS
117
118 i18n/l10n, eek
119
120 =head1 SEE ALSO
121
122 L<FS::Locales>, L<FS::msgcat>
123
124 =cut
125
126 1;
127