i18n: add infrastructure for translating Git with gettext
[git/jrn.git] / perl / Git / I18N.pm
blob07597dcb93a772caf7b676722c99d330ae947c47
1 package Git::I18N;
2 use 5.008;
3 use strict;
4 use warnings;
5 use Exporter 'import';
7 our @EXPORT = qw(__);
8 our @EXPORT_OK = @EXPORT;
10 sub __bootstrap_locale_messages {
11 our $TEXTDOMAIN = 'git';
12 our $TEXTDOMAINDIR = $ENV{GIT_TEXTDOMAINDIR} || '++LOCALEDIR++';
14 require POSIX;
15 POSIX->import(qw(setlocale));
16 # Non-core prerequisite module
17 require Locale::Messages;
18 Locale::Messages->import(qw(:locale_h :libintl_h));
20 setlocale(LC_MESSAGES(), '');
21 setlocale(LC_CTYPE(), '');
22 textdomain($TEXTDOMAIN);
23 bindtextdomain($TEXTDOMAIN => $TEXTDOMAINDIR);
25 return;
28 BEGIN
30 # Used by our test script to see if it should test fallbacks or
31 # not.
32 our $__HAS_LIBRARY = 1;
34 local $@;
35 eval {
36 __bootstrap_locale_messages();
37 *__ = \&Locale::Messages::gettext;
39 } or do {
40 # Tell test.pl that we couldn't load the gettext library.
41 $Git::I18N::__HAS_LIBRARY = 0;
43 # Just a fall-through no-op
44 *__ = sub ($) { $_[0] };
50 __END__
52 =head1 NAME
54 Git::I18N - Perl interface to Git's Gettext localizations
56 =head1 SYNOPSIS
58 use Git::I18N;
60 print __("Welcome to Git!\n");
62 printf __("The following error occured: %s\n"), $error;
64 =head1 DESCRIPTION
66 Git's internal Perl interface to gettext via L<Locale::Messages>. If
67 L<Locale::Messages> can't be loaded (it's not a core module) we
68 provide stub passthrough fallbacks.
70 This is a distilled interface to gettext, see C<info '(gettext)Perl'>
71 for the full interface. This module implements only a small part of
72 it.
74 =head1 FUNCTIONS
76 =head2 __($)
78 L<Locale::Messages>'s gettext function if all goes well, otherwise our
79 passthrough fallback function.
81 =head1 AUTHOR
83 E<AElig>var ArnfjE<ouml>rE<eth> Bjarmason <avarab@gmail.com>
85 =head1 COPYRIGHT
87 Copyright 2010 E<AElig>var ArnfjE<ouml>rE<eth> Bjarmason <avarab@gmail.com>
89 =cut