dpkg (1.18.25) stretch; urgency=medium
[dpkg] / scripts / Dpkg / Gettext.pm
CommitLineData
1479465f
GJ
1# Copied from /usr/share/perl5/Debconf/Gettext.pm
2#
3# Copyright © 2000 Joey Hess <joeyh@debian.org>
4# Copyright © 2007, 2009-2010, 2012-2015 Guillem Jover <guillem@debian.org>
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9# 1. Redistributions of source code must retain the above copyright
10# notice, this list of conditions and the following disclaimer.
11# 2. Redistributions in binary form must reproduce the above copyright
12# notice, this list of conditions and the following disclaimer in the
13# documentation and/or other materials provided with the distribution.
14#
15# THIS SOFTWARE IS PROVIDED BY AUTHORS AND CONTRIBUTORS ``AS IS'' AND
16# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
19# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25# SUCH DAMAGE.
26
27package Dpkg::Gettext;
28
29use strict;
30use warnings;
31
32our $VERSION = '1.02';
33our @EXPORT = qw(
34 textdomain
35 ngettext
36 g_
37 P_
38 N_
39 _g
40);
41
42use Exporter qw(import);
43
44=encoding utf8
45
46=head1 NAME
47
48Dpkg::Gettext - convenience wrapper around Locale::gettext
49
50=head1 DESCRIPTION
51
52The Dpkg::Gettext module is a convenience wrapper over the Locale::gettext
53module, to guarantee we always have working gettext functions, and to add
54some commonly used aliases.
55
56=head1 VARIABLES
57
58=over 4
59
60=item $Dpkg::Gettext::DEFAULT_TEXT_DOMAIN
61
62Specifies the default text domain name to be used with the short function
63aliases. This is intended to be used by the Dpkg modules, so that they
64can produce localized messages even when the calling program has set the
65current domain with textdomain(). If you would like to use the aliases
66for your own modules, you might want to set this variable to undef, or
67to another domain, but then the Dpkg modules will not produce localized
68messages.
69
70=back
71
72=cut
73
74our $DEFAULT_TEXT_DOMAIN = 'dpkg-dev';
75
76=head1 FUNCTIONS
77
78=over 4
79
80=item $trans = g_($msgid)
81
82Calls dgettext() on the $msgid and returns its translation for the current
83locale. If dgettext() is not available, simply returns $msgid.
84
85=item $trans = C_($msgctxt, $msgid)
86
87Calls dgettext() on the $msgid and returns its translation for the specific
88$msgctxt supplied. If dgettext() is not available, simply returns $msgid.
89
90=item $trans = P_($msgid, $msgid_plural, $n)
91
92Calls dngettext(), returning the correct translation for the plural form
93dependent on $n. If dngettext() is not available, returns $msgid if $n is 1
94or $msgid_plural otherwise.
95
96=cut
97
98use constant GETTEXT_CONTEXT_GLUE => "\004";
99
100BEGIN {
101 eval q{
102 pop @INC if $INC[-1] eq '.';
103 use Locale::gettext;
104 };
105 if ($@) {
106 eval q{
107 sub g_ {
108 return shift;
109 }
110 sub textdomain {
111 }
112 sub ngettext {
113 my ($msgid, $msgid_plural, $n) = @_;
114 if ($n == 1) {
115 return $msgid;
116 } else {
117 return $msgid_plural;
118 }
119 }
120 sub C_ {
121 my ($msgctxt, $msgid) = @_;
122 return $msgid;
123 }
124 sub P_ {
125 return ngettext(@_);
126 }
127 };
128 } else {
129 eval q{
130 sub g_ {
131 return dgettext($DEFAULT_TEXT_DOMAIN, shift);
132 }
133 sub C_ {
134 my ($msgctxt, $msgid) = @_;
135 return dgettext($DEFAULT_TEXT_DOMAIN,
136 $msgctxt . GETTEXT_CONTEXT_GLUE . $msgid);
137 }
138 sub P_ {
139 return dngettext($DEFAULT_TEXT_DOMAIN, @_);
140 }
141 };
142 }
143}
144
145=item $msgid = N_($msgid)
146
147A pseudo function that servers as a marked for automated extraction of
148messages, but does not call gettext(). The run-time translation is done
149at a different place in the code.
150
151=back
152
153=cut
154
155sub N_
156{
157 my $msgid = shift;
158 return $msgid;
159}
160
161# XXX: Backwards compatibility, to be removed on VERSION 2.00.
162sub _g ## no critic (Subroutines::ProhibitUnusedPrivateSubroutines)
163{
164 my $msgid = shift;
165
166 warnings::warnif('deprecated',
167 'obsolete _g() function, please use g_() instead');
168
169 return g_($msgid);
170}
171
172=head1 CHANGES
173
174=head2 Version 1.02 (dpkg 1.18.3)
175
176New function: N_().
177
178=head2 Version 1.01 (dpkg 1.18.0)
179
180Now the short aliases (g_ and P_) will call domain aware functions with
181$DEFAULT_TEXT_DOMAIN.
182
183New functions: g_(), C_().
184
185Deprecated function: _g().
186
187=head2 Version 1.00 (dpkg 1.15.6)
188
189Mark the module as public.
190
191=cut
192
1931;