File Coverage

blib/lib/Locale/Messages.pm
Criterion Covered Total %
statement 159 180 88.3
branch 10 22 45.4
condition 28 36 77.7
subroutine 48 55 87.2
pod 28 28 100.0
total 273 321 85.0


line stmt bran cond sub pod time code
1             #! /bin/false
2              
3             # vim: set autoindent shiftwidth=4 tabstop=4:
4              
5             # Copyright (C) 2002-2026 Guido Flohr <guido.flohr@cantanea.com>,
6             # all rights reserved.
7              
8             # This program is free software: you can redistribute it and/or modify
9             # it under the terms of the GNU General Public License as published by
10             # the Free Software Foundation; either version 3 of the License, or
11             # (at your option) any later version.
12              
13             # This program is distributed in the hope that it will be useful,
14             # but WITHOUT ANY WARRANTY; without even the implied warranty of
15             # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16             # GNU General Public License for more details.
17              
18             # You should have received a copy of the GNU General Public License
19             # along with this program. If not, see <http://www.gnu.org/licenses/>.
20              
21             package Locale::Messages;
22              
23 25     25   273301 use strict;
  25         62  
  25         1306  
24              
25 25     25   146 use vars qw ($package @EXPORT_OK %EXPORT_TAGS @ISA $VERSION);
  25         45  
  25         12075  
26              
27             $VERSION = '1.37';
28              
29             # Try to load the C version first.
30             $package = 'gettext_xs';
31              
32             # Do not load from current working directory.
33             local @INC = grep { $_ ne '.' } @INC;
34              
35             eval <<'EOF';
36             require Locale::gettext_xs;
37             my $version = Locale::gettext_xs::__gettext_xs_version();
38             die "Version: version mismatch ($VERSION vs. $version)" unless $version eq $VERSION;
39             EOF
40             my $no_xs = $@;
41              
42             # There are systems where setlocale() and the LC_ constants are not
43             # defined at all, see https://rt.cpan.org/Ticket/Display.html?id=98109
44             #
45             # On such systems, we always fall back to gettext_dumb.
46             if ($no_xs) {
47             eval {
48             require POSIX;
49             # void
50             POSIX::setlocale(POSIX::LC_ALL());
51             };
52             if ($@) {
53             $package = 'gettext_dumb';
54             require Locale::gettext_dumb;
55             } else {
56             $package = 'gettext_pp';
57             require Locale::gettext_pp;
58             }
59             }
60            
61             require Exporter;
62             @ISA = qw (Exporter);
63             %EXPORT_TAGS = (locale_h => [ qw (gettext
64             dgettext
65             dcgettext
66             ngettext
67             dngettext
68             dcngettext
69             pgettext
70             dpgettext
71             dcpgettext
72             npgettext
73             dnpgettext
74             dcnpgettext
75             textdomain
76             bindtextdomain
77             bind_textdomain_codeset
78             )
79             ],
80             libintl_h => [ qw (LC_CTYPE
81             LC_NUMERIC
82             LC_TIME
83             LC_COLLATE
84             LC_MONETARY
85             LC_MESSAGES
86             LC_ALL)
87             ],
88             );
89              
90             @EXPORT_OK = qw (select_package
91             turn_utf_8_on
92             turn_utf_8_off
93             gettext
94             dgettext
95             dcgettext
96             ngettext
97             dngettext
98             dcngettext
99             pgettext
100             dpgettext
101             dcpgettext
102             npgettext
103             dnpgettext
104             dcnpgettext
105             textdomain
106             bindtextdomain
107             bind_textdomain_codeset
108             bind_textdomain_filter
109             nl_putenv
110             setlocale
111             LC_CTYPE
112             LC_NUMERIC
113             LC_TIME
114             LC_COLLATE
115             LC_MONETARY
116             LC_MESSAGES
117             LC_ALL);
118              
119             BEGIN {
120 25     25   111 my ($has_encode, $has_bytes);
121            
122 25 50       124 if ($] >= 5.006) {
123 25 50       94 unless (defined $has_encode) {
124 25         1703 eval "require Encode";
125 25         482084 $has_encode = !$@;
126             }
127              
128 25 50 33     149 unless ($has_encode || defined $has_bytes) {
129 0         0 eval "use bytes";
130 0         0 $has_bytes = !$@;
131             }
132             }
133              
134             # Turn the UTF-8 flag on or off unconditionally. The prototypes
135             # allow an optional second parameter, so that you can use the
136             # functions as callbacks to bind_textdomain_filter.
137 25 50       103 if ($has_encode) {
    0          
138 25     1607 1 13131 eval <<'EOF';
  1607     0 1 5855  
  1607         4667  
  0         0  
  0         0  
139             sub turn_utf_8_on($;$)
140             {
141             Encode::_utf8_on ($_[0]);
142             return $_[0];
143             }
144              
145             sub turn_utf_8_off($;$)
146             {
147             Encode::_utf8_off ($_[0]);
148             return $_[0];
149             }
150              
151             EOF
152             } elsif ($has_bytes) {
153 0         0 eval <<'EOF';
154             sub turn_utf_8_on($;$)
155             {
156             $_[0] = pack "U0C*", unpack "C*", $_[0];
157             }
158              
159             sub turn_utf_8_off($;$)
160             {
161             use bytes;
162             $_[0] = join "", split //, $_[0];
163             }
164              
165             EOF
166             } else {
167 0         0 eval <<'EOF';
168             sub turn_utf_8_on($;$)
169             {
170             return $_[0];
171             }
172              
173             sub turn_utf_8_off($;$)
174             {
175             return $_[0];
176             }
177              
178             EOF
179             }
180             }
181              
182             # The textdomain could be undef. We avoid a warning by specifying
183             # a filter for the undefined textdomain.
184             my %filters = (undef => \&turn_utf_8_off);
185              
186             sub select_package {
187 21     21 1 9685 my ($pkg, $compatibility) = @_;
188              
189             # Compatibility quirk for a bug pre 1.17:
190 21 50 33     280 if (__PACKAGE__ eq $pkg && defined $compatibility) {
191 21         58 $pkg = $compatibility;
192             }
193              
194 21 50 33     129 if ($no_xs && 'gettext_xs' eq $pkg) {
195 0         0 $pkg = 'gettext_pp';
196             }
197              
198 21 100 66     121 if (defined $pkg && 'gettext_pp' eq $pkg) {
    50          
199             # This branch is not unnecessary. The next (elsif) branch does
200             # essentially the same but catches compilation errors.
201 20         12078 require Locale::gettext_pp;
202 20         115 $package = 'gettext_pp';
203             } elsif (defined $pkg) {
204 1         3 my $filename = "Locale::$pkg";
205 1         11 $filename =~ s{::|\'}{/};
206 1         3 $filename .= '.pm';
207 1         2 eval { require $filename };
  1         892  
208 1 50       7 $package = $pkg unless $@;
209             } else {
210 0         0 eval "require Locale::gettext_xs";
211 0 0       0 $package = 'gettext_xs' unless $@;
212             }
213              
214 21         208 return $package;
215             }
216              
217             sub bind_textdomain_filter ($;$$) {
218 1     1 1 20 my ($textdomain, $coderef, $data) = @_;
219              
220 1         5 $filters{$textdomain} = [ $coderef, $data ];
221              
222 1         7 return 1;
223             }
224              
225             sub textdomain (;$) {
226 209     209 1 343256 my $function = "Locale::${package}::textdomain";
227            
228 25     25   253 no strict 'refs';
  25         1111  
  25         2435  
229 209         920 &$function;
230             }
231              
232             sub bindtextdomain ($;$) {
233 41     41 1 162958 my $function = "Locale::${package}::bindtextdomain";
234              
235 25     25   153 no strict 'refs';
  25         48  
  25         2319  
236 41         203 &$function;
237             }
238              
239             sub bind_textdomain_codeset ($;$) {
240 2     2 1 252 my $function = "Locale::${package}::bind_textdomain_codeset";
241              
242 25     25   226 no strict 'refs';
  25         55  
  25         3205  
243 2         11 &$function;
244             }
245              
246             sub gettext ($) {
247 106     106 1 723 my $textdomain = textdomain;
248 106   100     371 $filters{$textdomain} ||= [ \&turn_utf_8_off ];
249 106         192 my $cb = $filters{$textdomain};
250              
251 106         275 my $function = "Locale::${package}::gettext";
252            
253 25     25   169 no strict 'refs';
  25         44  
  25         3786  
254 106         340 $cb->[0] (&$function, $cb->[1]);
255             }
256              
257             sub dgettext($$) {
258 12   100 12 1 226 my $cb = $filters{$_[0]} ||= [ \&turn_utf_8_off ];
259              
260 12         26 my $function = "Locale::${package}::dgettext";
261            
262 25     25   159 no strict 'refs';
  25         63  
  25         3540  
263 12         54 $cb->[0] (&$function, $cb->[1]);
264             }
265              
266             sub dcgettext($$$) {
267 12   100 12 1 67 my $cb = $filters{$_[0]} ||= [ \&turn_utf_8_off ];
268              
269 12         23 my $function = "Locale::${package}::dcgettext";
270            
271 25     25   164 no strict 'refs';
  25         44  
  25         3227  
272 12         42 $cb->[0] (&$function, $cb->[1]);
273             }
274              
275             sub ngettext($$$) {
276 83     83 1 5347 my $textdomain = textdomain;
277 83   100     345 $filters{$textdomain} ||= [ \&turn_utf_8_off ];
278 83         157 my $cb = $filters{$textdomain};
279              
280 83         156 my $function = "Locale::${package}::ngettext";
281            
282 25     25   156 no strict 'refs';
  25         46  
  25         3638  
283 83         278 $cb->[0] (&$function, $cb->[1]);
284             }
285              
286             sub dngettext($$$$) {
287 83   100 83 1 3425 my $cb = $filters{$_[0]} ||= [ \&turn_utf_8_off ];
288              
289 83         123 my $function = "Locale::${package}::dngettext";
290            
291 25     25   159 no strict 'refs';
  25         45  
  25         3226  
292 83         226 $cb->[0] (&$function, $cb->[1]);
293             }
294              
295             sub dcngettext($$$$$) {
296 83   100 83 1 316 my $cb = $filters{$_[0]} ||= [ \&turn_utf_8_off ];
297              
298 83         152 my $function = "Locale::${package}::dcngettext";
299            
300 25     25   206 no strict 'refs';
  25         44  
  25         3400  
301 83         342 $cb->[0] (&$function, $cb->[1]);
302             }
303              
304             sub pgettext($$) {
305 3     3 1 8 my $textdomain = textdomain;
306 3   50     8 $filters{$textdomain} ||= [ \&turn_utf_8_off ];
307 3         5 my $cb = $filters{$textdomain};
308              
309 3         5 my $function = "Locale::${package}::pgettext";
310            
311 25     25   278 no strict 'refs';
  25         59  
  25         3701  
312 3         8 $cb->[0] (&$function, $cb->[1]);
313             }
314              
315             sub dpgettext($$$) {
316 4   100 4 1 29 my $cb = $filters{$_[0]} ||= [ \&turn_utf_8_off ];
317              
318 4         7 my $function = "Locale::${package}::dpgettext";
319            
320 25     25   220 no strict 'refs';
  25         71  
  25         3060  
321 4         21 $cb->[0] (&$function, $cb->[1]);
322             }
323              
324             sub dcpgettext($$$$) {
325 5   100 5 1 28 my $cb = $filters{$_[0]} ||= [ \&turn_utf_8_off ];
326              
327 5         10 my $function = "Locale::${package}::dcpgettext";
328            
329 25     25   178 no strict 'refs';
  25         59  
  25         3015  
330 5         23 $cb->[0] (&$function, $cb->[1]);
331             }
332              
333             sub npgettext($$$$) {
334 91   100 91 1 4242 my $cb = $filters{$_[0]} ||= [ \&turn_utf_8_off ];
335              
336 91         141 my $function = "Locale::${package}::npgettext";
337            
338 25     25   209 no strict 'refs';
  25         55  
  25         3210  
339 91         279 $cb->[0] (&$function, $cb->[1]);
340             }
341              
342             sub dnpgettext($$$$$) {
343 91   100 91 1 7210 my $cb = $filters{$_[0]} ||= [ \&turn_utf_8_off ];
344              
345 91         229 my $function = "Locale::${package}::dnpgettext";
346            
347 25     25   161 no strict 'refs';
  25         60  
  25         3223  
348 91         417 $cb->[0] (&$function, $cb->[1]);
349             }
350              
351             sub dcnpgettext($$$$$$) {
352 91   100 91 1 366 my $cb = $filters{$_[0]} ||= [ \&turn_utf_8_off ];
353              
354 91         219 my $function = "Locale::${package}::dcnpgettext";
355            
356 25     25   185 no strict 'refs';
  25         47  
  25         2563  
357 91         384 $cb->[0] (&$function, $cb->[1]);
358             }
359              
360             sub setlocale($;$) {
361 128     128 1 694 my $function = "Locale::${package}::setlocale";
362            
363 25     25   218 no strict 'refs';
  25         79  
  25         2203  
364 128         541 &$function;
365             }
366              
367             sub nl_putenv($) {
368 530     530 1 2780297 my $function = "Locale::${package}::nl_putenv";
369            
370 25     25   151 no strict 'refs';
  25         74  
  25         1986  
371 530         1767 &$function;
372             }
373              
374             sub LC_NUMERIC {
375 0     0 1 0 my $function = "Locale::${package}::LC_NUMERIC";
376            
377 25     25   158 no strict 'refs';
  25         46  
  25         2248  
378 0         0 &$function;
379             }
380              
381             sub LC_CTYPE {
382 0     0 1 0 my $function = "Locale::${package}::LC_CTYPE";
383            
384 25     25   164 no strict 'refs';
  25         42  
  25         2140  
385 0         0 &$function;
386             }
387              
388             sub LC_TIME {
389 0     0 1 0 my $function = "Locale::${package}::LC_TIME";
390            
391 25     25   154 no strict 'refs';
  25         59  
  25         1908  
392 0         0 &$function;
393             }
394              
395             sub LC_COLLATE {
396 0     0 1 0 my $function = "Locale::${package}::LC_COLLATE";
397            
398 25     25   152 no strict 'refs';
  25         52  
  25         2415  
399 0         0 &$function;
400             }
401              
402             sub LC_MONETARY {
403 0     0 1 0 my $function = "Locale::${package}::LC_MONETARY";
404            
405 25     25   222 no strict 'refs';
  25         70  
  25         1964  
406 0         0 &$function;
407             }
408              
409             sub LC_MESSAGES {
410 191     191 1 13106 my $function = "Locale::${package}::LC_MESSAGES";
411            
412 25     25   140 no strict 'refs';
  25         49  
  25         1949  
413 191         6535 &$function;
414             }
415              
416             sub LC_ALL {
417 0     0 1   my $function = "Locale::${package}::LC_ALL";
418            
419 25     25   137 no strict 'refs';
  25         205  
  25         2344  
420 0           &$function;
421             }
422              
423             1;
424              
425             __END__
426              
427             =head1 NAME
428              
429             Locale::Messages - Gettext Like Message Retrieval
430              
431             =head1 SYNOPSIS
432              
433             use Locale::Messages qw(:locale_h :libintl_h);
434              
435             gettext $msgid;
436             dgettext $textdomain, $msgid;
437             dcgettext $textdomain, $msgid, LC_MESSAGES;
438             ngettext $msgid, $msgid_plural, $count;
439             dngettext $textdomain, $msgid, $msgid_plural, $count;
440             dcngettext $textdomain, $msgid, $msgid_plural, $count, LC_MESSAGES;
441             pgettext $msgctxt, $msgid;
442             dpgettext $textdomain, $msgctxt, $msgid;
443             dcpgettext $textdomain, $msgctxt, $msgid, LC_MESSAGES;
444             npgettext $msgctxt, $msgid, $msgid_plural, $count;
445             dnpgettext $textdomain, $msgctxt, $msgid, $msgid_plural, $count;
446             dcnpgettext $textdomain, $msgctxt, $msgid, $msgid_plural, $count, LC_MESSAGES;
447             textdomain $textdomain;
448             bindtextdomain $textdomain, $directory;
449             bind_textdomain_codeset $textdomain, $encoding;
450             bind_textdomain_filter $textdomain, \&filter, $data;
451             turn_utf_8_on ($variable);
452             turn_utf_8_off ($variable);
453             nl_putenv ('OUTPUT_CHARSET=koi8-r');
454             my $category = LC_CTYPE;
455             my $category = LC_NUMERIC;
456             my $category = LC_TIME;
457             my $category = LC_COLLATE;
458             my $category = LC_MONETARY;
459             my $category = LC_MESSAGES;
460             my $category = LC_ALL;
461              
462             =head1 DESCRIPTION
463              
464             The module B<Locale::Messages> is a wrapper around the interface to
465             message translation according to the Uniforum approach that is
466             for example used in GNU gettext and Sun's Solaris. It is intended
467             to allow Locale::Messages(3) to switch between different implementations
468             of the lower level libraries but this is not yet implemented.
469              
470             Normally you should not use this module directly, but the high
471             level interface Locale::TextDomain(3) that provides a much simpler
472             interface. This description is therefore deliberately kept
473             brief. Please refer to the GNU gettext documentation available at
474             L<http://www.gnu.org/manual/gettext/> for in-depth and background
475             information on the topic.
476              
477             The lower level module Locale::gettext_pp(3) provides the Perl
478             implementation of gettext() and related functions.
479              
480             =head1 FUNCTIONS
481              
482             The module exports by default nothing. Every function has to be
483             imported explicitely or via an export tag (L</"EXPORT TAGS">).
484              
485             =over 4
486              
487             =item B<gettext MSGID>
488              
489             Returns the translation for B<MSGID>. Example:
490              
491             print gettext "Hello World!\n";
492              
493             If no translation can be found, the unmodified B<MSGID> is returned,
494             i. e. the function can I<never> fail, and will I<never> mess up your
495             original message.
496              
497             Note for Perl 5.6 and later: The returned string will I<always> have
498             the UTF-8 flag off by default. See the documentation for function
499             bind_textdomain_filter() for a way to change this behavior.
500              
501             One common mistake is this:
502              
503             print gettext "Hello $name!";
504              
505             Perl will interpolate the variable C<$name> I<before> the function
506             will see the string. Unless the corresponding message catalog
507             contains a message "Hello Tom!", "Hello Dick!" or "Hello Harry!",
508             no translation will be found.
509              
510             Using printf() and friends has its own problems:
511              
512             print sprintf (gettext ("This is the %s %s."), $color, $thing);
513              
514             (The example is stupid because neither color nor thing will get
515             translated here ...).
516              
517             In English the adjective (the color) will precede the noun, many
518             other languages (for example French or Italian) differ here. The
519             translator of the message may therefore have a hard time to find
520             a translation that will still work and not sound stupid in the
521             target language. Many C implementations of printf() allow to
522             change the order of the arguments, and a French translator could
523             then say:
524              
525             "C'est le %2$s %1$s."
526              
527             Perl printf() implements this feature as of version 5.8 or better.
528             Consequently you can only use it, if you are sure that your software
529             will run with Perl 5.8 or a later version.
530              
531             Another disadvantage of using printf() is its cryptic syntax (maybe
532             not for you but translators of your software may have their own
533             opinion).
534              
535             See the description of the function C<__x()> in Locale::TextDomain(3)
536             for a much better way to get around this problem.
537              
538             Non-ASCII message ids ...
539              
540             You should note that the function (and all other similar functions
541             in this module) does a bytewise comparison of the B<MSGID> for the
542             lookup in the translation catalog, no matter whether obscure utf-8
543             flags are set on it, whether the string looks like utf-8, whether
544             the utf8(3pm) pragma is used, or whatever other weird method past
545             or future perl(1) versions invent for guessing character sets of
546             strings.
547              
548             Using other than us-ascii characters in Perl source code is a call
549             for trouble, a compatibility nightmare. Furthermore, GNU gettext
550             only lately introduced support for non-ascii character sets in sources,
551             and support for this feature may not be available everywhere. If
552             you absolutely want to use B<MSGID>s in non-ascii character sets,
553             it is wise to choose utf-8. This will minimize the risk that perl(1)
554             itself will mess with the strings, and it will also be a guaranty
555             that you can later translate your project into arbitrary target
556             languages.
557              
558             Other character sets can theoretically work. Yet, using another
559             character set in the Perl source code than the one used in your
560             message catalogs will B<never> work, since the lookup is done bytewise,
561             and all strings with non-ascii characters will not be found.
562              
563             Even if you have solved all these problems, there is still one show
564             stopper left: The gettext runtime API lacks a possibility to specify
565             the character set of the source code (including the original strings).
566             Consequently - in absence of a hint for the input encoding - strings
567             without a translation are not subject to output character set conversion.
568             In other words: If the (non-determinable) output character set differs
569             from the character set used in the source code, output can be a
570             mixture of two character sets. There is no point in trying to address
571             this problem in the pure Perl version of the gettext functions. because
572             breaking compatibilty between the Perl and the C version is a price too
573             high to pay.
574              
575             This all boils down to: Only use ASCII characters in your translatable
576             strings!
577              
578             =item B<dgettext TEXTDOMAIN, MSGID>
579              
580             Like gettext(), but retrieves the message for the specified
581             B<TEXTDOMAIN> instead of the default domain. In case you wonder what
582             a textdomain is, you should really read on with Locale::TextDomain(3).
583              
584             =item B<dcgettext TEXTDOMAIN, MSGID, CATEGORY>
585              
586             Like dgettext() but retrieves the message from the specified B<CATEGORY>
587             instead of the default category C<LC_MESSAGES>.
588              
589             =item B<ngettext MSGID, MSGID_PLURAL, COUNT>
590              
591             Retrieves the correct translation for B<COUNT> items. In legacy software
592             you will often find something like:
593              
594             print "$count file(s) deleted.\n";
595              
596             or
597              
598             printf "$count file%s deleted.\n", $count == 1 ? '' : 's';
599              
600             The first example looks awkward, the second will only work in English
601             and languages with similar plural rules. Before ngettext() was introduced,
602             the best practice for internationalized programs was:
603              
604             if ($count == 1) {
605             print gettext "One file deleted.\n";
606             } else {
607             printf gettext "%d files deleted.\n";
608             }
609              
610             This is a nuisance for the programmer and often still not sufficient
611             for an adequate translation. Many languages have completely different
612             ideas on numerals. Some (French, Italian, ...) treat 0 and 1 alike,
613             others make no distinction at all (Japanese, Korean, Chinese, ...),
614             others have two or more plural forms (Russian, Latvian, Czech,
615             Polish, ...). The solution is:
616              
617             printf (ngettext ("One file deleted.\n",
618             "%d files deleted.\n",
619             $count), # argument to ngettext!
620             $count); # argument to printf!
621              
622             In English, or if no translation can be found, the first argument
623             (B<MSGID>) is picked if C<$count> is one, the second one otherwise.
624             For other languages, the correct plural form (of 1, 2, 3, 4, ...)
625             is automatically picked, too. You don't have to know anything about
626             the plural rules in the target language, ngettext() will take care
627             of that.
628              
629             This is most of the time sufficient but you will have to prove your
630             creativity in cases like
631              
632             printf "%d file(s) deleted, and %d file(s) created.\n";
633              
634             =item B<dngettext TEXTDOMAIN, MSGID, MSGID_PLURAL, COUNT>
635              
636             Like ngettext() but retrieves the translation from the specified
637             textdomain instead of the default domain.
638              
639             =item B<dcngettext TEXTDOMAIN, MSGID, MSGID_PLURAL, COUNT, CATEGORY>
640              
641             Like dngettext() but retrieves the translation from the specified
642             category, instead of the default category C<LC_MESSAGES>.
643              
644             =item B<pgettext MSGCTXT, MSGID>
645              
646             Returns the translation of MSGID, given the context of MSGCTXT.
647              
648             Both items are used as a unique key into the message catalog.
649              
650             This allows the translator to have two entries for words that may
651             translate to different foreign words based on their context. For
652             example, the word "View" may be a noun or a verb, which may be
653             used in a menu as File->View or View->Source.
654              
655             pgettext "Verb: To View", "View\n";
656             pgettext "Noun: A View", "View\n";
657              
658             The above will both lookup different entries in the message catalog.
659              
660             A typical usage are GUI programs. Imagine a program with a main
661             menu and the notorious "Open" entry in the "File" menu. Now imagine,
662             there is another menu entry Preferences->Advanced->Policy where you have
663             a choice between the alternatives "Open" and "Closed". In English, "Open"
664             is the adequate text at both places. In other languages, it is very
665             likely that you need two different translations. Therefore, you would
666             now write:
667              
668             pgettext "File|", "Open";
669             pgettext "Preferences|Advanced|Policy", "Open";
670              
671             In English, or if no translation can be found, the second argument
672             (MSGID) is returned.
673              
674             The function was introduced with libintl-perl version 1.17.
675              
676             =item B<dpgettext TEXTDOMAIN, MSGCTXT, MSGID>
677              
678             Like pgettext(), but retrieves the message for the specified
679             B<TEXTDOMAIN> instead of the default domain.
680              
681             The function was introduced with libintl-perl version 1.17.
682              
683             =item B<dcpgettext TEXTDOMAIN, MSGCTXT, MSGID, CATEGORY>
684              
685             Like dpgettext() but retrieves the message from the specified B<CATEGORY>
686             instead of the default category C<LC_MESSAGES>.
687              
688             The function was introduced with libintl-perl version 1.17.
689              
690             =item B<npgettext MSGCTXT, MSGID, MSGID_PLURAL, COUNT>
691              
692             Like ngettext() with the addition of context as in pgettext().
693              
694             In English, or if no translation can be found, the second argument
695             (MSGID) is picked if $count is one, the third one otherwise.
696              
697             The function was introduced with libintl-perl version 1.17.
698              
699             =item B<dnpgettext TEXTDOMAIN, MSGCTXT, MSGID, MSGID_PLURAL, COUNT>
700              
701             Like npgettext() but retrieves the translation from the specified
702             textdomain instead of the default domain.
703              
704             The function was introduced with libintl-perl version 1.17.
705              
706             =item B<dcnpgettext TEXTDOMAIN, MSGCTXT, MSGID, MSGID_PLURAL, COUNT, CATEGORY>
707              
708             Like dnpgettext() but retrieves the translation from the specified
709             category, instead of the default category C<LC_MESSAGES>.
710              
711             The function was introduced with libintl-perl version 1.17.
712              
713             =item B<textdomain TEXTDOMAIN>
714              
715             Sets the default textdomain (initially 'messages').
716              
717             =item B<bindtextdomain TEXTDOMAIN, DIRECTORY>
718              
719             Binds B<TEXTDOMAIN> to B<DIRECTORY>. Huh? An example:
720              
721             bindtextdomain "my-package", "./mylocale";
722              
723             Say, the selected locale (actually the selected locale for category
724             C<LC_MESSAGES>) of the program is 'fr_CH', then the message catalog
725             will be expected in F<./mylocale/fr_CH/LC_MESSAGES/my-package.mo>.
726              
727             =item B<bind_textdomain_codeset TEXTDOMAIN, ENCODING>
728              
729             Sets the output encoding for B<TEXTDOMAIN> to B<ENCODING>.
730              
731             =item B<bind_textdomain_filter TEXTDOMAN, CODEREF, DATA>
732              
733             =item B<bind_textdomain_filter TEXTDOMAN, CODEREF>
734              
735             By default, Locale::Messages will turn the utf-8 flag of all returned
736             messages off. If you want to change this behavior, you can pass
737             a reference to a subroutine that does different things - for example
738             turn the utf-8 flag on, or leave it untouched. The callback function
739             will be called with B<DATA> as the first, and the possibly
740             translated string as the second argument. It should return the
741             possibly modified string.
742              
743             If you want an object method to be called, pass the object itself
744             in the data parameter and write a wrapper function. Example:
745              
746             sub wrapper {
747             my ($string, $obj) = @_;
748            
749             $obj->filterMethod ($string);
750             }
751             my $obj = MyPackage->new;
752              
753             bind_textdomain_filter ('mydomain', \&wrapper, $obj);
754              
755             The function cannot fail and always returns a true value.
756              
757             B<Attention:> If you use the function for setting the utf-8 flag,
758             it is B<your> responsability to ensure that the output is really
759             utf-8. You should only use it, if you have set the environment
760             variable B<OUTPUT_CHARSET> to "utf-8". Additionally you should
761             call bind_textdomain_codeset() with "utf-8" as the second
762             argument.
763              
764             Steven Haryanto has written a module Locale::TextDomain::UTF8(3pm)
765             that addresses the same problem.
766              
767             This function has been introduced in libintl-perl 1.16 and it is
768             B<not> part of the standard gettext API.
769              
770             =item B<turn_utf_8_on VARIABLE>
771              
772             Returns VARIABLE but with the UTF-8 flag (only known in Perl >=5.6)
773             guaranteed to be turned on. This function does not really fit into
774             the module, but it is often handy nevertheless.
775              
776             The flag does B<not> mean that the string is in fact valid utf-8!
777              
778             The function was introduced with libintl-perl version 1.16.
779              
780             =item B<turn_utf_8_off VARIABLE>
781              
782             Returns VARIABLE but with the UTF-8 flag (only known in Perl >=5.6)
783             guaranteed to be turned off. This function does not really fit into
784             the module, but it is often handy nevertheless.
785              
786             The function was introduced with libintl-perl version 1.07.
787              
788             =item B<select_package PACKAGE>
789              
790             By default, B<Locale::Messages> will try to load the XS version of
791             the gettext implementation, i. e. Locale::gettext_xs(3) and will fall
792             back to the pure Perl implementation Locale::gettext_pp(3). You can
793             override this behavior by passing the string "gettext_pp" or
794             "gettext_xs" to the function select_package(). Passing "gettext_pp"
795             here, will prefer the pure Perl implementation.
796              
797             You will normally want to use that in a BEGIN block of your main
798             script.
799              
800             The function was introduced with libintl-perl version 1.03 and is not
801             part of the standard gettext API.
802              
803             Beginning with version 1.22 you can pass other package names than "gettext_pp"
804             or "gettext_xs" and use a completely different backend. It is the caller's
805             responsability to make sure that the selected package offers the same
806             interface as the two standard packages.
807              
808             One package that offers that functionality is Locale::gettext_dumb(3pm).
809              
810             =item B<nl_putenv ENVSPEC>
811              
812             Resembles the ANSI C putenv(3) function. The sole purpose of this
813             function is to work around some ideosyncrasies in the environment
814             processing of Windows systems. If you want to portably set or
815             unset environment variables, use this function instead of directly
816             manipulating C<%ENV>.
817              
818             The argument B<ENVSPEC> may have three different forms.
819              
820             =over 8
821              
822             =item B<LANGUAGE=fr_CH>
823              
824             This would set the environment variable C<LANGUAGE> to "fr_CH".
825              
826             =item B<LANGUAGE=>
827              
828             Normally, this will set the environment variable C<LANGUAGE> to an
829             empty string. Under Windows, however, the environment variable will
830             be deleted instead (and is no longer present in C<%ENV>). Since
831             within libintl-perl empty environment variables are useless, consider
832             this usage as deprecated.
833              
834             =item B<LANGUAGE>
835              
836             This will delete the environment variable B<LANGUAGE>. If you are
837             familiar with the brain-damaged implementation of putenv(3) (resp.
838             _putenv()) in the so-called standard C library of MS-Windows, you
839             may suspect that this is an invalid argument. This is not the case!
840             Passing a variable name not followed by an equal sign will always
841             delete the variable, no matter which operating system you use.
842              
843             =back
844              
845             The function returns true for success, and false for failure. Possible
846             reasons for failure are an invalid syntax or - only under Windows -
847             failure to allocate space for the new environment entry ($! will be
848             set accordingly in this case).
849              
850             Why all this hassle? The 32-bit versions of MS-DOS (currently
851             Windows 95/98/ME/NT/2000/XP/CE/.NET) maintain two distinct blocks
852             of environment variables per process. Which block is considered
853             the "correct" environment is a compile-time option of the Perl
854             interpreter. Unfortunately, if you have build the XS version
855             Locale::gettext_xs(3) under Windows, the underlying library may use
856             a different environment block, and changes you make to C<%ENV> may
857             not be visible to the library.
858              
859             The function nl_putenv() is mostly a funny way of saying
860              
861             LANGUAGE=some_value
862            
863             but it does its best, to pass this information to the gettext
864             library. Under other operating systems than Windows, it only
865             operates on C<%ENV>, under Windows it will call the C library
866             function _putenv() (after doing some cleanup to its arguments),
867             before manipulating C<%ENV>.
868              
869             Please note, that your C<%ENV> is updated by nl_putenv() automatically.
870              
871             The function has been introduced in libintl-perl version 1.10.
872              
873             =item setlocale
874              
875             Modifies and queries program's locale, see the documentation for setlocale()
876             in POSIX(3pm) instead.
877              
878             On some systems, when using GNU gettext, a call from C to setlocale() is
879             - with the help of the C preprocessor - really a call to libintl_setlocale(),
880             which is in turn a wrapper around the system setlocale(3). Failure to call
881             libintl_setlocale() may lead to certain malfunctions. On such systems,
882             B<Locale::Messages::setlocale()> will call the wrapper libintl_setlocale().
883             If you want to avoid problems, you should therefore always call
884             the setlocale() implementation in Locale::Messages(3pm).
885              
886             See L<https://rt.cpan.org/Public/Bug/Display.html?id=83980> or
887             L<https://savannah.gnu.org/bugs/?38162>, and
888             L<https://savannah.gnu.org/bugs/?func=detailitem&item_id=44645> for a discussion
889             of the problem.
890              
891             The function has been introduced in libintl-perl version 1.24.
892              
893             =back
894              
895             =head1 CONSTANTS
896              
897             You can (maybe) get the same constants from POSIX(3); see there for
898             a detailed description
899              
900             =over 4
901              
902             =item B<LC_CTYPE>
903              
904             =item B<LC_NUMERIC>
905              
906             =item B<LC_TIME>
907              
908             =item B<LC_COLLATE>
909              
910             =item B<LC_MONETARY>
911              
912             =item B<LC_MESSAGES>
913              
914             This locale category was the reason that these constants from POSIX(3)
915             were included here. Even if it was present in your systems C include
916             file F<locale.h>, it was not provided by POSIX(3). Perl 5.8 and later
917             seems to export the constant if available, although it is not documented
918             in POSIX(3).
919              
920             Locale::Messages(3) makes an attempt to guess the value of this category for
921             all systems, and assumes the arbitrary value 1729 otherwise.
922              
923             =item B<LC_ALL>
924              
925             If you specify the category B<LC_ALL> as the first argument to
926             POSIX::setlocale(), I<all> locale categories will be affected at once.
927              
928             =back
929              
930             =head1 EXPORT TAGS
931              
932             The module does not export anything unless explicitely requested.
933             You can import groups of functions via two tags:
934              
935             =over 4
936              
937             =item B<use Locale::Messages (':locale_h')>
938              
939             Imports the functions that are normally defined in the C include
940             file F<locale.h>:
941              
942             =over 8
943              
944             =item B<gettext()>
945              
946             =item B<dgettext()>
947              
948             =item B<dcgettext()>
949              
950             =item B<ngettext()>
951              
952             =item B<dngettext()>
953              
954             =item B<dcngettext()>
955              
956             =item B<pgettext()>
957              
958             =item B<dpgettext()>
959              
960             =item B<dcpgettext()>
961              
962             =item B<npgettext()>
963              
964             =item B<dnpgettext()>
965              
966             =item B<dcnpgettext()>
967              
968             =item B<textdomain()>
969              
970             =item B<bindtextdomain()>
971              
972             =item B<bind_textdomain_codeset()>
973              
974             =back
975              
976             =item B<use Locale::Messages (':libintl_h')>
977              
978             Imports the locale category constants:
979              
980             =over 8
981              
982             =item B<LC_CTYPE>
983              
984             =item B<LC_NUMERIC>
985              
986             =item B<LC_TIME>
987              
988             =item B<LC_COLLATE>
989              
990             =item B<LC_MONETARY>
991              
992             =item B<LC_MESSAGES>
993              
994             =item B<LC_ALL>
995              
996             =back
997              
998             =back
999              
1000             =head1 OTHER EXPORTS
1001              
1002             =over 4
1003              
1004             =item B<select_package PACKAGE>
1005              
1006             =back
1007              
1008             =head1 USAGE
1009              
1010             A complete example:
1011              
1012             1: use Locale::Messages qw(:locale_h :libintl_h);
1013             2: use POSIX qw (setlocale);
1014             3: setlocale (LC_MESSAGES, '');
1015             4: textdomain ('my-package');
1016             5: bindtextdomain ('my-package' => '/usr/local/share/locale');
1017             6:
1018             7: print gettext ("Hello world!\n");
1019              
1020             Step by step: Line 1 imports the necessary functions and constants.
1021             In line 3 we set the locale for category LC_MESSAGES to the default
1022             user settings. For C programs you will often read that LC_ALL
1023             is the best category here but this will also change the locale for
1024             LC_NUMERIC and many programs will not work reliably after changing
1025             that category in Perl; choose your own poison!
1026              
1027             In line 4 we say that all messages (translations) without an explicit
1028             domain specification should be retrieved from the message catalog
1029             for the domain 'my-package'. Line 5 has the effect that the message
1030             catalog will be searched under the directory F</usr/local/share/locale>.
1031              
1032             If the user has selected the locale 'fr_CH', and if the file
1033             F</usr/local/share/locale/fr_CH/LC_MESSAGES/my-package.mo>
1034             exists, and if it contains a GNU message object file with a translation
1035             for the string "Hello world!\n", then line 7 will print the French
1036             translation (for Switzerland CH) to STDOUT.
1037              
1038             The documentation for GNU gettext explains how to extract translatable
1039             strings from your Perl files and how to create message catalogs.
1040              
1041             Another less portable example: If your system uses the GNU libc you
1042             should be able to find various files with the name F<libc.mo>, the
1043             message catalog for the library itself. If you have found these
1044             files under F</usr/share/locale>, then you can try the following:
1045              
1046             use Locale::Messages qw(:locale_h :libintl_h);
1047             use POSIX qw (setlocale);
1048              
1049             setlocale LC_MESSAGES, "";
1050             textdomain "libc";
1051              
1052             # The following is actually not needed, since this is
1053             # one of the default search directories.
1054             bindtextdomain libc => '/usr/share/locale';
1055             bind_textdomain_codeset libc => 'iso-8859-1';
1056              
1057             print gettext ("No such file or directory");
1058              
1059             See Locale::TextDomain(3) for much simpler ways.
1060              
1061             =head1 AUTHOR
1062              
1063             Copyright (C) 2002-2026 L<Guido Flohr|http://www.guido-flohr.net/>
1064             (L<mailto:guido.flohr@cantanea.com>), all rights reserved. See the source
1065             code for details!code for details!
1066              
1067             =head1 SEE ALSO
1068              
1069             Locale::TextDomain(3pm), Locale::gettext_pp(3pm), Encode(3pm),
1070             perllocale(3pm), POSIX(3pm), perl(1), gettext(1), gettext(3)
1071              
1072             =cut
1073              
1074             __END__
1075              
1076             Local Variables:
1077             mode: perl
1078             perl-indent-level: 4
1079             perl-continued-statement-offset: 4
1080             perl-continued-brace-offset: 0
1081             perl-brace-offset: -4
1082             perl-brace-imaginary-offset: 0
1083             perl-label-offset: -4
1084             cperl-indent-level: 4
1085             cperl-continued-statement-offset: 2
1086             tab-width: 4
1087             End: