summaryrefslogtreecommitdiff
path: root/FS/FS/Msgcat.pm
blob: e1bde8b51123f6b14dcb8e414f5ee4956a9cc932 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package FS::Msgcat;

use strict;
use vars qw( @ISA @EXPORT_OK $conf $def_locale $debug @translate_auto_insert );
use Exporter;
use FS::UID;
#use FS::Record qw( qsearchs ); # wtf?  won't import...
use FS::Record;
#use FS::Conf; #wtf?  causes dependency loops too.
use FS::msgcat;

@ISA = qw(Exporter);
@EXPORT_OK = qw( gettext geterror );

FS::UID->install_callback( sub {
  eval "use FS::Conf;";
  die $@ if $@;
  $conf = new FS::Conf;
  $def_locale = $conf->config('locale') || 'en_US';
  $debug = $conf->exists('show-msgcat-codes');
  @translate_auto_insert = $conf->config('translate-auto-insert');
});

=head1 NAME

FS::Msgcat - Message catalog functions

=head1 SYNOPSIS

  use FS::Msgcat qw(gettext geterror);

  #simple interface for retreiving messages...
  $message = gettext('msgcode');
  #or errors (includes the error code)
  $message = geterror('msgcode');

=head1 DESCRIPTION

FS::Msgcat provides functions to use the message catalog.  If you want to
maintain the message catalog database, see L<FS::msgcat> instead.

=head1 SUBROUTINES

=over 4

=item gettext MSGCODE

Returns the full message for the supplied message code.

=cut

sub gettext {
  $debug ? geterror(@_) : _gettext(@_);
}

#though i guess we don't really have to cache here since we do it in
# FS::L10N::DBI
our %cache;

sub _gettext {
  my $msgcode = shift;
  return '' unless defined($msgcode) && length($msgcode) > 0;

  my $locale =  (@_ && shift)
             || $FS::CurrentUser::CurrentUser->locale
             || $def_locale;

  return $cache{$locale}->{$msgcode} if exists $cache{$locale}->{$msgcode};

  my $msgcat = FS::Record::qsearchs('msgcat', {
    'msgcode' => $msgcode,
    'locale'  => $locale,
  } );
  if ( $msgcat ) {
    $cache{$locale}->{$msgcode} = $msgcat->msg;
  } else {
    warn "WARNING: message for msgcode $msgcode in locale $locale not found"
      unless $locale eq 'en_US';
    $cache{$locale}->{$msgcode} = $msgcode;

    if ( $locale ne 'en_US' && grep { $_ eq $locale } @translate_auto_insert ) {

        # :(
        my $newmsgcat = new FS::Record('msgcat', { locale     => $locale,
                                         msgcode    => $msgcode,
                                         msg        => $msgcode,
                                       });
        warn "WARNING: auto-inserting message for msgcode $msgcode in locale $locale";
        $newmsgcat->insert;
    }
    return $msgcode;
  }

}

=item geterror MSGCODE

Returns the full message for the supplied message code, including the message
code.

=cut

sub geterror {
  my $msgcode = shift;
  my $msg = _gettext($msgcode);
  if ( $msg eq $msgcode ) {
    my $locale = $FS::CurrentUser::CurrentUser->option('locale') || $def_locale;
    "Error code $msgcode (message for locale $locale not found)";
  } else {
    "$msg (error code $msgcode)";
  }
}

=back

=head1 BUGS

i18n/l10n, eek

=head1 SEE ALSO

L<FS::Locales>, L<FS::msgcat>

=cut

1;