setup: fix leaking repository format
[git/debian.git] / perl / Git / I18N.pm
blob895e759c57a9d9c76d677299ff4658796e2930af
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');
16 our @EXPORT = qw(__ __n N__);
17 our @EXPORT_OK = @EXPORT;
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
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;
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));
39 setlocale(LC_MESSAGES(), '');
40 setlocale(LC_CTYPE(), '');
41 textdomain($TEXTDOMAIN);
42 bindtextdomain($TEXTDOMAIN => $TEXTDOMAINDIR);
44 return;
47 BEGIN
49 # Used by our test script to see if it should test fallbacks or
50 # not.
51 our $__HAS_LIBRARY = 1;
53 local $@;
54 eval {
55 __bootstrap_locale_messages();
56 *__ = \&Locale::Messages::gettext;
57 *__n = \&Locale::Messages::ngettext;
59 } or do {
60 # Tell test.pl that we couldn't load the gettext library.
61 $Git::I18N::__HAS_LIBRARY = 0;
63 # Just a fall-through no-op
64 *__ = sub ($) { $_[0] };
65 *__n = sub ($$$) { $_[2] == 1 ? $_[0] : $_[1] };
68 sub N__($) { return shift; }
73 __END__
75 =head1 NAME
77 Git::I18N - Perl interface to Git's Gettext localizations
79 =head1 SYNOPSIS
81 use Git::I18N;
83 print __("Welcome to Git!\n");
85 printf __("The following error occurred: %s\n"), $error;
87 printf __n("committed %d file\n", "committed %d files\n", $files), $files;
90 =head1 DESCRIPTION
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.
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.
100 =head1 FUNCTIONS
102 =head2 __($)
104 L<Locale::Messages>'s gettext function if all goes well, otherwise our
105 passthrough fallback function.
107 =head2 __n($$$)
109 L<Locale::Messages>'s ngettext function or passthrough fallback function.
111 =head2 N__($)
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.
117 =head1 AUTHOR
119 E<AElig>var ArnfjE<ouml>rE<eth> Bjarmason <avarab@gmail.com>
121 =head1 COPYRIGHT
123 Copyright 2010 E<AElig>var ArnfjE<ouml>rE<eth> Bjarmason <avarab@gmail.com>
125 =cut