qbit/GetText.pm

filter: Statementstmt 35.0% 21 / 60 Branchbran 10.0% 1 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 31.5% 6 / 19 Podpod 55.5% 5 / 9 totaltotal 33.6% 33 / 98 CC 5 SCAR 11.4
CC
5
Cov
31%
gettext
6.0
ngettext
6.0
pgettext
6.0
CRAP 3.1
« qbit/Packages.pm ↑ Summary qbit/Log.pm »
1  
2 =head1 Name
3  
4 qbit::GetText
5  
6 =head1 Description
7  
8 Functions to internatilization application.
9  
10 More information on L<GNU.org|http://www.gnu.org/software/gettext/manual/gettext.html>.
11  
12 qbit::GetText use pure perl version of gettext by default. If you need to use XS version, set envirement variable FORCE_GETTEXT_XS to TRUE;
13  
14 =cut
15  
16 package qbit::GetText;
17 8 $qbit::GetText::VERSION = '2.5';
18 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 8 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 8 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 8 ▶use Locale::Messages;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 8 ▶Locale::Messages->select_package($ENV{'FORCE_GETTEXT_XS'} ? 'gettext_xs' : 'gettext_pp');
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'FORCE_GETTEXT_XS'} ? : 0 8 8
26  
27 BEGIN {
28 8 ▶ our (@EXPORT, @EXPORT_OK);
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 8 @EXPORT = qw(
31 gettext ngettext pgettext npgettext
32 d_gettext d_ngettext d_pgettext d_npgettext
33 set_locale
34 );
35 8 @EXPORT_OK = @EXPORT;
36 }
37  
38 =head1 Functions
39  
40 =head2 gettext
41  
42 B<Arguments:>
43  
44 =over
45  
46 =item
47  
48 B<$text> - string, message;
49  
50 =item
51  
52 B<@params> - array of strings, placeholders.
53  
54 =back
55  
56 B<Return value:> string, localized message.
57  
58 =cut
59  
60 sub gettext($;@) {
61 0 ▶ my ($text, @params) = @_;
Subroutine gettext: not called CC=2 SCAR=17.9
Pod documented
62 0 utf8::encode($text);
63 0 my $msg = Locale::Messages::turn_utf_8_on(Locale::Messages::gettext($text));
64 0 ▶ return @params ? sprintf($msg, @params) : $msg;
Branch: 1 branch
BranchTrueFalseTotal
@params ? : 0 0 0
65 }
66  
67 =head2 ngettext
68  
69 B<Arguments:>
70  
71 =over
72  
73 =item
74  
75 B<$text> - string, message;
76  
77 =item
78  
79 B<$plural> - string, plural form of message;
80  
81 =item
82  
83 B<$n> - number, quantity of something;
84  
85 =item
86  
87 B<@params> - array of strings, placeholders.
88  
89 =back
90  
91 B<Return value:> string, localized message.
92  
93 =cut
94  
95 sub ngettext($$$;@) {
96 0 ▶ my ($text, $plural, $n, @params) = @_;
Subroutine ngettext: not called CC=2 SCAR=17.9
Pod documented
97 0 utf8::encode($text);
98 0 utf8::encode($plural);
99 0 my $msg = Locale::Messages::turn_utf_8_on(Locale::Messages::ngettext($text, $plural, $n));
100 0 ▶ return @params ? sprintf($msg, @params) : $msg;
Branch: 1 branch
BranchTrueFalseTotal
@params ? : 0 0 0
101 }
102  
103 =head2 pgettext
104  
105 B<Arguments:>
106  
107 =over
108  
109 =item
110  
111 B<$context> - string, message context;
112  
113 =item
114  
115 B<$text> - string, message;
116  
117 =item
118  
119 B<@params> - array of strings, placeholders.
120  
121 =back
122  
123 B<Return value:> string, localized message.
124  
125 =cut
126  
127 sub pgettext($$;@) {
128 0 ▶ my ($context, $text, @params) = @_;
Subroutine pgettext: not called CC=2 SCAR=17.9
Pod documented
129 0 utf8::encode($context);
130 0 utf8::encode($text);
131 0 my $msg = Locale::Messages::turn_utf_8_on(Locale::Messages::pgettext($context, $text));
132 0 ▶ return @params ? sprintf($msg, @params) : $msg;
Branch: 1 branch
BranchTrueFalseTotal
@params ? : 0 0 0
133 }
134  
135 =head2 npgettext
136  
137 B<Arguments:>
138  
139 =over
140  
141 =item
142  
143 B<$context> - string, message context;
144  
145 =item
146  
147 B<$text> - string, message;
148  
149 =item
150  
151 B<$plural> - string, plural form of message;
152  
153 =item
154  
155 B<$n> - number, quantity of something;
156  
157 =item
158  
159 B<@params> - array of strings, placeholders.
160  
161 =back
162  
163 B<Return value:> string, localized message.
164  
165 =cut
166  
167 sub npgettext($$$$;@) {
168 0 ▶ my ($context, $text, $plural, $n, @params) = @_;
Subroutine npgettext: not called CC=2 SCAR=17.9
Pod documented
169 0 utf8::encode($context);
170 0 utf8::encode($text);
171 0 utf8::encode($plural);
172 0 my $msg = Locale::Messages::turn_utf_8_on(Locale::Messages::npgettext($context, $text, $plural, $n));
173 0 ▶ return @params ? sprintf($msg, @params) : $msg;
Branch: 1 branch
BranchTrueFalseTotal
@params ? : 0 0 0
174 }
175  
176 =head2 d_*gettext
177  
178 Deffered versions of *gettext functions.
179  
180 my $s = d_ngettext('site', 'sites', 1);
181 # equivalent
182 my $s = sub {ngettext('site', 'sites', 1)};
183  
184 =cut
185  
186 sub d_gettext($;@) {
187 0 ▶ my ($text, @params) = @_;
Subroutine d_gettext: not called CC=1 SCAR=6.9
Pod undocumented
188 0 ▶ return sub {gettext($text, @params)}
Subroutine __ANON__: not called CC=1 SCAR=6.9
189 0 }
190  
191 sub d_ngettext($$$;@) {
192 0 ▶ my ($text, $plural, $n, @params) = @_;
Subroutine d_ngettext: not called CC=1 SCAR=6.9
Pod undocumented
193  
194 0 ▶ return sub {ngettext($text, $plural, $n, @params)}
Subroutine __ANON__: not called CC=1 SCAR=6.9
195 0 }
196  
197 sub d_pgettext($$;@) {
198 0 ▶ my ($context, $text, @params) = @_;
Subroutine d_pgettext: not called CC=1 SCAR=6.9
Pod undocumented
199 0 ▶ return sub {pgettext($context, $text, @params)};
Subroutine __ANON__: not called CC=1 SCAR=6.9
200 }
201  
202 sub d_npgettext($$$$;@) {
203 0 ▶ my ($context, $text, $plural, $n, @params) = @_;
Subroutine d_npgettext: not called CC=1 SCAR=6.9
Pod undocumented
204  
205 0 ▶ return sub {npgettext($context, $text, $plural, $n, @params)}
Subroutine __ANON__: not called CC=1 SCAR=6.9
206 0 }
207  
208 =head2 set_locale
209  
210 B<Arguments as hash:>
211  
212 =over
213  
214 =item
215  
216 B<lang> - string, locale (ru_RU, en_UK, ...);
217  
218 =item
219  
220 B<path> - string, path to locales;
221  
222 =item
223  
224 B<project> - string, project name.
225  
226 =back
227  
228 Path with locales must be C<$opts{'path'}/$opts{'lang'}/LC_MESSAGES/$opts{'project'}.mo>.
229  
230 =cut
231  
232 sub set_locale {
233 0 ▶ my (%opts) = @_;
Subroutine set_locale: not called CC=1 SCAR=6.9
Pod documented
234  
235 0 Locale::Messages::nl_putenv("LC_ALL=$opts{'lang'}");
236 0 Locale::Messages::nl_putenv("LANGUAGE=$opts{'lang'}");
237 0 Locale::Messages::textdomain($opts{'project'});
238 0 Locale::Messages::bindtextdomain($opts{'project'} => $opts{'path'});
239 0 Locale::Messages::bind_textdomain_codeset($opts{'project'} => "UTF-8");
240  
241 0 return 1;
242 }
243  
244 8 1;
« qbit/Packages.pm ↑ Summary qbit/Log.pm »