The second batch
[git] / perl / Git / I18N.pm
1 package Git::I18N;
2 use 5.008;
3 use strict;
4 use warnings $ENV{GIT_PERL_FATAL_WARNINGS} ? qw(FATAL all) : ();
5 BEGIN {
6         require Exporter;
7         if ($] < 5.008003) {
8                 *import = \&Exporter::import;
9         } else {
10                 # Exporter 5.57 which supports this invocation was
11                 # released with perl 5.8.3
12                 Exporter->import('import');
13         }
14 }
15
16 our @EXPORT = qw(__ __n N__);
17 our @EXPORT_OK = @EXPORT;
18
19 # See Git::LoadCPAN's NO_PERL_CPAN_FALLBACKS_STR for a description of
20 # this "'@@' [...] '@@'" pattern.
21 use constant NO_GETTEXT_STR => '@@' . 'NO_GETTEXT' . '@@';
22 use constant NO_GETTEXT => (
23         q[@@NO_GETTEXT@@] ne ''
24         and
25         q[@@NO_GETTEXT@@] ne NO_GETTEXT_STR
26 );
27
28 sub __bootstrap_locale_messages {
29         our $TEXTDOMAIN = 'git';
30         our $TEXTDOMAINDIR ||= $ENV{GIT_TEXTDOMAINDIR} || '@@LOCALEDIR@@';
31         die "NO_GETTEXT=" . NO_GETTEXT_STR if NO_GETTEXT;
32
33         require POSIX;
34         POSIX->import(qw(setlocale));
35         # Non-core prerequisite module
36         require Locale::Messages;
37         Locale::Messages->import(qw(:locale_h :libintl_h));
38
39         setlocale(LC_MESSAGES(), '');
40         setlocale(LC_CTYPE(), '');
41         textdomain($TEXTDOMAIN);
42         bindtextdomain($TEXTDOMAIN => $TEXTDOMAINDIR);
43
44         return;
45 }
46
47 BEGIN
48 {
49         # Used by our test script to see if it should test fallbacks or
50         # not.
51         our $__HAS_LIBRARY = 1;
52
53         local $@;
54         eval {
55                 __bootstrap_locale_messages();
56                 *__ = \&Locale::Messages::gettext;
57                 *__n = \&Locale::Messages::ngettext;
58                 1;
59         } or do {
60                 # Tell test.pl that we couldn't load the gettext library.
61                 $Git::I18N::__HAS_LIBRARY = 0;
62
63                 # Just a fall-through no-op
64                 *__ = sub ($) { $_[0] };
65                 *__n = sub ($$$) { $_[2] == 1 ? $_[0] : $_[1] };
66         };
67
68         sub N__($) { return shift; }
69 }
70
71 1;
72
73 __END__
74
75 =head1 NAME
76
77 Git::I18N - Perl interface to Git's Gettext localizations
78
79 =head1 SYNOPSIS
80
81         use Git::I18N;
82
83         print __("Welcome to Git!\n");
84
85         printf __("The following error occurred: %s\n"), $error;
86
87         printf __n("committed %d file\n", "committed %d files\n", $files), $files;
88
89
90 =head1 DESCRIPTION
91
92 Git's internal Perl interface to gettext via L<Locale::Messages>. If
93 L<Locale::Messages> can't be loaded (it's not a core module) we
94 provide stub passthrough fallbacks.
95
96 This is a distilled interface to gettext, see C<info '(gettext)Perl'>
97 for the full interface. This module implements only a small part of
98 it.
99
100 =head1 FUNCTIONS
101
102 =head2 __($)
103
104 L<Locale::Messages>'s gettext function if all goes well, otherwise our
105 passthrough fallback function.
106
107 =head2 __n($$$)
108
109 L<Locale::Messages>'s ngettext function or passthrough fallback function.
110
111 =head2 N__($)
112
113 No-operation that only returns its argument. Use this if you want xgettext to
114 extract the text to the pot template but do not want to trigger retrival of the
115 translation at run time.
116
117 =head1 AUTHOR
118
119 E<AElig>var ArnfjE<ouml>rE<eth> Bjarmason <avarab@gmail.com>
120
121 =head1 COPYRIGHT
122
123 Copyright 2010 E<AElig>var ArnfjE<ouml>rE<eth> Bjarmason <avarab@gmail.com>
124
125 =cut