1Locale::Messages(3) User Contributed Perl Documentation Locale::Messages(3)
2
3
4
6 Locale::Messages - Gettext Like Message Retrieval
7
9 use Locale::Messages (:locale_h :libintl_h);
10
11 gettext $msgid;
12 dgettext $textdomain, $msgid;
13 dcgettext $textdomain, $msgid, LC_MESSAGES;
14 ngettext $msgid, $msgid_plural, $count;
15 dngettext $textdomain, $msgid, $msgid_plural, $count;
16 dcngettext $textdomain, $msgid, $msgid_plural, $count, LC_MESSAGES;
17 textdomain $textdomain;
18 bindtextdomain $textdomain, $directory;
19 bind_textdomain_codeset $textdomain, $encoding;
20 bind_textdomain_filter $textdomain, \&filter, $data;
21 turn_utf_8_on ($variable);
22 turn_utf_8_off ($variable);
23 nl_putenv ('OUTPUT_CHARSET=koi8-r');
24 my $category = LC_CTYPE;
25 my $category = LC_NUMERIC;
26 my $category = LC_TIME;
27 my $category = LC_COLLATE;
28 my $category = LC_MONETARY;
29 my $category = LC_MESSAGES;
30 my $category = LC_ALL;
31
33 The module Locale::Messages is a wrapper around the interface to mes‐
34 sage translation according to the Uniforum approach that is for example
35 used in GNU gettext and Sun's Solaris. It is intended to allow
36 Locale::Messages(3) to switch between different implementations of the
37 lower level libraries but this is not yet implemented.
38
39 Normally you should not use this module directly, but the high level
40 interface Locale::TextDomain(3) that provides a much simpler interface.
41 This description is therefore deliberately kept brief. Please refer to
42 the GNU gettext documentation available at <http://www.gnu.org/man‐
43 ual/gettext/> for in-depth and background information on the topic.
44
45 The lower level module Locale::gettext_pp(3) provides the Perl imple‐
46 mentation of gettext() and related functions.
47
49 The module exports by default nothing. Every function has to be
50 imported explicitely or via an export tag ("EXPORT TAGS").
51
52 gettext MSGID
53 Returns the translation for MSGID. Example:
54
55 print gettext "Hello World!\n";
56
57 If no translation can be found, the unmodified MSGID is returned,
58 i. e. the function can never fail, and will never mess up your
59 original message.
60
61 Note for Perl 5.6 and later: The returned string will always have
62 the UTF-8 flag off by default. See the documentation for function
63 bind_textdomain_filter() for a way to change this behavior.
64
65 One common mistake is this:
66
67 print gettext "Hello $name!";
68
69 Perl will interpolate the variable $name before the function will
70 see the string. Unless the corresponding message catalog contains
71 a message "Hello Tom!", "Hello Dick!" or "Hello Harry!", no trans‐
72 lation will be found.
73
74 Using printf() and friends has its own problems:
75
76 print sprintf (gettext ("This is the %s %s."), $color, $thing);
77
78 (The example is stupid because neither color nor thing will get
79 translated here ...).
80
81 In English the adjective (the color) will precede the noun, many
82 other languages (for example French or Italian) differ here. The
83 translator of the message may therefore have a hard time to find a
84 translation that will still work and not sound stupid in the target
85 language. Many C implementations of printf() allow to change the
86 order of the arguments, and a French translator could then say:
87
88 "C'est le %$2s %$1s."
89
90 Perl printf() implements this feature as of version 5.8 or better.
91 Consequently you can only use it, if you are sure that your soft‐
92 ware will run with Perl 5.8 or a later version.
93
94 Another disadvantage of using printf() is its cryptic syntax (maybe
95 not for you but translators of your software may have their own
96 opinion).
97
98 See the description of the function "__x()" in Locale::TextDo‐
99 main(3) for a much better way to get around this problem.
100
101 Non-ASCII message ids ...
102
103 You should note that the function (and all other similar functions
104 in this module) does a bytewise comparison of the MSGID for the
105 lookup in the translation catalog, no matter whether obscure utf-8
106 flags are set on it, whether the string looks like utf-8, whether
107 the utf8(3pm) pragma is used, or whatever other weird method past
108 or future perl(1) versions invent for guessing character sets of
109 strings.
110
111 Using other than us-ascii characters in Perl source code is a call
112 for trouble, a compatibility nightmare. Furthermore, GNU gettext
113 only lately introduced support for non-ascii character sets in
114 sources, and support for this feature may not be available every‐
115 where. If you absolutely want to use MSGIDs in non-ascii character
116 sets, it is wise to choose utf-8. This will minimize the risk that
117 perl(1) itself will mess with the strings, and it will also be a
118 guaranty that you can later translate your project into arbitrary
119 target languages.
120
121 Other character sets can theoretically work. Yet, using another
122 character set in the Perl source code than the one used in your
123 message catalogs will never work, since the lookup is done byte‐
124 wise, and all strings with non-ascii characters will not be found.
125
126 Even if you have solved all these problems, there is still one show
127 stopper left: The gettext runtime API lacks a possibility to spec‐
128 ify the character set of the source code (including the original
129 strings). Consequently - in absence of a hint for the input encod‐
130 ing - strings without a translation are not subject to output char‐
131 acter set conversion. In other words: If the (non-determinable)
132 output character set differs from the character set used in the
133 source code, output can be a mixture of two character sets. There
134 is no point in trying to address this problem in the pure Perl ver‐
135 sion of the gettext functions. because breaking compatibilty
136 between the Perl and the C version is a price too high to pay.
137
138 This all boils down to: Only use ASCII characters in your translat‐
139 able strings!
140
141 dgettext TEXTDOMAIN, MSGID
142 Like gettext(), but retrieves the message for the specified TEXTDO‐
143 MAIN instead of the default domain. In case you wonder what a
144 textdomain is, you should really read on with Locale::TextDo‐
145 main(3).
146
147 dcgettext TEXTDOMAIN, MSGID, CATEGORY
148 Like dgettext() but retrieves the message from the specified CATE‐
149 GORY instead of the default category "LC_MESSAGES".
150
151 ngettext MSGID, MSGID_PLURAL, COUNT
152 Retrieves the correct translation for COUNT items. In legacy soft‐
153 ware you will often find something like:
154
155 print "$count file(s) deleted.\n";
156
157 or
158
159 printf "$count file%s deleted.\n", $count == 1 ? '' : 's';
160
161 The first example looks awkward, the second will only work in Eng‐
162 lish and languages with similar plural rules. Before ngettext()
163 was introduced, the best practice for internationalized programs
164 was:
165
166 if ($count == 1) {
167 print gettext "One file deleted.\n";
168 } else {
169 printf gettext "%d files deleted.\n";
170 }
171
172 This is a nuisance for the programmer and often still not suffi‐
173 cient for an adequate translation. Many languages have completely
174 different ideas on numerals. Some (French, Italian, ...) treat 0
175 and 1 alike, others make no distinction at all (Japanese, Korean,
176 Chinese, ...), others have two or more plural forms (Russian, Lat‐
177 vian, Czech, Polish, ...). The solution is:
178
179 printf (ngettext ("One file deleted.\n",
180 "%d files deleted.\n",
181 $count), # argument to ngettext!
182 $count); # argument to printf!
183
184 In English, or if no translation can be found, the first argument
185 (MSGID) is picked if $count is one, the second one otherwise. For
186 other languages, the correct plural form (of 1, 2, 3, 4, ...) is
187 automatically picked, too. You don't have to know anything about
188 the plural rules in the target language, ngettext() will take care
189 of that.
190
191 This is most of the time sufficient but you will have to prove your
192 creativity in cases like
193
194 printf "%d file(s) deleted, and %d file(s) created.\n";
195
196 dngettext TEXTDOMAIN, MSGID, MSGID_PLURAL, COUNT
197 Like ngettext() but retrieves the translation from the specified
198 textdomain instead of the default domain.
199
200 dcngettext TEXTDOMAIN, MSGID, MSGID_PLURAL, COUNT, CATEGORY
201 Like dngettext() but retrieves the translation from the specified
202 category, instead of the default category "LC_MESSAGES".
203
204 textdomain TEXTDOMAIN
205 Sets the default textdomain (initially 'messages').
206
207 bindtextdomain TEXTDOMAIN, DIRECTORY
208 Binds TEXTDOMAIN to DIRECTORY. Huh? An example:
209
210 bindtextdomain "my-package", "./mylocale";
211
212 Say, the selected locale (actually the selected locale for category
213 "LC_MESSAGES") of the program is 'fr_CH', then the message catalog
214 will be expected in ./mylocale/fr_CH/LC_MESSAGES/my-package.mo.
215
216 bind_textdomain_codeset TEXTDOMAIN, ENCODING
217 Sets the output encoding for TEXTDOMAIN to ENCODING.
218
219 bind_textdomain_filter TEXTDOMAN, CODEREF, DATA
220 bind_textdomain_filter TEXTDOMAN, CODEREF
221 By default, Locale::Messages will turn the utf-8 flag of all
222 returned messages off. If you want to change this behavior, you
223 can pass a reference to a subroutine that does different things -
224 for example turn the utf-8 flag on, or leave it untouched. The
225 callback function will be called with DATA as the first, and the
226 possibly translated string as the second argument. It should
227 return the possibly modified string.
228
229 If you want an object method to be called, pass the object itself
230 in the data parameter and write a wrapper function. Example:
231
232 sub wrapper {
233 my ($string, $obj) = @_;
234
235 $obj->filterMethod ($string);
236 }
237 my $obj = MyPackage->new;
238
239 bind_textdomain_filter ('mydomain', \&wrapper, $obj);
240
241 The function cannot fail and always returns a true value.
242
243 Attention: If you use the function for setting the utf-8 flag, it
244 is your responsability to ensure that the output is really utf-8.
245 You should only use it, if you have set the environment variable
246 OUTPUT_CHARSET to "utf-8". Additionally you should call
247 bind_textdomain_codeset() with "utf-8" as the second argument.
248
249 This function has been introduced in libintl-perl 1.16 and it is
250 not part of the standard gettext API.
251
252 turn_utf_8_on VARIABLE
253 Returns VARIABLE but with the UTF-8 flag (only known in Perl >=5.6)
254 guaranteed to be turned on. This function does not really fit into
255 the module, but it is often handy nevertheless.
256
257 The flag does not mean that the string is in fact valid utf-8!
258
259 The function was introduced with libintl-perl version 1.16.
260
261 turn_utf_8_off VARIABLE
262 Returns VARIABLE but with the UTF-8 flag (only known in Perl >=5.6)
263 guaranteed to be turned off. This function does not really fit
264 into the module, but it is often handy nevertheless.
265
266 The function was introduced with libintl-perl version 1.07.
267
268 select_package PACKAGE
269 By default, Locale::Messages will try to load the XS version of the
270 gettext implementation, i. e. Locale::gettext_xs(3) and will fall
271 back to the pure Perl implementation Locale::gettext_pp(3). You
272 can override this behavior by passing the string "gettext_pp" or
273 "gettext_xs" to the function select_package(). Passing "get‐
274 text_pp" here, will prefer the pure Perl implementation.
275
276 You will normally want to use that in a BEGIN block of your main
277 script.
278
279 The function was introduced with libintl-perl version 1.03 and is
280 not part of the standard gettex API.
281
282 nl_putenv ENVSPEC
283 Resembles the ANSI C putenv(3) function. The sole purpose of this
284 function is to work around some ideosyncrasies in the environment
285 processing of Windows systems. If you want to portably set or
286 unset environment variables, use this function instead of directly
287 manipulating %ENV.
288
289 The argument ENVSPEC may have three different forms.
290
291 LANGUAGE=fr_CH
292 This would set the environment variable "LANGUAGE" to
293 "fr_CH".
294
295 LANGUAGE=
296 Normally, this will set the environment variable "LANGUAGE"
297 to an empty string. Under Windows, however, the environ‐
298 ment variable will be deleted instead (and is no longer
299 present in %ENV). Since within libintl-perl empty environ‐
300 ment variables are useless, consider this usage as depre‐
301 cated.
302
303 LANGUAGE
304 This will delete the environment variable LANGUAGE. If you
305 are familiar with the brain-damaged implementation of
306 putenv(3) (resp. _putenv()) in the so-called standard C
307 library of MS-Windows, you may suspect that this is an
308 invalid argument. This is not the case! Passing a vari‐
309 able name not followed by an equal sign will always delete
310 the variable, no matter which operating system you use.
311
312 The function returns true for success, and false for failure. Pos‐
313 sible reasons for failure are an invalid syntax or - only under
314 Windows - failure to allocate space for the new environment entry
315 ($! will be set accordingly in this case).
316
317 Why all this hassle? The 32-bit versions of MS-DOS (currently Win‐
318 dows 95/98/ME/NT/2000/XP/CE/.NET) maintain two distinct blocks of
319 environment variables per process. Which block is considered the
320 "correct" environment is a compile-time option of the Perl inter‐
321 preter. Unfortunately, if you have build the XS version
322 Locale::gettext_xs(3) under Windows, the underlying library may use
323 a different environment block, and changes you make to %ENV may not
324 be visible to the library.
325
326 The function nl_putenv() is mostly a funny way of saying
327
328 LANGUAGE=some_value
329
330 but it does its best, to pass this information to the gettext
331 library. Under other operating systems than Windows, it only oper‐
332 ates on %ENV, under Windows it will call the C library function
333 _putenv() (after doing some cleanup to its arguments), before
334 manipulating %ENV.
335
336 Please note, that you %ENV is updated by nl_putenv() automatically.
337
338 The function has been introduced in libintl-perl version 1.10.
339
341 You can (maybe) get the same constants from POSIX(3); see there for a
342 detailed description
343
344 LC_CTYPE
345 LC_NUMERIC
346 LC_TIME
347 LC_COLLATE
348 LC_MONETARY
349 LC_MESSAGES
350 This locale category was the reason that these constants from
351 POSIX(3) were included here. Even if it was present in your sys‐
352 tems C include file locale.h, it was not provided by POSIX(3).
353 Perl 5.8 and later seems to export the constant if available,
354 although it is not documented in POSIX(3).
355
356 Locale::Messages(3) makes an attempt to guess the value of this
357 category for all systems, and assumes the arbitrary value 1729 oth‐
358 erwise.
359
360 LC_ALL
361 If you specify the category LC_ALL as the first argument to
362 POSIX::setlocale(), all locale categories will be affected at once.
363
365 The module does not export anything unless explicitely requested. You
366 can import groups of functions via two tags:
367
368 use Locale::Messages (':locale_h')
369 Imports the functions that are normally defined in the C include
370 file locale.h:
371
372 gettext()
373 dgettext()
374 dcgettext()
375 ngettext()
376 dngettext()
377 dcngettext()
378 textdomain()
379 bindtextdomain()
380 bind_textdomain_codeset()
381 use Locale::Messages (':libintl_h')
382 Imports the locale category constants:
383
384 LC_CTYPE
385 LC_NUMERIC
386 LC_TIME
387 LC_COLLATE
388 LC_MONETARY
389 LC_MESSAGES
390 LC_ALL
391
393 select_package PACKAGE
394
396 A complete example:
397
398 1: use Locale::Messages qw (:locale_h :libintl_h);
399 2: use POSIX qw (setlocale);
400 3: setlocale (LC_MESSAGES, '');
401 4: textdomain ('my-package');
402 5: bindtextdomain ('my-package' => '/usr/local/share/locale');
403 6:
404 7: print gettext ("Hello world!\n");
405
406 Step by step: Line 1 imports the necessary functions and constants. In
407 line 3 we set the locale for category LC_MESSAGES to the default user
408 settings. For C programs you will often read that LC_ALL is the best
409 category here but this will also change the locale for LC_NUMERIC and
410 many programs will not work reliably after changing that category in
411 Perl; choose your own poison!
412
413 In line 4 we say that all messages (translations) without an explicit
414 domain specification should be retrieved from the message catalog for
415 the domain 'my-package'. Line 5 has the effect that the message cata‐
416 log will be searched under the directory /usr/local/share/locale.
417
418 If the user has selected the locale 'fr_CH', and if the file
419 /usr/local/share/locale/fr_CH/LC_MESSAGES/my-package.mo exists, and if
420 it contains a GNU message object file with a translation for the string
421 "Hello world!\n", then line 7 will print the French translation (for
422 Switzerland CH) to STDOUT.
423
424 The documentation for GNU gettext explains how to extract translatable
425 strings from your Perl files and how to create message catalogs.
426
427 Another less portable example: If your system uses the GNU libc you
428 should be able to find various files with the name libc.mo, the message
429 catalog for the library itself. If you have found these files under
430 /usr/share/locale, then you can try the following:
431
432 use Locale::Messages qw (:locale_h :libintl_h);
433 use POSIX qw (setlocale);
434
435 setlocale LC_MESSAGES, "";
436 textdomain "libc";
437
438 # The following is actually not needed, since this is
439 # one of the default search directories.
440 bindtextdomain libc => '/usr/share/locale';
441 bind_textdomain_codeset libc => 'iso-8859-1';
442
443 print gettext ("No such file or directory");
444
445 See Locale::TextDomain(3) for much simpler ways.
446
448 Copyright (C) 2002-2004, Guido Flohr <guido@imperia.net>, all rights
449 reserved. See the source code for details.
450
451 This software is contributed to the Perl community by Imperia
452 (<http://www.imperia.net/>).
453
455 Locale::TextDomain(3pm), Locale::gettext_pp(3pm), Encode(3pm), perllo‐
456 cale(3pm), POSIX(3pm), perl(1), gettext(1), gettext(3)
457
458
459
460perl v5.8.8 2006-08-28 Locale::Messages(3)