Mention BZ #15674
[glibc.git] / manual / locale.texi
blob2f10fcd2af7b8e5d5b75d0d506643e4f41fa91de
1 @node Locales, Message Translation, Character Set Handling, Top
2 @c %MENU% The country and language can affect the behavior of library functions
3 @chapter Locales and Internationalization
5 Different countries and cultures have varying conventions for how to
6 communicate.  These conventions range from very simple ones, such as the
7 format for representing dates and times, to very complex ones, such as
8 the language spoken.
10 @cindex internationalization
11 @cindex locales
12 @dfn{Internationalization} of software means programming it to be able
13 to adapt to the user's favorite conventions.  In @w{ISO C},
14 internationalization works by means of @dfn{locales}.  Each locale
15 specifies a collection of conventions, one convention for each purpose.
16 The user chooses a set of conventions by specifying a locale (via
17 environment variables).
19 All programs inherit the chosen locale as part of their environment.
20 Provided the programs are written to obey the choice of locale, they
21 will follow the conventions preferred by the user.
23 @menu
24 * Effects of Locale::           Actions affected by the choice of
25                                  locale.
26 * Choosing Locale::             How the user specifies a locale.
27 * Locale Categories::           Different purposes for which you can
28                                  select a locale.
29 * Setting the Locale::          How a program specifies the locale
30                                  with library functions.
31 * Standard Locales::            Locale names available on all systems.
32 * Locale Information::          How to access the information for the locale.
33 * Formatting Numbers::          A dedicated function to format numbers.
34 * Yes-or-No Questions::         Check a Response against the locale.
35 @end menu
37 @node Effects of Locale, Choosing Locale,  , Locales
38 @section What Effects a Locale Has
40 Each locale specifies conventions for several purposes, including the
41 following:
43 @itemize @bullet
44 @item
45 What multibyte character sequences are valid, and how they are
46 interpreted (@pxref{Character Set Handling}).
48 @item
49 Classification of which characters in the local character set are
50 considered alphabetic, and upper- and lower-case conversion conventions
51 (@pxref{Character Handling}).
53 @item
54 The collating sequence for the local language and character set
55 (@pxref{Collation Functions}).
57 @item
58 Formatting of numbers and currency amounts (@pxref{General Numeric}).
60 @item
61 Formatting of dates and times (@pxref{Formatting Calendar Time}).
63 @item
64 What language to use for output, including error messages
65 (@pxref{Message Translation}).
67 @item
68 What language to use for user answers to yes-or-no questions
69 (@pxref{Yes-or-No Questions}).
71 @item
72 What language to use for more complex user input.
73 (The C library doesn't yet help you implement this.)
74 @end itemize
76 Some aspects of adapting to the specified locale are handled
77 automatically by the library subroutines.  For example, all your program
78 needs to do in order to use the collating sequence of the chosen locale
79 is to use @code{strcoll} or @code{strxfrm} to compare strings.
81 Other aspects of locales are beyond the comprehension of the library.
82 For example, the library can't automatically translate your program's
83 output messages into other languages.  The only way you can support
84 output in the user's favorite language is to program this more or less
85 by hand.  The C library provides functions to handle translations for
86 multiple languages easily.
88 This chapter discusses the mechanism by which you can modify the current
89 locale.  The effects of the current locale on specific library functions
90 are discussed in more detail in the descriptions of those functions.
92 @node Choosing Locale, Locale Categories, Effects of Locale, Locales
93 @section Choosing a Locale
95 The simplest way for the user to choose a locale is to set the
96 environment variable @code{LANG}.  This specifies a single locale to use
97 for all purposes.  For example, a user could specify a hypothetical
98 locale named @samp{espana-castellano} to use the standard conventions of
99 most of Spain.
101 The set of locales supported depends on the operating system you are
102 using, and so do their names.  We can't make any promises about what
103 locales will exist, except for one standard locale called @samp{C} or
104 @samp{POSIX}.  Later we will describe how to construct locales.
105 @comment (@pxref{Building Locale Files}).
107 @cindex combining locales
108 A user also has the option of specifying different locales for different
109 purposes---in effect, choosing a mixture of multiple locales.
111 For example, the user might specify the locale @samp{espana-castellano}
112 for most purposes, but specify the locale @samp{usa-english} for
113 currency formatting.  This might make sense if the user is a
114 Spanish-speaking American, working in Spanish, but representing monetary
115 amounts in US dollars.
117 Note that both locales @samp{espana-castellano} and @samp{usa-english},
118 like all locales, would include conventions for all of the purposes to
119 which locales apply.  However, the user can choose to use each locale
120 for a particular subset of those purposes.
122 @node Locale Categories, Setting the Locale, Choosing Locale, Locales
123 @section Categories of Activities that Locales Affect
124 @cindex categories for locales
125 @cindex locale categories
127 The purposes that locales serve are grouped into @dfn{categories}, so
128 that a user or a program can choose the locale for each category
129 independently.  Here is a table of categories; each name is both an
130 environment variable that a user can set, and a macro name that you can
131 use as an argument to @code{setlocale}.
133 @vtable @code
134 @comment locale.h
135 @comment ISO
136 @item LC_COLLATE
137 This category applies to collation of strings (functions @code{strcoll}
138 and @code{strxfrm}); see @ref{Collation Functions}.
140 @comment locale.h
141 @comment ISO
142 @item LC_CTYPE
143 This category applies to classification and conversion of characters,
144 and to multibyte and wide characters;
145 see @ref{Character Handling}, and @ref{Character Set Handling}.
147 @comment locale.h
148 @comment ISO
149 @item LC_MONETARY
150 This category applies to formatting monetary values; see @ref{General Numeric}.
152 @comment locale.h
153 @comment ISO
154 @item LC_NUMERIC
155 This category applies to formatting numeric values that are not
156 monetary; see @ref{General Numeric}.
158 @comment locale.h
159 @comment ISO
160 @item LC_TIME
161 This category applies to formatting date and time values; see
162 @ref{Formatting Calendar Time}.
164 @comment locale.h
165 @comment XOPEN
166 @item LC_MESSAGES
167 This category applies to selecting the language used in the user
168 interface for message translation (@pxref{The Uniforum approach};
169 @pxref{Message catalogs a la X/Open})  and contains regular expressions
170 for affirmative and negative responses.
172 @comment locale.h
173 @comment ISO
174 @item LC_ALL
175 This is not an environment variable; it is only a macro that you can use
176 with @code{setlocale} to set a single locale for all purposes.  Setting
177 this environment variable overwrites all selections by the other
178 @code{LC_*} variables or @code{LANG}.
180 @comment locale.h
181 @comment ISO
182 @item LANG
183 If this environment variable is defined, its value specifies the locale
184 to use for all purposes except as overridden by the variables above.
185 @end vtable
187 @vindex LANGUAGE
188 When developing the message translation functions it was felt that the
189 functionality provided by the variables above is not sufficient.  For
190 example, it should be possible to specify more than one locale name.
191 Take a Swedish user who better speaks German than English, and a program
192 whose messages are output in English by default.  It should be possible
193 to specify that the first choice of language is Swedish, the second
194 German, and if this also fails to use English.  This is
195 possible with the variable @code{LANGUAGE}.  For further description of
196 this GNU extension see @ref{Using gettextized software}.
198 @node Setting the Locale, Standard Locales, Locale Categories, Locales
199 @section How Programs Set the Locale
201 A C program inherits its locale environment variables when it starts up.
202 This happens automatically.  However, these variables do not
203 automatically control the locale used by the library functions, because
204 @w{ISO C} says that all programs start by default in the standard @samp{C}
205 locale.  To use the locales specified by the environment, you must call
206 @code{setlocale}.  Call it as follows:
208 @smallexample
209 setlocale (LC_ALL, "");
210 @end smallexample
212 @noindent
213 to select a locale based on the user choice of the appropriate
214 environment variables.
216 @cindex changing the locale
217 @cindex locale, changing
218 You can also use @code{setlocale} to specify a particular locale, for
219 general use or for a specific category.
221 @pindex locale.h
222 The symbols in this section are defined in the header file @file{locale.h}.
224 @comment locale.h
225 @comment ISO
226 @deftypefun {char *} setlocale (int @var{category}, const char *@var{locale})
227 The function @code{setlocale} sets the current locale for category
228 @var{category} to @var{locale}.  A list of all the locales the system
229 provides can be created by running
231 @pindex locale
232 @smallexample
233   locale -a
234 @end smallexample
236 If @var{category} is @code{LC_ALL}, this specifies the locale for all
237 purposes.  The other possible values of @var{category} specify an
238 single purpose (@pxref{Locale Categories}).
240 You can also use this function to find out the current locale by passing
241 a null pointer as the @var{locale} argument.  In this case,
242 @code{setlocale} returns a string that is the name of the locale
243 currently selected for category @var{category}.
245 The string returned by @code{setlocale} can be overwritten by subsequent
246 calls, so you should make a copy of the string (@pxref{Copying and
247 Concatenation}) if you want to save it past any further calls to
248 @code{setlocale}.  (The standard library is guaranteed never to call
249 @code{setlocale} itself.)
251 You should not modify the string returned by @code{setlocale}.  It might
252 be the same string that was passed as an argument in a previous call to
253 @code{setlocale}.  One requirement is that the @var{category} must be
254 the same in the call the string was returned and the one when the string
255 is passed in as @var{locale} parameter.
257 When you read the current locale for category @code{LC_ALL}, the value
258 encodes the entire combination of selected locales for all categories.
259 In this case, the value is not just a single locale name.  In fact, we
260 don't make any promises about what it looks like.  But if you specify
261 the same ``locale name'' with @code{LC_ALL} in a subsequent call to
262 @code{setlocale}, it restores the same combination of locale selections.
264 To be sure you can use the returned string encoding the currently selected
265 locale at a later time, you must make a copy of the string.  It is not
266 guaranteed that the returned pointer remains valid over time.
268 When the @var{locale} argument is not a null pointer, the string returned
269 by @code{setlocale} reflects the newly-modified locale.
271 If you specify an empty string for @var{locale}, this means to read the
272 appropriate environment variable and use its value to select the locale
273 for @var{category}.
275 If a nonempty string is given for @var{locale}, then the locale of that
276 name is used if possible.
278 If you specify an invalid locale name, @code{setlocale} returns a null
279 pointer and leaves the current locale unchanged.
280 @end deftypefun
282 The path used for finding locale data can be set using the
283 @code{LOCPATH} environment variable. The default path for finding
284 locale data is system specific.  It is computed from the value given
285 as the prefix while configuring the C library.  This value normally is
286 @file{/usr} or @file{/}.  For the former the complete path is:
288 @smallexample
289 /usr/lib/locale
290 @end smallexample
292 Here is an example showing how you might use @code{setlocale} to
293 temporarily switch to a new locale.
295 @smallexample
296 #include <stddef.h>
297 #include <locale.h>
298 #include <stdlib.h>
299 #include <string.h>
301 void
302 with_other_locale (char *new_locale,
303                    void (*subroutine) (int),
304                    int argument)
306   char *old_locale, *saved_locale;
308   /* @r{Get the name of the current locale.}  */
309   old_locale = setlocale (LC_ALL, NULL);
311   /* @r{Copy the name so it won't be clobbered by @code{setlocale}.} */
312   saved_locale = strdup (old_locale);
313   if (saved_locale == NULL)
314     fatal ("Out of memory");
316   /* @r{Now change the locale and do some stuff with it.} */
317   setlocale (LC_ALL, new_locale);
318   (*subroutine) (argument);
320   /* @r{Restore the original locale.} */
321   setlocale (LC_ALL, saved_locale);
322   free (saved_locale);
324 @end smallexample
326 @strong{Portability Note:} Some @w{ISO C} systems may define additional
327 locale categories, and future versions of the library will do so.  For
328 portability, assume that any symbol beginning with @samp{LC_} might be
329 defined in @file{locale.h}.
331 @node Standard Locales, Locale Information, Setting the Locale, Locales
332 @section Standard Locales
334 The only locale names you can count on finding on all operating systems
335 are these three standard ones:
337 @table @code
338 @item "C"
339 This is the standard C locale.  The attributes and behavior it provides
340 are specified in the @w{ISO C} standard.  When your program starts up, it
341 initially uses this locale by default.
343 @item "POSIX"
344 This is the standard POSIX locale.  Currently, it is an alias for the
345 standard C locale.
347 @item ""
348 The empty name says to select a locale based on environment variables.
349 @xref{Locale Categories}.
350 @end table
352 Defining and installing named locales is normally a responsibility of
353 the system administrator at your site (or the person who installed
354 @theglibc{}).  It is also possible for the user to create private
355 locales.  All this will be discussed later when describing the tool to
356 do so.
357 @comment (@pxref{Building Locale Files}).
359 If your program needs to use something other than the @samp{C} locale,
360 it will be more portable if you use whatever locale the user specifies
361 with the environment, rather than trying to specify some non-standard
362 locale explicitly by name.  Remember, different machines might have
363 different sets of locales installed.
365 @node Locale Information, Formatting Numbers, Standard Locales, Locales
366 @section Accessing Locale Information
368 There are several ways to access locale information.  The simplest
369 way is to let the C library itself do the work.  Several of the
370 functions in this library implicitly access the locale data, and use
371 what information is provided by the currently selected locale.  This is
372 how the locale model is meant to work normally.
374 As an example take the @code{strftime} function, which is meant to nicely
375 format date and time information (@pxref{Formatting Calendar Time}).
376 Part of the standard information contained in the @code{LC_TIME}
377 category is the names of the months.  Instead of requiring the
378 programmer to take care of providing the translations the
379 @code{strftime} function does this all by itself.  @code{%A}
380 in the format string is replaced by the appropriate weekday
381 name of the locale currently selected by @code{LC_TIME}.  This is an
382 easy example, and wherever possible functions do things automatically
383 in this way.
385 But there are quite often situations when there is simply no function
386 to perform the task, or it is simply not possible to do the work
387 automatically.  For these cases it is necessary to access the
388 information in the locale directly.  To do this the C library provides
389 two functions: @code{localeconv} and @code{nl_langinfo}.  The former is
390 part of @w{ISO C} and therefore portable, but has a brain-damaged
391 interface.  The second is part of the Unix interface and is portable in
392 as far as the system follows the Unix standards.
394 @menu
395 * The Lame Way to Locale Data::   ISO C's @code{localeconv}.
396 * The Elegant and Fast Way::      X/Open's @code{nl_langinfo}.
397 @end menu
399 @node The Lame Way to Locale Data, The Elegant and Fast Way, ,Locale Information
400 @subsection @code{localeconv}: It is portable but @dots{}
402 Together with the @code{setlocale} function the @w{ISO C} people
403 invented the @code{localeconv} function.  It is a masterpiece of poor
404 design.  It is expensive to use, not extendable, and not generally
405 usable as it provides access to only @code{LC_MONETARY} and
406 @code{LC_NUMERIC} related information.  Nevertheless, if it is
407 applicable to a given situation it should be used since it is very
408 portable.  The function @code{strfmon} formats monetary amounts
409 according to the selected locale using this information.
410 @pindex locale.h
411 @cindex monetary value formatting
412 @cindex numeric value formatting
414 @comment locale.h
415 @comment ISO
416 @deftypefun {struct lconv *} localeconv (void)
417 The @code{localeconv} function returns a pointer to a structure whose
418 components contain information about how numeric and monetary values
419 should be formatted in the current locale.
421 You should not modify the structure or its contents.  The structure might
422 be overwritten by subsequent calls to @code{localeconv}, or by calls to
423 @code{setlocale}, but no other function in the library overwrites this
424 value.
425 @end deftypefun
427 @comment locale.h
428 @comment ISO
429 @deftp {Data Type} {struct lconv}
430 @code{localeconv}'s return value is of this data type.  Its elements are
431 described in the following subsections.
432 @end deftp
434 If a member of the structure @code{struct lconv} has type @code{char},
435 and the value is @code{CHAR_MAX}, it means that the current locale has
436 no value for that parameter.
438 @menu
439 * General Numeric::             Parameters for formatting numbers and
440                                  currency amounts.
441 * Currency Symbol::             How to print the symbol that identifies an
442                                  amount of money (e.g. @samp{$}).
443 * Sign of Money Amount::        How to print the (positive or negative) sign
444                                  for a monetary amount, if one exists.
445 @end menu
447 @node General Numeric, Currency Symbol, , The Lame Way to Locale Data
448 @subsubsection Generic Numeric Formatting Parameters
450 These are the standard members of @code{struct lconv}; there may be
451 others.
453 @table @code
454 @item char *decimal_point
455 @itemx char *mon_decimal_point
456 These are the decimal-point separators used in formatting non-monetary
457 and monetary quantities, respectively.  In the @samp{C} locale, the
458 value of @code{decimal_point} is @code{"."}, and the value of
459 @code{mon_decimal_point} is @code{""}.
460 @cindex decimal-point separator
462 @item char *thousands_sep
463 @itemx char *mon_thousands_sep
464 These are the separators used to delimit groups of digits to the left of
465 the decimal point in formatting non-monetary and monetary quantities,
466 respectively.  In the @samp{C} locale, both members have a value of
467 @code{""} (the empty string).
469 @item char *grouping
470 @itemx char *mon_grouping
471 These are strings that specify how to group the digits to the left of
472 the decimal point.  @code{grouping} applies to non-monetary quantities
473 and @code{mon_grouping} applies to monetary quantities.  Use either
474 @code{thousands_sep} or @code{mon_thousands_sep} to separate the digit
475 groups.
476 @cindex grouping of digits
478 Each member of these strings is to be interpreted as an integer value of
479 type @code{char}.  Successive numbers (from left to right) give the
480 sizes of successive groups (from right to left, starting at the decimal
481 point.)  The last member is either @code{0}, in which case the previous
482 member is used over and over again for all the remaining groups, or
483 @code{CHAR_MAX}, in which case there is no more grouping---or, put
484 another way, any remaining digits form one large group without
485 separators.
487 For example, if @code{grouping} is @code{"\04\03\02"}, the correct
488 grouping for the number @code{123456787654321} is @samp{12}, @samp{34},
489 @samp{56}, @samp{78}, @samp{765}, @samp{4321}.  This uses a group of 4
490 digits at the end, preceded by a group of 3 digits, preceded by groups
491 of 2 digits (as many as needed).  With a separator of @samp{,}, the
492 number would be printed as @samp{12,34,56,78,765,4321}.
494 A value of @code{"\03"} indicates repeated groups of three digits, as
495 normally used in the U.S.
497 In the standard @samp{C} locale, both @code{grouping} and
498 @code{mon_grouping} have a value of @code{""}.  This value specifies no
499 grouping at all.
501 @item char int_frac_digits
502 @itemx char frac_digits
503 These are small integers indicating how many fractional digits (to the
504 right of the decimal point) should be displayed in a monetary value in
505 international and local formats, respectively.  (Most often, both
506 members have the same value.)
508 In the standard @samp{C} locale, both of these members have the value
509 @code{CHAR_MAX}, meaning ``unspecified''.  The ISO standard doesn't say
510 what to do when you find this value; we recommend printing no
511 fractional digits.  (This locale also specifies the empty string for
512 @code{mon_decimal_point}, so printing any fractional digits would be
513 confusing!)
514 @end table
516 @node Currency Symbol, Sign of Money Amount, General Numeric, The Lame Way to Locale Data
517 @subsubsection Printing the Currency Symbol
518 @cindex currency symbols
520 These members of the @code{struct lconv} structure specify how to print
521 the symbol to identify a monetary value---the international analog of
522 @samp{$} for US dollars.
524 Each country has two standard currency symbols.  The @dfn{local currency
525 symbol} is used commonly within the country, while the
526 @dfn{international currency symbol} is used internationally to refer to
527 that country's currency when it is necessary to indicate the country
528 unambiguously.
530 For example, many countries use the dollar as their monetary unit, and
531 when dealing with international currencies it's important to specify
532 that one is dealing with (say) Canadian dollars instead of U.S. dollars
533 or Australian dollars.  But when the context is known to be Canada,
534 there is no need to make this explicit---dollar amounts are implicitly
535 assumed to be in Canadian dollars.
537 @table @code
538 @item char *currency_symbol
539 The local currency symbol for the selected locale.
541 In the standard @samp{C} locale, this member has a value of @code{""}
542 (the empty string), meaning ``unspecified''.  The ISO standard doesn't
543 say what to do when you find this value; we recommend you simply print
544 the empty string as you would print any other string pointed to by this
545 variable.
547 @item char *int_curr_symbol
548 The international currency symbol for the selected locale.
550 The value of @code{int_curr_symbol} should normally consist of a
551 three-letter abbreviation determined by the international standard
552 @cite{ISO 4217 Codes for the Representation of Currency and Funds},
553 followed by a one-character separator (often a space).
555 In the standard @samp{C} locale, this member has a value of @code{""}
556 (the empty string), meaning ``unspecified''.  We recommend you simply print
557 the empty string as you would print any other string pointed to by this
558 variable.
560 @item char p_cs_precedes
561 @itemx char n_cs_precedes
562 @itemx char int_p_cs_precedes
563 @itemx char int_n_cs_precedes
564 These members are @code{1} if the @code{currency_symbol} or
565 @code{int_curr_symbol} strings should precede the value of a monetary
566 amount, or @code{0} if the strings should follow the value.  The
567 @code{p_cs_precedes} and @code{int_p_cs_precedes} members apply to
568 positive amounts (or zero), and the @code{n_cs_precedes} and
569 @code{int_n_cs_precedes} members apply to negative amounts.
571 In the standard @samp{C} locale, all of these members have a value of
572 @code{CHAR_MAX}, meaning ``unspecified''.  The ISO standard doesn't say
573 what to do when you find this value.  We recommend printing the
574 currency symbol before the amount, which is right for most countries.
575 In other words, treat all nonzero values alike in these members.
577 The members with the @code{int_} prefix apply to the
578 @code{int_curr_symbol} while the other two apply to
579 @code{currency_symbol}.
581 @item char p_sep_by_space
582 @itemx char n_sep_by_space
583 @itemx char int_p_sep_by_space
584 @itemx char int_n_sep_by_space
585 These members are @code{1} if a space should appear between the
586 @code{currency_symbol} or @code{int_curr_symbol} strings and the
587 amount, or @code{0} if no space should appear.  The
588 @code{p_sep_by_space} and @code{int_p_sep_by_space} members apply to
589 positive amounts (or zero), and the @code{n_sep_by_space} and
590 @code{int_n_sep_by_space} members apply to negative amounts.
592 In the standard @samp{C} locale, all of these members have a value of
593 @code{CHAR_MAX}, meaning ``unspecified''.  The ISO standard doesn't say
594 what you should do when you find this value; we suggest you treat it as
595 1 (print a space).  In other words, treat all nonzero values alike in
596 these members.
598 The members with the @code{int_} prefix apply to the
599 @code{int_curr_symbol} while the other two apply to
600 @code{currency_symbol}.  There is one specialty with the
601 @code{int_curr_symbol}, though.  Since all legal values contain a space
602 at the end the string one either printf this space (if the currency
603 symbol must appear in front and must be separated) or one has to avoid
604 printing this character at all (especially when at the end of the
605 string).
606 @end table
608 @node Sign of Money Amount, , Currency Symbol, The Lame Way to Locale Data
609 @subsubsection Printing the Sign of a Monetary Amount
611 These members of the @code{struct lconv} structure specify how to print
612 the sign (if any) of a monetary value.
614 @table @code
615 @item char *positive_sign
616 @itemx char *negative_sign
617 These are strings used to indicate positive (or zero) and negative
618 monetary quantities, respectively.
620 In the standard @samp{C} locale, both of these members have a value of
621 @code{""} (the empty string), meaning ``unspecified''.
623 The ISO standard doesn't say what to do when you find this value; we
624 recommend printing @code{positive_sign} as you find it, even if it is
625 empty.  For a negative value, print @code{negative_sign} as you find it
626 unless both it and @code{positive_sign} are empty, in which case print
627 @samp{-} instead.  (Failing to indicate the sign at all seems rather
628 unreasonable.)
630 @item char p_sign_posn
631 @itemx char n_sign_posn
632 @itemx char int_p_sign_posn
633 @itemx char int_n_sign_posn
634 These members are small integers that indicate how to
635 position the sign for nonnegative and negative monetary quantities,
636 respectively.  (The string used by the sign is what was specified with
637 @code{positive_sign} or @code{negative_sign}.)  The possible values are
638 as follows:
640 @table @code
641 @item 0
642 The currency symbol and quantity should be surrounded by parentheses.
644 @item 1
645 Print the sign string before the quantity and currency symbol.
647 @item 2
648 Print the sign string after the quantity and currency symbol.
650 @item 3
651 Print the sign string right before the currency symbol.
653 @item 4
654 Print the sign string right after the currency symbol.
656 @item CHAR_MAX
657 ``Unspecified''.  Both members have this value in the standard
658 @samp{C} locale.
659 @end table
661 The ISO standard doesn't say what you should do when the value is
662 @code{CHAR_MAX}.  We recommend you print the sign after the currency
663 symbol.
665 The members with the @code{int_} prefix apply to the
666 @code{int_curr_symbol} while the other two apply to
667 @code{currency_symbol}.
668 @end table
670 @node The Elegant and Fast Way, , The Lame Way to Locale Data, Locale Information
671 @subsection Pinpoint Access to Locale Data
673 When writing the X/Open Portability Guide the authors realized that the
674 @code{localeconv} function is not enough to provide reasonable access to
675 locale information.  The information which was meant to be available
676 in the locale (as later specified in the POSIX.1 standard) requires more
677 ways to access it.  Therefore the @code{nl_langinfo} function
678 was introduced.
680 @comment langinfo.h
681 @comment XOPEN
682 @deftypefun {char *} nl_langinfo (nl_item @var{item})
683 The @code{nl_langinfo} function can be used to access individual
684 elements of the locale categories.  Unlike the @code{localeconv}
685 function, which returns all the information, @code{nl_langinfo}
686 lets the caller select what information it requires.  This is very
687 fast and it is not a problem to call this function multiple times.
689 A second advantage is that in addition to the numeric and monetary
690 formatting information, information from the
691 @code{LC_TIME} and @code{LC_MESSAGES} categories is available.
693 @pindex langinfo.h
694 The type @code{nl_type} is defined in @file{nl_types.h}.  The argument
695 @var{item} is a numeric value defined in the header @file{langinfo.h}.
696 The X/Open standard defines the following values:
698 @vtable @code
699 @item CODESET
700 @code{nl_langinfo} returns a string with the name of the coded character
701 set used in the selected locale.
703 @item ABDAY_1
704 @itemx ABDAY_2
705 @itemx ABDAY_3
706 @itemx ABDAY_4
707 @itemx ABDAY_5
708 @itemx ABDAY_6
709 @itemx ABDAY_7
710 @code{nl_langinfo} returns the abbreviated weekday name.  @code{ABDAY_1}
711 corresponds to Sunday.
712 @item DAY_1
713 @itemx DAY_2
714 @itemx DAY_3
715 @itemx DAY_4
716 @itemx DAY_5
717 @itemx DAY_6
718 @itemx DAY_7
719 Similar to @code{ABDAY_1} etc., but here the return value is the
720 unabbreviated weekday name.
721 @item ABMON_1
722 @itemx ABMON_2
723 @itemx ABMON_3
724 @itemx ABMON_4
725 @itemx ABMON_5
726 @itemx ABMON_6
727 @itemx ABMON_7
728 @itemx ABMON_8
729 @itemx ABMON_9
730 @itemx ABMON_10
731 @itemx ABMON_11
732 @itemx ABMON_12
733 The return value is abbreviated name of the month.  @code{ABMON_1}
734 corresponds to January.
735 @item MON_1
736 @itemx MON_2
737 @itemx MON_3
738 @itemx MON_4
739 @itemx MON_5
740 @itemx MON_6
741 @itemx MON_7
742 @itemx MON_8
743 @itemx MON_9
744 @itemx MON_10
745 @itemx MON_11
746 @itemx MON_12
747 Similar to @code{ABMON_1} etc., but here the month names are not abbreviated.
748 Here the first value @code{MON_1} also corresponds to January.
749 @item AM_STR
750 @itemx PM_STR
751 The return values are strings which can be used in the representation of time
752 as an hour from 1 to 12 plus an am/pm specifier.
754 Note that in locales which do not use this time representation
755 these strings might be empty, in which case the am/pm format
756 cannot be used at all.
757 @item D_T_FMT
758 The return value can be used as a format string for @code{strftime} to
759 represent time and date in a locale-specific way.
760 @item D_FMT
761 The return value can be used as a format string for @code{strftime} to
762 represent a date in a locale-specific way.
763 @item T_FMT
764 The return value can be used as a format string for @code{strftime} to
765 represent time in a locale-specific way.
766 @item T_FMT_AMPM
767 The return value can be used as a format string for @code{strftime} to
768 represent time in the am/pm format.
770 Note that if the am/pm format does not make any sense for the
771 selected locale, the return value might be the same as the one for
772 @code{T_FMT}.
773 @item ERA
774 The return value represents the era used in the current locale.
776 Most locales do not define this value.  An example of a locale which
777 does define this value is the Japanese one.  In Japan, the traditional
778 representation of dates includes the name of the era corresponding to
779 the then-emperor's reign.
781 Normally it should not be necessary to use this value directly.
782 Specifying the @code{E} modifier in their format strings causes the
783 @code{strftime} functions to use this information.  The format of the
784 returned string is not specified, and therefore you should not assume
785 knowledge of it on different systems.
786 @item ERA_YEAR
787 The return value gives the year in the relevant era of the locale.
788 As for @code{ERA} it should not be necessary to use this value directly.
789 @item ERA_D_T_FMT
790 This return value can be used as a format string for @code{strftime} to
791 represent dates and times in a locale-specific era-based way.
792 @item ERA_D_FMT
793 This return value can be used as a format string for @code{strftime} to
794 represent a date in a locale-specific era-based way.
795 @item ERA_T_FMT
796 This return value can be used as a format string for @code{strftime} to
797 represent time in a locale-specific era-based way.
798 @item ALT_DIGITS
799 The return value is a representation of up to @math{100} values used to
800 represent the values @math{0} to @math{99}.  As for @code{ERA} this
801 value is not intended to be used directly, but instead indirectly
802 through the @code{strftime} function.  When the modifier @code{O} is
803 used in a format which would otherwise use numerals to represent hours,
804 minutes, seconds, weekdays, months, or weeks, the appropriate value for
805 the locale is used instead.
806 @item INT_CURR_SYMBOL
807 The same as the value returned by @code{localeconv} in the
808 @code{int_curr_symbol} element of the @code{struct lconv}.
809 @item CURRENCY_SYMBOL
810 @itemx CRNCYSTR
811 The same as the value returned by @code{localeconv} in the
812 @code{currency_symbol} element of the @code{struct lconv}.
814 @code{CRNCYSTR} is a deprecated alias still required by Unix98.
815 @item MON_DECIMAL_POINT
816 The same as the value returned by @code{localeconv} in the
817 @code{mon_decimal_point} element of the @code{struct lconv}.
818 @item MON_THOUSANDS_SEP
819 The same as the value returned by @code{localeconv} in the
820 @code{mon_thousands_sep} element of the @code{struct lconv}.
821 @item MON_GROUPING
822 The same as the value returned by @code{localeconv} in the
823 @code{mon_grouping} element of the @code{struct lconv}.
824 @item POSITIVE_SIGN
825 The same as the value returned by @code{localeconv} in the
826 @code{positive_sign} element of the @code{struct lconv}.
827 @item NEGATIVE_SIGN
828 The same as the value returned by @code{localeconv} in the
829 @code{negative_sign} element of the @code{struct lconv}.
830 @item INT_FRAC_DIGITS
831 The same as the value returned by @code{localeconv} in the
832 @code{int_frac_digits} element of the @code{struct lconv}.
833 @item FRAC_DIGITS
834 The same as the value returned by @code{localeconv} in the
835 @code{frac_digits} element of the @code{struct lconv}.
836 @item P_CS_PRECEDES
837 The same as the value returned by @code{localeconv} in the
838 @code{p_cs_precedes} element of the @code{struct lconv}.
839 @item P_SEP_BY_SPACE
840 The same as the value returned by @code{localeconv} in the
841 @code{p_sep_by_space} element of the @code{struct lconv}.
842 @item N_CS_PRECEDES
843 The same as the value returned by @code{localeconv} in the
844 @code{n_cs_precedes} element of the @code{struct lconv}.
845 @item N_SEP_BY_SPACE
846 The same as the value returned by @code{localeconv} in the
847 @code{n_sep_by_space} element of the @code{struct lconv}.
848 @item P_SIGN_POSN
849 The same as the value returned by @code{localeconv} in the
850 @code{p_sign_posn} element of the @code{struct lconv}.
851 @item N_SIGN_POSN
852 The same as the value returned by @code{localeconv} in the
853 @code{n_sign_posn} element of the @code{struct lconv}.
855 @item INT_P_CS_PRECEDES
856 The same as the value returned by @code{localeconv} in the
857 @code{int_p_cs_precedes} element of the @code{struct lconv}.
858 @item INT_P_SEP_BY_SPACE
859 The same as the value returned by @code{localeconv} in the
860 @code{int_p_sep_by_space} element of the @code{struct lconv}.
861 @item INT_N_CS_PRECEDES
862 The same as the value returned by @code{localeconv} in the
863 @code{int_n_cs_precedes} element of the @code{struct lconv}.
864 @item INT_N_SEP_BY_SPACE
865 The same as the value returned by @code{localeconv} in the
866 @code{int_n_sep_by_space} element of the @code{struct lconv}.
867 @item INT_P_SIGN_POSN
868 The same as the value returned by @code{localeconv} in the
869 @code{int_p_sign_posn} element of the @code{struct lconv}.
870 @item INT_N_SIGN_POSN
871 The same as the value returned by @code{localeconv} in the
872 @code{int_n_sign_posn} element of the @code{struct lconv}.
874 @item DECIMAL_POINT
875 @itemx RADIXCHAR
876 The same as the value returned by @code{localeconv} in the
877 @code{decimal_point} element of the @code{struct lconv}.
879 The name @code{RADIXCHAR} is a deprecated alias still used in Unix98.
880 @item THOUSANDS_SEP
881 @itemx THOUSEP
882 The same as the value returned by @code{localeconv} in the
883 @code{thousands_sep} element of the @code{struct lconv}.
885 The name @code{THOUSEP} is a deprecated alias still used in Unix98.
886 @item GROUPING
887 The same as the value returned by @code{localeconv} in the
888 @code{grouping} element of the @code{struct lconv}.
889 @item YESEXPR
890 The return value is a regular expression which can be used with the
891 @code{regex} function to recognize a positive response to a yes/no
892 question.  @Theglibc{} provides the @code{rpmatch} function for
893 easier handling in applications.
894 @item NOEXPR
895 The return value is a regular expression which can be used with the
896 @code{regex} function to recognize a negative response to a yes/no
897 question.
898 @item YESSTR
899 The return value is a locale-specific translation of the positive response
900 to a yes/no question.
902 Using this value is deprecated since it is a very special case of
903 message translation, and is better handled by the message
904 translation functions (@pxref{Message Translation}).
906 The use of this symbol is deprecated.  Instead message translation
907 should be used.
908 @item NOSTR
909 The return value is a locale-specific translation of the negative response
910 to a yes/no question.  What is said for @code{YESSTR} is also true here.
912 The use of this symbol is deprecated.  Instead message translation
913 should be used.
914 @end vtable
916 The file @file{langinfo.h} defines a lot more symbols but none of them
917 is official.  Using them is not portable, and the format of the
918 return values might change.  Therefore we recommended you not use
919 them.
921 Note that the return value for any valid argument can be used for
922 in all situations (with the possible exception of the am/pm time formatting
923 codes).  If the user has not selected any locale for the
924 appropriate category, @code{nl_langinfo} returns the information from the
925 @code{"C"} locale.  It is therefore possible to use this function as
926 shown in the example below.
928 If the argument @var{item} is not valid, a pointer to an empty string is
929 returned.
930 @end deftypefun
932 An example of @code{nl_langinfo} usage is a function which has to
933 print a given date and time in a locale-specific way.  At first one
934 might think that, since @code{strftime} internally uses the locale
935 information, writing something like the following is enough:
937 @smallexample
938 size_t
939 i18n_time_n_data (char *s, size_t len, const struct tm *tp)
941   return strftime (s, len, "%X %D", tp);
943 @end smallexample
945 The format contains no weekday or month names and therefore is
946 internationally usable.  Wrong!  The output produced is something like
947 @code{"hh:mm:ss MM/DD/YY"}.  This format is only recognizable in the
948 USA.  Other countries use different formats.  Therefore the function
949 should be rewritten like this:
951 @smallexample
952 size_t
953 i18n_time_n_data (char *s, size_t len, const struct tm *tp)
955   return strftime (s, len, nl_langinfo (D_T_FMT), tp);
957 @end smallexample
959 Now it uses the date and time format of the locale
960 selected when the program runs.  If the user selects the locale
961 correctly there should never be a misunderstanding over the time and
962 date format.
964 @node Formatting Numbers, Yes-or-No Questions, Locale Information, Locales
965 @section A dedicated function to format numbers
967 We have seen that the structure returned by @code{localeconv} as well as
968 the values given to @code{nl_langinfo} allow you to retrieve the various
969 pieces of locale-specific information to format numbers and monetary
970 amounts.  We have also seen that the underlying rules are quite complex.
972 Therefore the X/Open standards introduce a function which uses such
973 locale information, making it easier for the user to format
974 numbers according to these rules.
976 @deftypefun ssize_t strfmon (char *@var{s}, size_t @var{maxsize}, const char *@var{format}, @dots{})
977 The @code{strfmon} function is similar to the @code{strftime} function
978 in that it takes a buffer, its size, a format string,
979 and values to write into the buffer as text in a form specified
980 by the format string.  Like @code{strftime}, the function
981 also returns the number of bytes written into the buffer.
983 There are two differences: @code{strfmon} can take more than one
984 argument, and, of course, the format specification is different.  Like
985 @code{strftime}, the format string consists of normal text, which is
986 output as is, and format specifiers, which are indicated by a @samp{%}.
987 Immediately after the @samp{%}, you can optionally specify various flags
988 and formatting information before the main formatting character, in a
989 similar way to @code{printf}:
991 @itemize @bullet
992 @item
993 Immediately following the @samp{%} there can be one or more of the
994 following flags:
995 @table @asis
996 @item @samp{=@var{f}}
997 The single byte character @var{f} is used for this field as the numeric
998 fill character.  By default this character is a space character.
999 Filling with this character is only performed if a left precision
1000 is specified.  It is not just to fill to the given field width.
1001 @item @samp{^}
1002 The number is printed without grouping the digits according to the rules
1003 of the current locale.  By default grouping is enabled.
1004 @item @samp{+}, @samp{(}
1005 At most one of these flags can be used.  They select which format to
1006 represent the sign of a currency amount.  By default, and if
1007 @samp{+} is given, the locale equivalent of @math{+}/@math{-} is used.  If
1008 @samp{(} is given, negative amounts are enclosed in parentheses.  The
1009 exact format is determined by the values of the @code{LC_MONETARY}
1010 category of the locale selected at program runtime.
1011 @item @samp{!}
1012 The output will not contain the currency symbol.
1013 @item @samp{-}
1014 The output will be formatted left-justified instead of right-justified if
1015 it does not fill the entire field width.
1016 @end table
1017 @end itemize
1019 The next part of a specification is an optional field width.  If no
1020 width is specified @math{0} is taken.  During output, the function first
1021 determines how much space is required.  If it requires at least as many
1022 characters as given by the field width, it is output using as much space
1023 as necessary.  Otherwise, it is extended to use the full width by
1024 filling with the space character.  The presence or absence of the
1025 @samp{-} flag determines the side at which such padding occurs.  If
1026 present, the spaces are added at the right making the output
1027 left-justified, and vice versa.
1029 So far the format looks familiar, being similar to the @code{printf} and
1030 @code{strftime} formats.  However, the next two optional fields
1031 introduce something new.  The first one is a @samp{#} character followed
1032 by a decimal digit string.  The value of the digit string specifies the
1033 number of @emph{digit} positions to the left of the decimal point (or
1034 equivalent).  This does @emph{not} include the grouping character when
1035 the @samp{^} flag is not given.  If the space needed to print the number
1036 does not fill the whole width, the field is padded at the left side with
1037 the fill character, which can be selected using the @samp{=} flag and by
1038 default is a space.  For example, if the field width is selected as 6
1039 and the number is @math{123}, the fill character is @samp{*} the result
1040 will be @samp{***123}.
1042 The second optional field starts with a @samp{.} (period) and consists
1043 of another decimal digit string.  Its value describes the number of
1044 characters printed after the decimal point.  The default is selected
1045 from the current locale (@code{frac_digits}, @code{int_frac_digits}, see
1046 @pxref{General Numeric}).  If the exact representation needs more digits
1047 than given by the field width, the displayed value is rounded.  If the
1048 number of fractional digits is selected to be zero, no decimal point is
1049 printed.
1051 As a GNU extension, the @code{strfmon} implementation in @theglibc{}
1052 allows an optional @samp{L} next as a format modifier.  If this modifier
1053 is given, the argument is expected to be a @code{long double} instead of
1054 a @code{double} value.
1056 Finally, the last component is a format specifier.  There are three
1057 specifiers defined:
1059 @table @asis
1060 @item @samp{i}
1061 Use the locale's rules for formatting an international currency value.
1062 @item @samp{n}
1063 Use the locale's rules for formatting a national currency value.
1064 @item @samp{%}
1065 Place a @samp{%} in the output.  There must be no flag, width
1066 specifier or modifier given, only @samp{%%} is allowed.
1067 @end table
1069 As for @code{printf}, the function reads the format string
1070 from left to right and uses the values passed to the function following
1071 the format string.  The values are expected to be either of type
1072 @code{double} or @code{long double}, depending on the presence of the
1073 modifier @samp{L}.  The result is stored in the buffer pointed to by
1074 @var{s}.  At most @var{maxsize} characters are stored.
1076 The return value of the function is the number of characters stored in
1077 @var{s}, including the terminating @code{NULL} byte.  If the number of
1078 characters stored would exceed @var{maxsize}, the function returns
1079 @math{-1} and the content of the buffer @var{s} is unspecified.  In this
1080 case @code{errno} is set to @code{E2BIG}.
1081 @end deftypefun
1083 A few examples should make clear how the function works.  It is
1084 assumed that all the following pieces of code are executed in a program
1085 which uses the USA locale (@code{en_US}).  The simplest
1086 form of the format is this:
1088 @smallexample
1089 strfmon (buf, 100, "@@%n@@%n@@%n@@", 123.45, -567.89, 12345.678);
1090 @end smallexample
1092 @noindent
1093 The output produced is
1094 @smallexample
1095 "@@$123.45@@-$567.89@@$12,345.68@@"
1096 @end smallexample
1098 We can notice several things here.  First, the widths of the output
1099 numbers are different.  We have not specified a width in the format
1100 string, and so this is no wonder.  Second, the third number is printed
1101 using thousands separators.  The thousands separator for the
1102 @code{en_US} locale is a comma.  The number is also rounded.
1103 @math{.678} is rounded to @math{.68} since the format does not specify a
1104 precision and the default value in the locale is @math{2}.  Finally,
1105 note that the national currency symbol is printed since @samp{%n} was
1106 used, not @samp{i}.  The next example shows how we can align the output.
1108 @smallexample
1109 strfmon (buf, 100, "@@%=*11n@@%=*11n@@%=*11n@@", 123.45, -567.89, 12345.678);
1110 @end smallexample
1112 @noindent
1113 The output this time is:
1115 @smallexample
1116 "@@    $123.45@@   -$567.89@@ $12,345.68@@"
1117 @end smallexample
1119 Two things stand out.  Firstly, all fields have the same width (eleven
1120 characters) since this is the width given in the format and since no
1121 number required more characters to be printed.  The second important
1122 point is that the fill character is not used.  This is correct since the
1123 white space was not used to achieve a precision given by a @samp{#}
1124 modifier, but instead to fill to the given width.  The difference
1125 becomes obvious if we now add a width specification.
1127 @smallexample
1128 strfmon (buf, 100, "@@%=*11#5n@@%=*11#5n@@%=*11#5n@@",
1129          123.45, -567.89, 12345.678);
1130 @end smallexample
1132 @noindent
1133 The output is
1135 @smallexample
1136 "@@ $***123.45@@-$***567.89@@ $12,456.68@@"
1137 @end smallexample
1139 Here we can see that all the currency symbols are now aligned, and that
1140 the space between the currency sign and the number is filled with the
1141 selected fill character.  Note that although the width is selected to be
1142 @math{5} and @math{123.45} has three digits left of the decimal point,
1143 the space is filled with three asterisks.  This is correct since, as
1144 explained above, the width does not include the positions used to store
1145 thousands separators.  One last example should explain the remaining
1146 functionality.
1148 @smallexample
1149 strfmon (buf, 100, "@@%=0(16#5.3i@@%=0(16#5.3i@@%=0(16#5.3i@@",
1150          123.45, -567.89, 12345.678);
1151 @end smallexample
1153 @noindent
1154 This rather complex format string produces the following output:
1156 @smallexample
1157 "@@ USD 000123,450 @@(USD 000567.890)@@ USD 12,345.678 @@"
1158 @end smallexample
1160 The most noticeable change is the alternative way of representing
1161 negative numbers.  In financial circles this is often done using
1162 parentheses, and this is what the @samp{(} flag selected.  The fill
1163 character is now @samp{0}.  Note that this @samp{0} character is not
1164 regarded as a numeric zero, and therefore the first and second numbers
1165 are not printed using a thousands separator.  Since we used the format
1166 specifier @samp{i} instead of @samp{n}, the international form of the
1167 currency symbol is used.  This is a four letter string, in this case
1168 @code{"USD "}.  The last point is that since the precision right of the
1169 decimal point is selected to be three, the first and second numbers are
1170 printed with an extra zero at the end and the third number is printed
1171 without rounding.
1173 @node Yes-or-No Questions,  , Formatting Numbers , Locales
1174 @section Yes-or-No Questions
1176 Some non GUI programs ask a yes-or-no question.  If the messages
1177 (especially the questions) are translated into foreign languages, be
1178 sure that you localize the answers too.  It would be very bad habit to
1179 ask a question in one language and request the answer in another, often
1180 English.
1182 @Theglibc{} contains @code{rpmatch} to give applications easy
1183 access to the corresponding locale definitions.
1185 @comment GNU
1186 @comment stdlib.h
1187 @deftypefun int rpmatch (const char *@var{response})
1188 The function @code{rpmatch} checks the string in @var{response} whether
1189 or not it is a correct yes-or-no answer and if yes, which one.  The
1190 check uses the @code{YESEXPR} and @code{NOEXPR} data in the
1191 @code{LC_MESSAGES} category of the currently selected locale.  The
1192 return value is as follows:
1194 @table @code
1195 @item 1
1196 The user entered an affirmative answer.
1198 @item 0
1199 The user entered a negative answer.
1201 @item -1
1202 The answer matched neither the @code{YESEXPR} nor the @code{NOEXPR}
1203 regular expression.
1204 @end table
1206 This function is not standardized but available beside in @theglibc{} at
1207 least also in the IBM AIX library.
1208 @end deftypefun
1210 @noindent
1211 This function would normally be used like this:
1213 @smallexample
1214   @dots{}
1215   /* @r{Use a safe default.}  */
1216   _Bool doit = false;
1218   fputs (gettext ("Do you really want to do this? "), stdout);
1219   fflush (stdout);
1220   /* @r{Prepare the @code{getline} call.}  */
1221   line = NULL;
1222   len = 0;
1223   while (getline (&line, &len, stdin) >= 0)
1224     @{
1225       /* @r{Check the response.}  */
1226       int res = rpmatch (line);
1227       if (res >= 0)
1228         @{
1229           /* @r{We got a definitive answer.}  */
1230           if (res > 0)
1231             doit = true;
1232           break;
1233         @}
1234     @}
1235   /* @r{Free what @code{getline} allocated.}  */
1236   free (line);
1237 @end smallexample
1239 Note that the loop continues until an read error is detected or until a
1240 definitive (positive or negative) answer is read.