Dpkg/Gettext.pm

filter: Statementstmt 52.0% 26 / 50 Branchbran 20.0% 2 / 10 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 53.3% 8 / 15 Podpod 100% 1 / 1 totaltotal 48.1% 38 / 79 CC 7 SCAR 14.2
CC
7
Cov
47%
BEGIN
8.9
__ANON__
6.0
__ANON__
6.0
CRAP 4.1
« Dpkg/BuildOptions.pm ↑ Summary Dpkg/Compression.pm »
1 # Based on Debconf::Gettext.
2 #
3 # Copyright © 2000 Joey Hess <joeyh@debian.org>
4 # Copyright © 2006 Nicolas François <nicolas.francois@centraliens.net>
5 # Copyright © 2007-2022 Guillem Jover <guillem@debian.org>
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <https://www.gnu.org/licenses/>.
19  
20 =encoding utf8
21  
22 =head1 NAME
23  
24 Dpkg::Gettext - convenience wrapper around Locale::gettext
25  
26 =head1 DESCRIPTION
27  
28 The Dpkg::Gettext module is a convenience wrapper over the L<Locale::gettext>
29 module, to guarantee we always have working gettext functions, and to add
30 some commonly used aliases.
31  
32 =cut
33  
34 package Dpkg::Gettext 2.01;
35  
36 576 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 574 our @EXPORT = qw(
39 textdomain
40 gettext
41 ngettext
42 g_
43 C_
44 P_
45 N_
46 );
47  
48 576 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
49  
50 =head1 ENVIRONMENT
51  
52 =over 4
53  
54 =item DPKG_NLS
55  
56 When set to 0, this environment variable will disable the National Language
57 Support in all Dpkg modules.
58  
59 =back
60  
61 =head1 VARIABLES
62  
63 =over 4
64  
65 =item $Dpkg::Gettext::DEFAULT_TEXT_DOMAIN
66  
67 Specifies the default text domain name to be used with the short function
68 aliases. This is intended to be used by the Dpkg modules, so that they
69 can produce localized messages even when the calling program has set the
70 current domain with textdomain(). If you would like to use the aliases
71 for your own modules, you might want to set this variable to undef, or
72 to another domain, but then the Dpkg modules will not produce localized
73 messages.
74  
75 =back
76  
77 =cut
78  
79 574 our $DEFAULT_TEXT_DOMAIN = 'dpkg-dev';
80  
81 =head1 FUNCTIONS
82  
83 =over 4
84  
85 =item $domain = textdomain($new_domain)
86  
87 Compatibility textdomain() fallback when L<Locale::gettext> is not available.
88  
89 If $new_domain is not undef, it will set the current domain to $new_domain.
90 Returns the current domain, after possibly changing it.
91  
92 =item $trans = gettext($msgid)
93  
94 Compatibility gettext() fallback when L<Locale::gettext> is not available.
95  
96 Returns $msgid.
97  
98 =item $trans = ngettext($msgid, $msgid_plural, $n)
99  
100 Compatibility ngettext() fallback when L<Locale::gettext> is not available.
101  
102 Returns $msgid if $n is 1 or $msgid_plural otherwise.
103  
104 =item $trans = g_($msgid)
105  
106 Calls dgettext() on the $msgid and returns its translation for the current
107 locale. If dgettext() is not available, returns $msgid.
108  
109 =item $trans = C_($msgctxt, $msgid)
110  
111 Calls dgettext() on the $msgid and returns its translation for the specific
112 $msgctxt supplied. If dgettext() is not available, returns $msgid.
113  
114 =item $trans = P_($msgid, $msgid_plural, $n)
115  
116 Calls dngettext(), returning the correct translation for the plural form
117 dependent on $n. If dngettext() is not available, returns $msgid if $n is 1
118 or $msgid_plural otherwise.
119  
120 =cut
121  
122 576 ▶use constant GETTEXT_CONTEXT_GLUE => "\004";
Subroutine BEGIN: called CC=1 SCAR=0.0
123  
124 576 ▶BEGIN {
Subroutine BEGIN: called CC=4 SCAR=21.8
125 576 ▶ my $use_gettext = $ENV{DPKG_NLS} // 1;
Condition50%1/2
$ENV{"DPKG_NLS"} // 1
l!l
0576
MC/DC0%0/1
$ENV{"DPKG_NLS"} // 1
$ENV{"DPKG_NLS"}
Truth table50%1/2
A
A= $ENV{"DPKG_NLS"}
Aresult
11
00
126 576 ▶ if ($use_gettext) {
Branch: 1 branch
BranchTrueFalseTotal
if ($use_gettext) 576 0 576
127 576 ▶ eval q{
Subroutine BEGIN: called CC=1 SCAR=0.0
128 use Locale::gettext;
129 };
130 576 $use_gettext = not $@;
131 }
132 576 ▶ if (not $use_gettext) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $use_gettext) { } 0 576 576
133 0 ▶ *g_ = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
134 0 return shift;
135 0 };
136 0 ▶ *textdomain = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
137 0 my $new_domain = shift;
138 0 state $domain = $DEFAULT_TEXT_DOMAIN;
139  
140 0 ▶ $domain = $new_domain if defined $new_domain;
Branch: 1 branch
BranchTrueFalseTotal
if defined $new_domain 0 0 0
141  
142 0 return $domain;
143 0 };
144 0 ▶ *gettext = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
145 0 my $msgid = shift;
146 0 return $msgid;
147 0 };
148 0 ▶ *ngettext = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
149 0 my ($msgid, $msgid_plural, $n) = @_;
150 0 ▶ if ($n == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n == 1) { } 0 0 0
151 0 return $msgid;
152 } else {
153 0 return $msgid_plural;
154 }
155 0 };
156 0 ▶ *C_ = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
157 0 my ($msgctxt, $msgid) = @_;
158 0 return $msgid;
159 0 };
160 0 ▶ *P_ = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
161 0 return ngettext(@_);
162 0 };
163 } else {
164 8985 ▶ *g_ = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
165 8985 return dgettext($DEFAULT_TEXT_DOMAIN, shift);
166 576 };
167 0 ▶ *C_ = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
168 0 my ($msgctxt, $msgid) = @_;
169 0 my $msgctxtid = $msgctxt . GETTEXT_CONTEXT_GLUE . $msgid;
170 0 my $msg = dgettext($DEFAULT_TEXT_DOMAIN, $msgctxtid);
171  
172 0 ▶ return $msg eq $msgctxtid ? $msgid : $msg;
Branch: 1 branch
BranchTrueFalseTotal
$msg eq $msgctxtid ? : 0 0 0
173 576 };
174 1 ▶ *P_ = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
175 1 return dngettext($DEFAULT_TEXT_DOMAIN, @_);
176 576 };
177 }
178 }
179  
180 =item $msgid = N_($msgid)
181  
182 A pseudo function that servers as a marker for automated extraction of
183 messages, but does not call gettext(). The run-time translation is done
184 at a different place in the code.
185  
186 =back
187  
188 =cut
189  
190 sub N_
191 120 ▶{
Subroutine N_: called CC=1 SCAR=0.0
Pod documented
192 120 my $msgid = shift;
193 120 return $msgid;
194 }
195  
196 =head1 CHANGES
197  
198 =head2 Version 2.01 (dpkg 1.21.10)
199  
200 New function: gettext().
201  
202 =head2 Version 2.00 (dpkg 1.20.0)
203  
204 Remove function: _g().
205  
206 =head2 Version 1.03 (dpkg 1.19.0)
207  
208 New envvar: Add support for new B<DPKG_NLS> environment variable.
209  
210 =head2 Version 1.02 (dpkg 1.18.3)
211  
212 New function: N_().
213  
214 =head2 Version 1.01 (dpkg 1.18.0)
215  
216 Now the short aliases (g_ and P_) will call domain aware functions with
217 $DEFAULT_TEXT_DOMAIN.
218  
219 New functions: g_(), C_().
220  
221 Deprecated function: _g().
222  
223 =head2 Version 1.00 (dpkg 1.15.6)
224  
225 Mark the module as public.
226  
227 =cut
228  
229 574 1;
« Dpkg/BuildOptions.pm ↑ Summary Dpkg/Compression.pm »