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