]> git.ipfire.org Git - thirdparty/git.git/blob - perl/Git/I18N.pm
i18n: add infrastructure for translating Git with gettext
[thirdparty/git.git] / perl / Git / I18N.pm
1 package Git::I18N;
2 use 5.008;
3 use strict;
4 use warnings;
5 use Exporter 'import';
6
7 our @EXPORT = qw(__);
8 our @EXPORT_OK = @EXPORT;
9
10 sub __bootstrap_locale_messages {
11 our $TEXTDOMAIN = 'git';
12 our $TEXTDOMAINDIR = $ENV{GIT_TEXTDOMAINDIR} || '++LOCALEDIR++';
13
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));
19
20 setlocale(LC_MESSAGES(), '');
21 setlocale(LC_CTYPE(), '');
22 textdomain($TEXTDOMAIN);
23 bindtextdomain($TEXTDOMAIN => $TEXTDOMAINDIR);
24
25 return;
26 }
27
28 BEGIN
29 {
30 # Used by our test script to see if it should test fallbacks or
31 # not.
32 our $__HAS_LIBRARY = 1;
33
34 local $@;
35 eval {
36 __bootstrap_locale_messages();
37 *__ = \&Locale::Messages::gettext;
38 1;
39 } or do {
40 # Tell test.pl that we couldn't load the gettext library.
41 $Git::I18N::__HAS_LIBRARY = 0;
42
43 # Just a fall-through no-op
44 *__ = sub ($) { $_[0] };
45 };
46 }
47
48 1;
49
50 __END__
51
52 =head1 NAME
53
54 Git::I18N - Perl interface to Git's Gettext localizations
55
56 =head1 SYNOPSIS
57
58 use Git::I18N;
59
60 print __("Welcome to Git!\n");
61
62 printf __("The following error occured: %s\n"), $error;
63
64 =head1 DESCRIPTION
65
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.
69
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.
73
74 =head1 FUNCTIONS
75
76 =head2 __($)
77
78 L<Locale::Messages>'s gettext function if all goes well, otherwise our
79 passthrough fallback function.
80
81 =head1 AUTHOR
82
83 E<AElig>var ArnfjE<ouml>rE<eth> Bjarmason <avarab@gmail.com>
84
85 =head1 COPYRIGHT
86
87 Copyright 2010 E<AElig>var ArnfjE<ouml>rE<eth> Bjarmason <avarab@gmail.com>
88
89 =cut