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