blob: 18285003311f94f67f68672c37db9080006cddad [file] [log] [blame]
xf.libdd93d52023-05-12 07:10:14 -07001@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
4
5Different countries and cultures have varying conventions for how to
6communicate. These conventions range from very simple ones, such as the
7format for representing dates and times, to very complex ones, such as
8the language spoken.
9
10@cindex internationalization
11@cindex locales
12@dfn{Internationalization} of software means programming it to be able
13to adapt to the user's favorite conventions. In @w{ISO C},
14internationalization works by means of @dfn{locales}. Each locale
15specifies a collection of conventions, one convention for each purpose.
16The user chooses a set of conventions by specifying a locale (via
17environment variables).
18
19All programs inherit the chosen locale as part of their environment.
20Provided the programs are written to obey the choice of locale, they
21will follow the conventions preferred by the user.
22
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 Names:: Format of system-specific locale names.
33* Locale Information:: How to access the information for the locale.
34* Formatting Numbers:: A dedicated function to format numbers.
35* Yes-or-No Questions:: Check a Response against the locale.
36@end menu
37
38@node Effects of Locale, Choosing Locale, , Locales
39@section What Effects a Locale Has
40
41Each locale specifies conventions for several purposes, including the
42following:
43
44@itemize @bullet
45@item
46What multibyte character sequences are valid, and how they are
47interpreted (@pxref{Character Set Handling}).
48
49@item
50Classification of which characters in the local character set are
51considered alphabetic, and upper- and lower-case conversion conventions
52(@pxref{Character Handling}).
53
54@item
55The collating sequence for the local language and character set
56(@pxref{Collation Functions}).
57
58@item
59Formatting of numbers and currency amounts (@pxref{General Numeric}).
60
61@item
62Formatting of dates and times (@pxref{Formatting Calendar Time}).
63
64@item
65What language to use for output, including error messages
66(@pxref{Message Translation}).
67
68@item
69What language to use for user answers to yes-or-no questions
70(@pxref{Yes-or-No Questions}).
71
72@item
73What language to use for more complex user input.
74(The C library doesn't yet help you implement this.)
75@end itemize
76
77Some aspects of adapting to the specified locale are handled
78automatically by the library subroutines. For example, all your program
79needs to do in order to use the collating sequence of the chosen locale
80is to use @code{strcoll} or @code{strxfrm} to compare strings.
81
82Other aspects of locales are beyond the comprehension of the library.
83For example, the library can't automatically translate your program's
84output messages into other languages. The only way you can support
85output in the user's favorite language is to program this more or less
86by hand. The C library provides functions to handle translations for
87multiple languages easily.
88
89This chapter discusses the mechanism by which you can modify the current
90locale. The effects of the current locale on specific library functions
91are discussed in more detail in the descriptions of those functions.
92
93@node Choosing Locale, Locale Categories, Effects of Locale, Locales
94@section Choosing a Locale
95
96The simplest way for the user to choose a locale is to set the
97environment variable @code{LANG}. This specifies a single locale to use
98for all purposes. For example, a user could specify a hypothetical
99locale named @samp{espana-castellano} to use the standard conventions of
100most of Spain.
101
102The set of locales supported depends on the operating system you are
103using, and so do their names, except that the standard locale called
104@samp{C} or @samp{POSIX} always exist. @xref{Locale Names}.
105
106In order to force the system to always use the default locale, the
107user can set the @code{LC_ALL} environment variable to @samp{C}.
108
109@cindex combining locales
110A user also has the option of specifying different locales for
111different purposes---in effect, choosing a mixture of multiple
112locales. @xref{Locale Categories}.
113
114For example, the user might specify the locale @samp{espana-castellano}
115for most purposes, but specify the locale @samp{usa-english} for
116currency formatting. This might make sense if the user is a
117Spanish-speaking American, working in Spanish, but representing monetary
118amounts in US dollars.
119
120Note that both locales @samp{espana-castellano} and @samp{usa-english},
121like all locales, would include conventions for all of the purposes to
122which locales apply. However, the user can choose to use each locale
123for a particular subset of those purposes.
124
125@node Locale Categories, Setting the Locale, Choosing Locale, Locales
126@section Locale Categories
127@cindex categories for locales
128@cindex locale categories
129
130The purposes that locales serve are grouped into @dfn{categories}, so
131that a user or a program can choose the locale for each category
132independently. Here is a table of categories; each name is both an
133environment variable that a user can set, and a macro name that you can
134use as the first argument to @code{setlocale}.
135
136The contents of the environment variable (or the string in the second
137argument to @code{setlocale}) has to be a valid locale name.
138@xref{Locale Names}.
139
140@vtable @code
141@comment locale.h
142@comment ISO
143@item LC_COLLATE
144This category applies to collation of strings (functions @code{strcoll}
145and @code{strxfrm}); see @ref{Collation Functions}.
146
147@comment locale.h
148@comment ISO
149@item LC_CTYPE
150This category applies to classification and conversion of characters,
151and to multibyte and wide characters;
152see @ref{Character Handling}, and @ref{Character Set Handling}.
153
154@comment locale.h
155@comment ISO
156@item LC_MONETARY
157This category applies to formatting monetary values; see @ref{General Numeric}.
158
159@comment locale.h
160@comment ISO
161@item LC_NUMERIC
162This category applies to formatting numeric values that are not
163monetary; see @ref{General Numeric}.
164
165@comment locale.h
166@comment ISO
167@item LC_TIME
168This category applies to formatting date and time values; see
169@ref{Formatting Calendar Time}.
170
171@comment locale.h
172@comment XOPEN
173@item LC_MESSAGES
174This category applies to selecting the language used in the user
175interface for message translation (@pxref{The Uniforum approach};
176@pxref{Message catalogs a la X/Open}) and contains regular expressions
177for affirmative and negative responses.
178
179@comment locale.h
180@comment ISO
181@item LC_ALL
182This is not a category; it is only a macro that you can use
183with @code{setlocale} to set a single locale for all purposes. Setting
184this environment variable overwrites all selections by the other
185@code{LC_*} variables or @code{LANG}.
186
187@comment locale.h
188@comment ISO
189@item LANG
190If this environment variable is defined, its value specifies the locale
191to use for all purposes except as overridden by the variables above.
192@end vtable
193
194@vindex LANGUAGE
195When developing the message translation functions it was felt that the
196functionality provided by the variables above is not sufficient. For
197example, it should be possible to specify more than one locale name.
198Take a Swedish user who better speaks German than English, and a program
199whose messages are output in English by default. It should be possible
200to specify that the first choice of language is Swedish, the second
201German, and if this also fails to use English. This is
202possible with the variable @code{LANGUAGE}. For further description of
203this GNU extension see @ref{Using gettextized software}.
204
205@node Setting the Locale, Standard Locales, Locale Categories, Locales
206@section How Programs Set the Locale
207
208A C program inherits its locale environment variables when it starts up.
209This happens automatically. However, these variables do not
210automatically control the locale used by the library functions, because
211@w{ISO C} says that all programs start by default in the standard @samp{C}
212locale. To use the locales specified by the environment, you must call
213@code{setlocale}. Call it as follows:
214
215@smallexample
216setlocale (LC_ALL, "");
217@end smallexample
218
219@noindent
220to select a locale based on the user choice of the appropriate
221environment variables.
222
223@cindex changing the locale
224@cindex locale, changing
225You can also use @code{setlocale} to specify a particular locale, for
226general use or for a specific category.
227
228@pindex locale.h
229The symbols in this section are defined in the header file @file{locale.h}.
230
231@comment locale.h
232@comment ISO
233@deftypefun {char *} setlocale (int @var{category}, const char *@var{locale})
234@safety{@prelim{}@mtunsafe{@mtasuconst{:@mtslocale{}} @mtsenv{}}@asunsafe{@asuinit{} @asulock{} @ascuheap{} @asucorrupt{}}@acunsafe{@acuinit{} @acucorrupt{} @aculock{} @acsmem{} @acsfd{}}}
235@c Uses of the global locale object are unguarded in functions that
236@c ought to be MT-Safe, so we're ruling out the use of this function
237@c once threads are started. It takes a write lock itself, but it may
238@c return a pointer loaded from the global locale object after releasing
239@c the lock, or before taking it.
240@c setlocale @mtasuconst:@mtslocale @mtsenv @asuinit @ascuheap @asulock @asucorrupt @acucorrupt @acsmem @acsfd @aculock
241@c libc_rwlock_wrlock @asulock @aculock
242@c libc_rwlock_unlock @aculock
243@c getenv LOCPATH @mtsenv
244@c malloc @ascuheap @acsmem
245@c free @ascuheap @acsmem
246@c new_composite_name ok
247@c setdata ok
248@c setname ok
249@c _nl_find_locale @mtsenv @asuinit @ascuheap @asulock @asucorrupt @acucorrupt @acsmem @acsfd @aculock
250@c getenv LC_ALL and LANG @mtsenv
251@c _nl_load_locale_from_archive @ascuheap @acucorrupt @acsmem @acsfd
252@c sysconf _SC_PAGE_SIZE ok
253@c _nl_normalize_codeset @ascuheap @acsmem
254@c isalnum_l ok (C locale)
255@c isdigit_l ok (C locale)
256@c malloc @ascuheap @acsmem
257@c tolower_l ok (C locale)
258@c open_not_cancel_2 @acsfd
259@c fxstat64 ok
260@c close_not_cancel_no_status ok
261@c __mmap64 @acsmem
262@c calculate_head_size ok
263@c __munmap ok
264@c compute_hashval ok
265@c qsort dup @acucorrupt
266@c rangecmp ok
267@c malloc @ascuheap @acsmem
268@c strdup @ascuheap @acsmem
269@c _nl_intern_locale_data @ascuheap @acsmem
270@c malloc @ascuheap @acsmem
271@c free @ascuheap @acsmem
272@c _nl_expand_alias @ascuheap @asulock @acsmem @acsfd @aculock
273@c libc_lock_lock @asulock @aculock
274@c bsearch ok
275@c alias_compare ok
276@c strcasecmp ok
277@c read_alias_file @ascuheap @asulock @acsmem @acsfd @aculock
278@c fopen @ascuheap @asulock @acsmem @acsfd @aculock
279@c fsetlocking ok
280@c feof_unlocked ok
281@c fgets_unlocked ok
282@c isspace ok (locale mutex is locked)
283@c extend_alias_table @ascuheap @acsmem
284@c realloc @ascuheap @acsmem
285@c realloc @ascuheap @acsmem
286@c fclose @ascuheap @asulock @acsmem @acsfd @aculock
287@c qsort @ascuheap @acsmem
288@c alias_compare dup
289@c libc_lock_unlock @aculock
290@c _nl_explode_name @ascuheap @acsmem
291@c _nl_find_language ok
292@c _nl_normalize_codeset dup @ascuheap @acsmem
293@c _nl_make_l10nflist @ascuheap @acsmem
294@c malloc @ascuheap @acsmem
295@c free @ascuheap @acsmem
296@c __argz_stringify ok
297@c __argz_count ok
298@c __argz_next ok
299@c _nl_load_locale @ascuheap @acsmem @acsfd
300@c open_not_cancel_2 @acsfd
301@c __fxstat64 ok
302@c close_not_cancel_no_status ok
303@c mmap @acsmem
304@c malloc @ascuheap @acsmem
305@c read_not_cancel ok
306@c free @ascuheap @acsmem
307@c _nl_intern_locale_data dup @ascuheap @acsmem
308@c munmap ok
309@c __gconv_compare_alias @asuinit @ascuheap @asucorrupt @asulock @acsmem@acucorrupt @acsfd @aculock
310@c __gconv_read_conf @asuinit @ascuheap @asucorrupt @asulock @acsmem@acucorrupt @acsfd @aculock
311@c (libc_once-initializes gconv_cache and gconv_path_envvar; they're
312@c never modified afterwards)
313@c __gconv_load_cache @ascuheap @acsmem @acsfd
314@c getenv GCONV_PATH @mtsenv
315@c open_not_cancel @acsfd
316@c __fxstat64 ok
317@c close_not_cancel_no_status ok
318@c mmap @acsmem
319@c malloc @ascuheap @acsmem
320@c __read ok
321@c free @ascuheap @acsmem
322@c munmap ok
323@c __gconv_get_path @asulock @ascuheap @aculock @acsmem @acsfd
324@c getcwd @ascuheap @acsmem @acsfd
325@c libc_lock_lock @asulock @aculock
326@c malloc @ascuheap @acsmem
327@c strtok_r ok
328@c libc_lock_unlock @aculock
329@c read_conf_file @ascuheap @asucorrupt @asulock @acsmem @acucorrupt @acsfd @aculock
330@c fopen @ascuheap @asulock @acsmem @acsfd @aculock
331@c fsetlocking ok
332@c feof_unlocked ok
333@c getdelim @ascuheap @asucorrupt @acsmem @acucorrupt
334@c isspace_l ok (C locale)
335@c add_alias
336@c isspace_l ok (C locale)
337@c toupper_l ok (C locale)
338@c add_alias2 dup @ascuheap @acucorrupt @acsmem
339@c add_module @ascuheap @acsmem
340@c isspace_l ok (C locale)
341@c toupper_l ok (C locale)
342@c strtol ok (@mtslocale but we hold the locale lock)
343@c tfind __gconv_alias_db ok
344@c __gconv_alias_compare dup ok
345@c calloc @ascuheap @acsmem
346@c insert_module dup @ascuheap
347@c __tfind ok (because the tree is read only by then)
348@c __gconv_alias_compare dup ok
349@c insert_module @ascuheap
350@c free @ascuheap
351@c add_alias2 @ascuheap @acucorrupt @acsmem
352@c detect_conflict ok, reads __gconv_modules_db
353@c malloc @ascuheap @acsmem
354@c tsearch __gconv_alias_db @ascuheap @acucorrupt @acsmem [exclusive tree, no @mtsrace]
355@c __gconv_alias_compare ok
356@c free @ascuheap
357@c __gconv_compare_alias_cache ok
358@c find_module_idx ok
359@c do_lookup_alias ok
360@c __tfind ok (because the tree is read only by then)
361@c __gconv_alias_compare ok
362@c strndup @ascuheap @acsmem
363@c strcasecmp_l ok (C locale)
364The function @code{setlocale} sets the current locale for category
365@var{category} to @var{locale}.
366
367If @var{category} is @code{LC_ALL}, this specifies the locale for all
368purposes. The other possible values of @var{category} specify an
369single purpose (@pxref{Locale Categories}).
370
371You can also use this function to find out the current locale by passing
372a null pointer as the @var{locale} argument. In this case,
373@code{setlocale} returns a string that is the name of the locale
374currently selected for category @var{category}.
375
376The string returned by @code{setlocale} can be overwritten by subsequent
377calls, so you should make a copy of the string (@pxref{Copying Strings
378and Arrays}) if you want to save it past any further calls to
379@code{setlocale}. (The standard library is guaranteed never to call
380@code{setlocale} itself.)
381
382You should not modify the string returned by @code{setlocale}. It might
383be the same string that was passed as an argument in a previous call to
384@code{setlocale}. One requirement is that the @var{category} must be
385the same in the call the string was returned and the one when the string
386is passed in as @var{locale} parameter.
387
388When you read the current locale for category @code{LC_ALL}, the value
389encodes the entire combination of selected locales for all categories.
390If you specify the same ``locale name'' with @code{LC_ALL} in a
391subsequent call to @code{setlocale}, it restores the same combination
392of locale selections.
393
394To be sure you can use the returned string encoding the currently selected
395locale at a later time, you must make a copy of the string. It is not
396guaranteed that the returned pointer remains valid over time.
397
398When the @var{locale} argument is not a null pointer, the string returned
399by @code{setlocale} reflects the newly-modified locale.
400
401If you specify an empty string for @var{locale}, this means to read the
402appropriate environment variable and use its value to select the locale
403for @var{category}.
404
405If a nonempty string is given for @var{locale}, then the locale of that
406name is used if possible.
407
408The effective locale name (either the second argument to
409@code{setlocale}, or if the argument is an empty string, the name
410obtained from the process environment) must be valid locale name.
411@xref{Locale Names}.
412
413If you specify an invalid locale name, @code{setlocale} returns a null
414pointer and leaves the current locale unchanged.
415@end deftypefun
416
417Here is an example showing how you might use @code{setlocale} to
418temporarily switch to a new locale.
419
420@smallexample
421#include <stddef.h>
422#include <locale.h>
423#include <stdlib.h>
424#include <string.h>
425
426void
427with_other_locale (char *new_locale,
428 void (*subroutine) (int),
429 int argument)
430@{
431 char *old_locale, *saved_locale;
432
433 /* @r{Get the name of the current locale.} */
434 old_locale = setlocale (LC_ALL, NULL);
435
436 /* @r{Copy the name so it won't be clobbered by @code{setlocale}.} */
437 saved_locale = strdup (old_locale);
438 if (saved_locale == NULL)
439 fatal ("Out of memory");
440
441 /* @r{Now change the locale and do some stuff with it.} */
442 setlocale (LC_ALL, new_locale);
443 (*subroutine) (argument);
444
445 /* @r{Restore the original locale.} */
446 setlocale (LC_ALL, saved_locale);
447 free (saved_locale);
448@}
449@end smallexample
450
451@strong{Portability Note:} Some @w{ISO C} systems may define additional
452locale categories, and future versions of the library will do so. For
453portability, assume that any symbol beginning with @samp{LC_} might be
454defined in @file{locale.h}.
455
456@node Standard Locales, Locale Names, Setting the Locale, Locales
457@section Standard Locales
458
459The only locale names you can count on finding on all operating systems
460are these three standard ones:
461
462@table @code
463@item "C"
464This is the standard C locale. The attributes and behavior it provides
465are specified in the @w{ISO C} standard. When your program starts up, it
466initially uses this locale by default.
467
468@item "POSIX"
469This is the standard POSIX locale. Currently, it is an alias for the
470standard C locale.
471
472@item ""
473The empty name says to select a locale based on environment variables.
474@xref{Locale Categories}.
475@end table
476
477Defining and installing named locales is normally a responsibility of
478the system administrator at your site (or the person who installed
479@theglibc{}). It is also possible for the user to create private
480locales. All this will be discussed later when describing the tool to
481do so.
482@comment (@pxref{Building Locale Files}).
483
484If your program needs to use something other than the @samp{C} locale,
485it will be more portable if you use whatever locale the user specifies
486with the environment, rather than trying to specify some non-standard
487locale explicitly by name. Remember, different machines might have
488different sets of locales installed.
489
490@node Locale Names, Locale Information, Standard Locales, Locales
491@section Locale Names
492
493The following command prints a list of locales supported by the
494system:
495
496@pindex locale
497@smallexample
498 locale -a
499@end smallexample
500
501@strong{Portability Note:} With the notable exception of the standard
502locale names @samp{C} and @samp{POSIX}, locale names are
503system-specific.
504
505Most locale names follow XPG syntax and consist of up to four parts:
506
507@smallexample
508@var{language}[_@var{territory}[.@var{codeset}]][@@@var{modifier}]
509@end smallexample
510
511Beside the first part, all of them are allowed to be missing. If the
512full specified locale is not found, less specific ones are looked for.
513The various parts will be stripped off, in the following order:
514
515@enumerate
516@item
517codeset
518@item
519normalized codeset
520@item
521territory
522@item
523modifier
524@end enumerate
525
526For example, the locale name @samp{de_AT.iso885915@@euro} denotes a
527German-language locale for use in Austria, using the ISO-8859-15
528(Latin-9) character set, and with the Euro as the currency symbol.
529
530In addition to locale names which follow XPG syntax, systems may
531provide aliases such as @samp{german}. Both categories of names must
532not contain the slash character @samp{/}.
533
534If the locale name starts with a slash @samp{/}, it is treated as a
535path relative to the configured locale directories; see @code{LOCPATH}
536below. The specified path must not contain a component @samp{..}, or
537the name is invalid, and @code{setlocale} will fail.
538
539@strong{Portability Note:} POSIX suggests that if a locale name starts
540with a slash @samp{/}, it is resolved as an absolute path. However,
541@theglibc{} treats it as a relative path under the directories listed
542in @code{LOCPATH} (or the default locale directory if @code{LOCPATH}
543is unset).
544
545Locale names which are longer than an implementation-defined limit are
546invalid and cause @code{setlocale} to fail.
547
548As a special case, locale names used with @code{LC_ALL} can combine
549several locales, reflecting different locale settings for different
550categories. For example, you might want to use a U.S. locale with ISO
551A4 paper format, so you set @code{LANG} to @samp{en_US.UTF-8}, and
552@code{LC_PAPER} to @samp{de_DE.UTF-8}. In this case, the
553@code{LC_ALL}-style combined locale name is
554
555@smallexample
556LC_CTYPE=en_US.UTF-8;LC_TIME=en_US.UTF-8;LC_PAPER=de_DE.UTF-8;@dots{}
557@end smallexample
558
559followed by other category settings not shown here.
560
561@vindex LOCPATH
562The path used for finding locale data can be set using the
563@code{LOCPATH} environment variable. This variable lists the
564directories in which to search for locale definitions, separated by a
565colon @samp{:}.
566
567The default path for finding locale data is system specific. A typical
568value for the @code{LOCPATH} default is:
569
570@smallexample
571/usr/share/locale
572@end smallexample
573
574The value of @code{LOCPATH} is ignored by privileged programs for
575security reasons, and only the default directory is used.
576
577@node Locale Information, Formatting Numbers, Locale Names, Locales
578@section Accessing Locale Information
579
580There are several ways to access locale information. The simplest
581way is to let the C library itself do the work. Several of the
582functions in this library implicitly access the locale data, and use
583what information is provided by the currently selected locale. This is
584how the locale model is meant to work normally.
585
586As an example take the @code{strftime} function, which is meant to nicely
587format date and time information (@pxref{Formatting Calendar Time}).
588Part of the standard information contained in the @code{LC_TIME}
589category is the names of the months. Instead of requiring the
590programmer to take care of providing the translations the
591@code{strftime} function does this all by itself. @code{%A}
592in the format string is replaced by the appropriate weekday
593name of the locale currently selected by @code{LC_TIME}. This is an
594easy example, and wherever possible functions do things automatically
595in this way.
596
597But there are quite often situations when there is simply no function
598to perform the task, or it is simply not possible to do the work
599automatically. For these cases it is necessary to access the
600information in the locale directly. To do this the C library provides
601two functions: @code{localeconv} and @code{nl_langinfo}. The former is
602part of @w{ISO C} and therefore portable, but has a brain-damaged
603interface. The second is part of the Unix interface and is portable in
604as far as the system follows the Unix standards.
605
606@menu
607* The Lame Way to Locale Data:: ISO C's @code{localeconv}.
608* The Elegant and Fast Way:: X/Open's @code{nl_langinfo}.
609@end menu
610
611@node The Lame Way to Locale Data, The Elegant and Fast Way, ,Locale Information
612@subsection @code{localeconv}: It is portable but @dots{}
613
614Together with the @code{setlocale} function the @w{ISO C} people
615invented the @code{localeconv} function. It is a masterpiece of poor
616design. It is expensive to use, not extendable, and not generally
617usable as it provides access to only @code{LC_MONETARY} and
618@code{LC_NUMERIC} related information. Nevertheless, if it is
619applicable to a given situation it should be used since it is very
620portable. The function @code{strfmon} formats monetary amounts
621according to the selected locale using this information.
622@pindex locale.h
623@cindex monetary value formatting
624@cindex numeric value formatting
625
626@comment locale.h
627@comment ISO
628@deftypefun {struct lconv *} localeconv (void)
629@safety{@prelim{}@mtunsafe{@mtasurace{:localeconv} @mtslocale{}}@asunsafe{}@acsafe{}}
630@c This function reads from multiple components of the locale object,
631@c without synchronization, while writing to the static buffer it uses
632@c as the return value.
633The @code{localeconv} function returns a pointer to a structure whose
634components contain information about how numeric and monetary values
635should be formatted in the current locale.
636
637You should not modify the structure or its contents. The structure might
638be overwritten by subsequent calls to @code{localeconv}, or by calls to
639@code{setlocale}, but no other function in the library overwrites this
640value.
641@end deftypefun
642
643@comment locale.h
644@comment ISO
645@deftp {Data Type} {struct lconv}
646@code{localeconv}'s return value is of this data type. Its elements are
647described in the following subsections.
648@end deftp
649
650If a member of the structure @code{struct lconv} has type @code{char},
651and the value is @code{CHAR_MAX}, it means that the current locale has
652no value for that parameter.
653
654@menu
655* General Numeric:: Parameters for formatting numbers and
656 currency amounts.
657* Currency Symbol:: How to print the symbol that identifies an
658 amount of money (e.g. @samp{$}).
659* Sign of Money Amount:: How to print the (positive or negative) sign
660 for a monetary amount, if one exists.
661@end menu
662
663@node General Numeric, Currency Symbol, , The Lame Way to Locale Data
664@subsubsection Generic Numeric Formatting Parameters
665
666These are the standard members of @code{struct lconv}; there may be
667others.
668
669@table @code
670@item char *decimal_point
671@itemx char *mon_decimal_point
672These are the decimal-point separators used in formatting non-monetary
673and monetary quantities, respectively. In the @samp{C} locale, the
674value of @code{decimal_point} is @code{"."}, and the value of
675@code{mon_decimal_point} is @code{""}.
676@cindex decimal-point separator
677
678@item char *thousands_sep
679@itemx char *mon_thousands_sep
680These are the separators used to delimit groups of digits to the left of
681the decimal point in formatting non-monetary and monetary quantities,
682respectively. In the @samp{C} locale, both members have a value of
683@code{""} (the empty string).
684
685@item char *grouping
686@itemx char *mon_grouping
687These are strings that specify how to group the digits to the left of
688the decimal point. @code{grouping} applies to non-monetary quantities
689and @code{mon_grouping} applies to monetary quantities. Use either
690@code{thousands_sep} or @code{mon_thousands_sep} to separate the digit
691groups.
692@cindex grouping of digits
693
694Each member of these strings is to be interpreted as an integer value of
695type @code{char}. Successive numbers (from left to right) give the
696sizes of successive groups (from right to left, starting at the decimal
697point.) The last member is either @code{0}, in which case the previous
698member is used over and over again for all the remaining groups, or
699@code{CHAR_MAX}, in which case there is no more grouping---or, put
700another way, any remaining digits form one large group without
701separators.
702
703For example, if @code{grouping} is @code{"\04\03\02"}, the correct
704grouping for the number @code{123456787654321} is @samp{12}, @samp{34},
705@samp{56}, @samp{78}, @samp{765}, @samp{4321}. This uses a group of 4
706digits at the end, preceded by a group of 3 digits, preceded by groups
707of 2 digits (as many as needed). With a separator of @samp{,}, the
708number would be printed as @samp{12,34,56,78,765,4321}.
709
710A value of @code{"\03"} indicates repeated groups of three digits, as
711normally used in the U.S.
712
713In the standard @samp{C} locale, both @code{grouping} and
714@code{mon_grouping} have a value of @code{""}. This value specifies no
715grouping at all.
716
717@item char int_frac_digits
718@itemx char frac_digits
719These are small integers indicating how many fractional digits (to the
720right of the decimal point) should be displayed in a monetary value in
721international and local formats, respectively. (Most often, both
722members have the same value.)
723
724In the standard @samp{C} locale, both of these members have the value
725@code{CHAR_MAX}, meaning ``unspecified''. The ISO standard doesn't say
726what to do when you find this value; we recommend printing no
727fractional digits. (This locale also specifies the empty string for
728@code{mon_decimal_point}, so printing any fractional digits would be
729confusing!)
730@end table
731
732@node Currency Symbol, Sign of Money Amount, General Numeric, The Lame Way to Locale Data
733@subsubsection Printing the Currency Symbol
734@cindex currency symbols
735
736These members of the @code{struct lconv} structure specify how to print
737the symbol to identify a monetary value---the international analog of
738@samp{$} for US dollars.
739
740Each country has two standard currency symbols. The @dfn{local currency
741symbol} is used commonly within the country, while the
742@dfn{international currency symbol} is used internationally to refer to
743that country's currency when it is necessary to indicate the country
744unambiguously.
745
746For example, many countries use the dollar as their monetary unit, and
747when dealing with international currencies it's important to specify
748that one is dealing with (say) Canadian dollars instead of U.S. dollars
749or Australian dollars. But when the context is known to be Canada,
750there is no need to make this explicit---dollar amounts are implicitly
751assumed to be in Canadian dollars.
752
753@table @code
754@item char *currency_symbol
755The local currency symbol for the selected locale.
756
757In the standard @samp{C} locale, this member has a value of @code{""}
758(the empty string), meaning ``unspecified''. The ISO standard doesn't
759say what to do when you find this value; we recommend you simply print
760the empty string as you would print any other string pointed to by this
761variable.
762
763@item char *int_curr_symbol
764The international currency symbol for the selected locale.
765
766The value of @code{int_curr_symbol} should normally consist of a
767three-letter abbreviation determined by the international standard
768@cite{ISO 4217 Codes for the Representation of Currency and Funds},
769followed by a one-character separator (often a space).
770
771In the standard @samp{C} locale, this member has a value of @code{""}
772(the empty string), meaning ``unspecified''. We recommend you simply print
773the empty string as you would print any other string pointed to by this
774variable.
775
776@item char p_cs_precedes
777@itemx char n_cs_precedes
778@itemx char int_p_cs_precedes
779@itemx char int_n_cs_precedes
780These members are @code{1} if the @code{currency_symbol} or
781@code{int_curr_symbol} strings should precede the value of a monetary
782amount, or @code{0} if the strings should follow the value. The
783@code{p_cs_precedes} and @code{int_p_cs_precedes} members apply to
784positive amounts (or zero), and the @code{n_cs_precedes} and
785@code{int_n_cs_precedes} members apply to negative amounts.
786
787In the standard @samp{C} locale, all of these members have a value of
788@code{CHAR_MAX}, meaning ``unspecified''. The ISO standard doesn't say
789what to do when you find this value. We recommend printing the
790currency symbol before the amount, which is right for most countries.
791In other words, treat all nonzero values alike in these members.
792
793The members with the @code{int_} prefix apply to the
794@code{int_curr_symbol} while the other two apply to
795@code{currency_symbol}.
796
797@item char p_sep_by_space
798@itemx char n_sep_by_space
799@itemx char int_p_sep_by_space
800@itemx char int_n_sep_by_space
801These members are @code{1} if a space should appear between the
802@code{currency_symbol} or @code{int_curr_symbol} strings and the
803amount, or @code{0} if no space should appear. The
804@code{p_sep_by_space} and @code{int_p_sep_by_space} members apply to
805positive amounts (or zero), and the @code{n_sep_by_space} and
806@code{int_n_sep_by_space} members apply to negative amounts.
807
808In the standard @samp{C} locale, all of these members have a value of
809@code{CHAR_MAX}, meaning ``unspecified''. The ISO standard doesn't say
810what you should do when you find this value; we suggest you treat it as
8111 (print a space). In other words, treat all nonzero values alike in
812these members.
813
814The members with the @code{int_} prefix apply to the
815@code{int_curr_symbol} while the other two apply to
816@code{currency_symbol}. There is one specialty with the
817@code{int_curr_symbol}, though. Since all legal values contain a space
818at the end the string one either printf this space (if the currency
819symbol must appear in front and must be separated) or one has to avoid
820printing this character at all (especially when at the end of the
821string).
822@end table
823
824@node Sign of Money Amount, , Currency Symbol, The Lame Way to Locale Data
825@subsubsection Printing the Sign of a Monetary Amount
826
827These members of the @code{struct lconv} structure specify how to print
828the sign (if any) of a monetary value.
829
830@table @code
831@item char *positive_sign
832@itemx char *negative_sign
833These are strings used to indicate positive (or zero) and negative
834monetary quantities, respectively.
835
836In the standard @samp{C} locale, both of these members have a value of
837@code{""} (the empty string), meaning ``unspecified''.
838
839The ISO standard doesn't say what to do when you find this value; we
840recommend printing @code{positive_sign} as you find it, even if it is
841empty. For a negative value, print @code{negative_sign} as you find it
842unless both it and @code{positive_sign} are empty, in which case print
843@samp{-} instead. (Failing to indicate the sign at all seems rather
844unreasonable.)
845
846@item char p_sign_posn
847@itemx char n_sign_posn
848@itemx char int_p_sign_posn
849@itemx char int_n_sign_posn
850These members are small integers that indicate how to
851position the sign for nonnegative and negative monetary quantities,
852respectively. (The string used by the sign is what was specified with
853@code{positive_sign} or @code{negative_sign}.) The possible values are
854as follows:
855
856@table @code
857@item 0
858The currency symbol and quantity should be surrounded by parentheses.
859
860@item 1
861Print the sign string before the quantity and currency symbol.
862
863@item 2
864Print the sign string after the quantity and currency symbol.
865
866@item 3
867Print the sign string right before the currency symbol.
868
869@item 4
870Print the sign string right after the currency symbol.
871
872@item CHAR_MAX
873``Unspecified''. Both members have this value in the standard
874@samp{C} locale.
875@end table
876
877The ISO standard doesn't say what you should do when the value is
878@code{CHAR_MAX}. We recommend you print the sign after the currency
879symbol.
880
881The members with the @code{int_} prefix apply to the
882@code{int_curr_symbol} while the other two apply to
883@code{currency_symbol}.
884@end table
885
886@node The Elegant and Fast Way, , The Lame Way to Locale Data, Locale Information
887@subsection Pinpoint Access to Locale Data
888
889When writing the X/Open Portability Guide the authors realized that the
890@code{localeconv} function is not enough to provide reasonable access to
891locale information. The information which was meant to be available
892in the locale (as later specified in the POSIX.1 standard) requires more
893ways to access it. Therefore the @code{nl_langinfo} function
894was introduced.
895
896@comment langinfo.h
897@comment XOPEN
898@deftypefun {char *} nl_langinfo (nl_item @var{item})
899@safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
900@c It calls _nl_langinfo_l with the current locale, which returns a
901@c pointer into constant strings defined in locale data structures.
902The @code{nl_langinfo} function can be used to access individual
903elements of the locale categories. Unlike the @code{localeconv}
904function, which returns all the information, @code{nl_langinfo}
905lets the caller select what information it requires. This is very
906fast and it is not a problem to call this function multiple times.
907
908A second advantage is that in addition to the numeric and monetary
909formatting information, information from the
910@code{LC_TIME} and @code{LC_MESSAGES} categories is available.
911
912@pindex langinfo.h
913The type @code{nl_type} is defined in @file{nl_types.h}. The argument
914@var{item} is a numeric value defined in the header @file{langinfo.h}.
915The X/Open standard defines the following values:
916
917@vtable @code
918@item CODESET
919@code{nl_langinfo} returns a string with the name of the coded character
920set used in the selected locale.
921
922@item ABDAY_1
923@itemx ABDAY_2
924@itemx ABDAY_3
925@itemx ABDAY_4
926@itemx ABDAY_5
927@itemx ABDAY_6
928@itemx ABDAY_7
929@code{nl_langinfo} returns the abbreviated weekday name. @code{ABDAY_1}
930corresponds to Sunday.
931@item DAY_1
932@itemx DAY_2
933@itemx DAY_3
934@itemx DAY_4
935@itemx DAY_5
936@itemx DAY_6
937@itemx DAY_7
938Similar to @code{ABDAY_1} etc., but here the return value is the
939unabbreviated weekday name.
940@item ABMON_1
941@itemx ABMON_2
942@itemx ABMON_3
943@itemx ABMON_4
944@itemx ABMON_5
945@itemx ABMON_6
946@itemx ABMON_7
947@itemx ABMON_8
948@itemx ABMON_9
949@itemx ABMON_10
950@itemx ABMON_11
951@itemx ABMON_12
952The return value is abbreviated name of the month. @code{ABMON_1}
953corresponds to January.
954@item MON_1
955@itemx MON_2
956@itemx MON_3
957@itemx MON_4
958@itemx MON_5
959@itemx MON_6
960@itemx MON_7
961@itemx MON_8
962@itemx MON_9
963@itemx MON_10
964@itemx MON_11
965@itemx MON_12
966Similar to @code{ABMON_1} etc., but here the month names are not abbreviated.
967Here the first value @code{MON_1} also corresponds to January.
968@item AM_STR
969@itemx PM_STR
970The return values are strings which can be used in the representation of time
971as an hour from 1 to 12 plus an am/pm specifier.
972
973Note that in locales which do not use this time representation
974these strings might be empty, in which case the am/pm format
975cannot be used at all.
976@item D_T_FMT
977The return value can be used as a format string for @code{strftime} to
978represent time and date in a locale-specific way.
979@item D_FMT
980The return value can be used as a format string for @code{strftime} to
981represent a date in a locale-specific way.
982@item T_FMT
983The return value can be used as a format string for @code{strftime} to
984represent time in a locale-specific way.
985@item T_FMT_AMPM
986The return value can be used as a format string for @code{strftime} to
987represent time in the am/pm format.
988
989Note that if the am/pm format does not make any sense for the
990selected locale, the return value might be the same as the one for
991@code{T_FMT}.
992@item ERA
993The return value represents the era used in the current locale.
994
995Most locales do not define this value. An example of a locale which
996does define this value is the Japanese one. In Japan, the traditional
997representation of dates includes the name of the era corresponding to
998the then-emperor's reign.
999
1000Normally it should not be necessary to use this value directly.
1001Specifying the @code{E} modifier in their format strings causes the
1002@code{strftime} functions to use this information. The format of the
1003returned string is not specified, and therefore you should not assume
1004knowledge of it on different systems.
1005@item ERA_YEAR
1006The return value gives the year in the relevant era of the locale.
1007As for @code{ERA} it should not be necessary to use this value directly.
1008@item ERA_D_T_FMT
1009This return value can be used as a format string for @code{strftime} to
1010represent dates and times in a locale-specific era-based way.
1011@item ERA_D_FMT
1012This return value can be used as a format string for @code{strftime} to
1013represent a date in a locale-specific era-based way.
1014@item ERA_T_FMT
1015This return value can be used as a format string for @code{strftime} to
1016represent time in a locale-specific era-based way.
1017@item ALT_DIGITS
1018The return value is a representation of up to @math{100} values used to
1019represent the values @math{0} to @math{99}. As for @code{ERA} this
1020value is not intended to be used directly, but instead indirectly
1021through the @code{strftime} function. When the modifier @code{O} is
1022used in a format which would otherwise use numerals to represent hours,
1023minutes, seconds, weekdays, months, or weeks, the appropriate value for
1024the locale is used instead.
1025@item INT_CURR_SYMBOL
1026The same as the value returned by @code{localeconv} in the
1027@code{int_curr_symbol} element of the @code{struct lconv}.
1028@item CURRENCY_SYMBOL
1029@itemx CRNCYSTR
1030The same as the value returned by @code{localeconv} in the
1031@code{currency_symbol} element of the @code{struct lconv}.
1032
1033@code{CRNCYSTR} is a deprecated alias still required by Unix98.
1034@item MON_DECIMAL_POINT
1035The same as the value returned by @code{localeconv} in the
1036@code{mon_decimal_point} element of the @code{struct lconv}.
1037@item MON_THOUSANDS_SEP
1038The same as the value returned by @code{localeconv} in the
1039@code{mon_thousands_sep} element of the @code{struct lconv}.
1040@item MON_GROUPING
1041The same as the value returned by @code{localeconv} in the
1042@code{mon_grouping} element of the @code{struct lconv}.
1043@item POSITIVE_SIGN
1044The same as the value returned by @code{localeconv} in the
1045@code{positive_sign} element of the @code{struct lconv}.
1046@item NEGATIVE_SIGN
1047The same as the value returned by @code{localeconv} in the
1048@code{negative_sign} element of the @code{struct lconv}.
1049@item INT_FRAC_DIGITS
1050The same as the value returned by @code{localeconv} in the
1051@code{int_frac_digits} element of the @code{struct lconv}.
1052@item FRAC_DIGITS
1053The same as the value returned by @code{localeconv} in the
1054@code{frac_digits} element of the @code{struct lconv}.
1055@item P_CS_PRECEDES
1056The same as the value returned by @code{localeconv} in the
1057@code{p_cs_precedes} element of the @code{struct lconv}.
1058@item P_SEP_BY_SPACE
1059The same as the value returned by @code{localeconv} in the
1060@code{p_sep_by_space} element of the @code{struct lconv}.
1061@item N_CS_PRECEDES
1062The same as the value returned by @code{localeconv} in the
1063@code{n_cs_precedes} element of the @code{struct lconv}.
1064@item N_SEP_BY_SPACE
1065The same as the value returned by @code{localeconv} in the
1066@code{n_sep_by_space} element of the @code{struct lconv}.
1067@item P_SIGN_POSN
1068The same as the value returned by @code{localeconv} in the
1069@code{p_sign_posn} element of the @code{struct lconv}.
1070@item N_SIGN_POSN
1071The same as the value returned by @code{localeconv} in the
1072@code{n_sign_posn} element of the @code{struct lconv}.
1073
1074@item INT_P_CS_PRECEDES
1075The same as the value returned by @code{localeconv} in the
1076@code{int_p_cs_precedes} element of the @code{struct lconv}.
1077@item INT_P_SEP_BY_SPACE
1078The same as the value returned by @code{localeconv} in the
1079@code{int_p_sep_by_space} element of the @code{struct lconv}.
1080@item INT_N_CS_PRECEDES
1081The same as the value returned by @code{localeconv} in the
1082@code{int_n_cs_precedes} element of the @code{struct lconv}.
1083@item INT_N_SEP_BY_SPACE
1084The same as the value returned by @code{localeconv} in the
1085@code{int_n_sep_by_space} element of the @code{struct lconv}.
1086@item INT_P_SIGN_POSN
1087The same as the value returned by @code{localeconv} in the
1088@code{int_p_sign_posn} element of the @code{struct lconv}.
1089@item INT_N_SIGN_POSN
1090The same as the value returned by @code{localeconv} in the
1091@code{int_n_sign_posn} element of the @code{struct lconv}.
1092
1093@item DECIMAL_POINT
1094@itemx RADIXCHAR
1095The same as the value returned by @code{localeconv} in the
1096@code{decimal_point} element of the @code{struct lconv}.
1097
1098The name @code{RADIXCHAR} is a deprecated alias still used in Unix98.
1099@item THOUSANDS_SEP
1100@itemx THOUSEP
1101The same as the value returned by @code{localeconv} in the
1102@code{thousands_sep} element of the @code{struct lconv}.
1103
1104The name @code{THOUSEP} is a deprecated alias still used in Unix98.
1105@item GROUPING
1106The same as the value returned by @code{localeconv} in the
1107@code{grouping} element of the @code{struct lconv}.
1108@item YESEXPR
1109The return value is a regular expression which can be used with the
1110@code{regex} function to recognize a positive response to a yes/no
1111question. @Theglibc{} provides the @code{rpmatch} function for
1112easier handling in applications.
1113@item NOEXPR
1114The return value is a regular expression which can be used with the
1115@code{regex} function to recognize a negative response to a yes/no
1116question.
1117@item YESSTR
1118The return value is a locale-specific translation of the positive response
1119to a yes/no question.
1120
1121Using this value is deprecated since it is a very special case of
1122message translation, and is better handled by the message
1123translation functions (@pxref{Message Translation}).
1124
1125The use of this symbol is deprecated. Instead message translation
1126should be used.
1127@item NOSTR
1128The return value is a locale-specific translation of the negative response
1129to a yes/no question. What is said for @code{YESSTR} is also true here.
1130
1131The use of this symbol is deprecated. Instead message translation
1132should be used.
1133@end vtable
1134
1135The file @file{langinfo.h} defines a lot more symbols but none of them
1136is official. Using them is not portable, and the format of the
1137return values might change. Therefore we recommended you not use
1138them.
1139
1140Note that the return value for any valid argument can be used for
1141in all situations (with the possible exception of the am/pm time formatting
1142codes). If the user has not selected any locale for the
1143appropriate category, @code{nl_langinfo} returns the information from the
1144@code{"C"} locale. It is therefore possible to use this function as
1145shown in the example below.
1146
1147If the argument @var{item} is not valid, a pointer to an empty string is
1148returned.
1149@end deftypefun
1150
1151An example of @code{nl_langinfo} usage is a function which has to
1152print a given date and time in a locale-specific way. At first one
1153might think that, since @code{strftime} internally uses the locale
1154information, writing something like the following is enough:
1155
1156@smallexample
1157size_t
1158i18n_time_n_data (char *s, size_t len, const struct tm *tp)
1159@{
1160 return strftime (s, len, "%X %D", tp);
1161@}
1162@end smallexample
1163
1164The format contains no weekday or month names and therefore is
1165internationally usable. Wrong! The output produced is something like
1166@code{"hh:mm:ss MM/DD/YY"}. This format is only recognizable in the
1167USA. Other countries use different formats. Therefore the function
1168should be rewritten like this:
1169
1170@smallexample
1171size_t
1172i18n_time_n_data (char *s, size_t len, const struct tm *tp)
1173@{
1174 return strftime (s, len, nl_langinfo (D_T_FMT), tp);
1175@}
1176@end smallexample
1177
1178Now it uses the date and time format of the locale
1179selected when the program runs. If the user selects the locale
1180correctly there should never be a misunderstanding over the time and
1181date format.
1182
1183@node Formatting Numbers, Yes-or-No Questions, Locale Information, Locales
1184@section A dedicated function to format numbers
1185
1186We have seen that the structure returned by @code{localeconv} as well as
1187the values given to @code{nl_langinfo} allow you to retrieve the various
1188pieces of locale-specific information to format numbers and monetary
1189amounts. We have also seen that the underlying rules are quite complex.
1190
1191Therefore the X/Open standards introduce a function which uses such
1192locale information, making it easier for the user to format
1193numbers according to these rules.
1194
1195@deftypefun ssize_t strfmon (char *@var{s}, size_t @var{maxsize}, const char *@var{format}, @dots{})
1196@safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{}}}
1197@c It (and strfmon_l) both call vstrfmon_l, which, besides accessing the
1198@c locale object passed to it, accesses the active locale through
1199@c isdigit (but to_digit assumes ASCII digits only). It may call
1200@c __printf_fp (@mtslocale @ascuheap @acsmem) and guess_grouping (safe).
1201The @code{strfmon} function is similar to the @code{strftime} function
1202in that it takes a buffer, its size, a format string,
1203and values to write into the buffer as text in a form specified
1204by the format string. Like @code{strftime}, the function
1205also returns the number of bytes written into the buffer.
1206
1207There are two differences: @code{strfmon} can take more than one
1208argument, and, of course, the format specification is different. Like
1209@code{strftime}, the format string consists of normal text, which is
1210output as is, and format specifiers, which are indicated by a @samp{%}.
1211Immediately after the @samp{%}, you can optionally specify various flags
1212and formatting information before the main formatting character, in a
1213similar way to @code{printf}:
1214
1215@itemize @bullet
1216@item
1217Immediately following the @samp{%} there can be one or more of the
1218following flags:
1219@table @asis
1220@item @samp{=@var{f}}
1221The single byte character @var{f} is used for this field as the numeric
1222fill character. By default this character is a space character.
1223Filling with this character is only performed if a left precision
1224is specified. It is not just to fill to the given field width.
1225@item @samp{^}
1226The number is printed without grouping the digits according to the rules
1227of the current locale. By default grouping is enabled.
1228@item @samp{+}, @samp{(}
1229At most one of these flags can be used. They select which format to
1230represent the sign of a currency amount. By default, and if
1231@samp{+} is given, the locale equivalent of @math{+}/@math{-} is used. If
1232@samp{(} is given, negative amounts are enclosed in parentheses. The
1233exact format is determined by the values of the @code{LC_MONETARY}
1234category of the locale selected at program runtime.
1235@item @samp{!}
1236The output will not contain the currency symbol.
1237@item @samp{-}
1238The output will be formatted left-justified instead of right-justified if
1239it does not fill the entire field width.
1240@end table
1241@end itemize
1242
1243The next part of a specification is an optional field width. If no
1244width is specified @math{0} is taken. During output, the function first
1245determines how much space is required. If it requires at least as many
1246characters as given by the field width, it is output using as much space
1247as necessary. Otherwise, it is extended to use the full width by
1248filling with the space character. The presence or absence of the
1249@samp{-} flag determines the side at which such padding occurs. If
1250present, the spaces are added at the right making the output
1251left-justified, and vice versa.
1252
1253So far the format looks familiar, being similar to the @code{printf} and
1254@code{strftime} formats. However, the next two optional fields
1255introduce something new. The first one is a @samp{#} character followed
1256by a decimal digit string. The value of the digit string specifies the
1257number of @emph{digit} positions to the left of the decimal point (or
1258equivalent). This does @emph{not} include the grouping character when
1259the @samp{^} flag is not given. If the space needed to print the number
1260does not fill the whole width, the field is padded at the left side with
1261the fill character, which can be selected using the @samp{=} flag and by
1262default is a space. For example, if the field width is selected as 6
1263and the number is @math{123}, the fill character is @samp{*} the result
1264will be @samp{***123}.
1265
1266The second optional field starts with a @samp{.} (period) and consists
1267of another decimal digit string. Its value describes the number of
1268characters printed after the decimal point. The default is selected
1269from the current locale (@code{frac_digits}, @code{int_frac_digits}, see
1270@pxref{General Numeric}). If the exact representation needs more digits
1271than given by the field width, the displayed value is rounded. If the
1272number of fractional digits is selected to be zero, no decimal point is
1273printed.
1274
1275As a GNU extension, the @code{strfmon} implementation in @theglibc{}
1276allows an optional @samp{L} next as a format modifier. If this modifier
1277is given, the argument is expected to be a @code{long double} instead of
1278a @code{double} value.
1279
1280Finally, the last component is a format specifier. There are three
1281specifiers defined:
1282
1283@table @asis
1284@item @samp{i}
1285Use the locale's rules for formatting an international currency value.
1286@item @samp{n}
1287Use the locale's rules for formatting a national currency value.
1288@item @samp{%}
1289Place a @samp{%} in the output. There must be no flag, width
1290specifier or modifier given, only @samp{%%} is allowed.
1291@end table
1292
1293As for @code{printf}, the function reads the format string
1294from left to right and uses the values passed to the function following
1295the format string. The values are expected to be either of type
1296@code{double} or @code{long double}, depending on the presence of the
1297modifier @samp{L}. The result is stored in the buffer pointed to by
1298@var{s}. At most @var{maxsize} characters are stored.
1299
1300The return value of the function is the number of characters stored in
1301@var{s}, including the terminating @code{NULL} byte. If the number of
1302characters stored would exceed @var{maxsize}, the function returns
1303@math{-1} and the content of the buffer @var{s} is unspecified. In this
1304case @code{errno} is set to @code{E2BIG}.
1305@end deftypefun
1306
1307A few examples should make clear how the function works. It is
1308assumed that all the following pieces of code are executed in a program
1309which uses the USA locale (@code{en_US}). The simplest
1310form of the format is this:
1311
1312@smallexample
1313strfmon (buf, 100, "@@%n@@%n@@%n@@", 123.45, -567.89, 12345.678);
1314@end smallexample
1315
1316@noindent
1317The output produced is
1318@smallexample
1319"@@$123.45@@-$567.89@@$12,345.68@@"
1320@end smallexample
1321
1322We can notice several things here. First, the widths of the output
1323numbers are different. We have not specified a width in the format
1324string, and so this is no wonder. Second, the third number is printed
1325using thousands separators. The thousands separator for the
1326@code{en_US} locale is a comma. The number is also rounded.
1327@math{.678} is rounded to @math{.68} since the format does not specify a
1328precision and the default value in the locale is @math{2}. Finally,
1329note that the national currency symbol is printed since @samp{%n} was
1330used, not @samp{i}. The next example shows how we can align the output.
1331
1332@smallexample
1333strfmon (buf, 100, "@@%=*11n@@%=*11n@@%=*11n@@", 123.45, -567.89, 12345.678);
1334@end smallexample
1335
1336@noindent
1337The output this time is:
1338
1339@smallexample
1340"@@ $123.45@@ -$567.89@@ $12,345.68@@"
1341@end smallexample
1342
1343Two things stand out. Firstly, all fields have the same width (eleven
1344characters) since this is the width given in the format and since no
1345number required more characters to be printed. The second important
1346point is that the fill character is not used. This is correct since the
1347white space was not used to achieve a precision given by a @samp{#}
1348modifier, but instead to fill to the given width. The difference
1349becomes obvious if we now add a width specification.
1350
1351@smallexample
1352strfmon (buf, 100, "@@%=*11#5n@@%=*11#5n@@%=*11#5n@@",
1353 123.45, -567.89, 12345.678);
1354@end smallexample
1355
1356@noindent
1357The output is
1358
1359@smallexample
1360"@@ $***123.45@@-$***567.89@@ $12,456.68@@"
1361@end smallexample
1362
1363Here we can see that all the currency symbols are now aligned, and that
1364the space between the currency sign and the number is filled with the
1365selected fill character. Note that although the width is selected to be
1366@math{5} and @math{123.45} has three digits left of the decimal point,
1367the space is filled with three asterisks. This is correct since, as
1368explained above, the width does not include the positions used to store
1369thousands separators. One last example should explain the remaining
1370functionality.
1371
1372@smallexample
1373strfmon (buf, 100, "@@%=0(16#5.3i@@%=0(16#5.3i@@%=0(16#5.3i@@",
1374 123.45, -567.89, 12345.678);
1375@end smallexample
1376
1377@noindent
1378This rather complex format string produces the following output:
1379
1380@smallexample
1381"@@ USD 000123,450 @@(USD 000567.890)@@ USD 12,345.678 @@"
1382@end smallexample
1383
1384The most noticeable change is the alternative way of representing
1385negative numbers. In financial circles this is often done using
1386parentheses, and this is what the @samp{(} flag selected. The fill
1387character is now @samp{0}. Note that this @samp{0} character is not
1388regarded as a numeric zero, and therefore the first and second numbers
1389are not printed using a thousands separator. Since we used the format
1390specifier @samp{i} instead of @samp{n}, the international form of the
1391currency symbol is used. This is a four letter string, in this case
1392@code{"USD "}. The last point is that since the precision right of the
1393decimal point is selected to be three, the first and second numbers are
1394printed with an extra zero at the end and the third number is printed
1395without rounding.
1396
1397@node Yes-or-No Questions, , Formatting Numbers , Locales
1398@section Yes-or-No Questions
1399
1400Some non GUI programs ask a yes-or-no question. If the messages
1401(especially the questions) are translated into foreign languages, be
1402sure that you localize the answers too. It would be very bad habit to
1403ask a question in one language and request the answer in another, often
1404English.
1405
1406@Theglibc{} contains @code{rpmatch} to give applications easy
1407access to the corresponding locale definitions.
1408
1409@comment GNU
1410@comment stdlib.h
1411@deftypefun int rpmatch (const char *@var{response})
1412@safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@asucorrupt{} @ascuheap{} @asulock{} @ascudlopen{}}@acunsafe{@acucorrupt{} @aculock{} @acsmem{} @acsfd{}}}
1413@c Calls nl_langinfo with YESEXPR and NOEXPR, triggering @mtslocale but
1414@c it's regcomp and regexec that bring in all of the safety issues.
1415@c regfree is also called, but it doesn't introduce any further issues.
1416The function @code{rpmatch} checks the string in @var{response} whether
1417or not it is a correct yes-or-no answer and if yes, which one. The
1418check uses the @code{YESEXPR} and @code{NOEXPR} data in the
1419@code{LC_MESSAGES} category of the currently selected locale. The
1420return value is as follows:
1421
1422@table @code
1423@item 1
1424The user entered an affirmative answer.
1425
1426@item 0
1427The user entered a negative answer.
1428
1429@item -1
1430The answer matched neither the @code{YESEXPR} nor the @code{NOEXPR}
1431regular expression.
1432@end table
1433
1434This function is not standardized but available beside in @theglibc{} at
1435least also in the IBM AIX library.
1436@end deftypefun
1437
1438@noindent
1439This function would normally be used like this:
1440
1441@smallexample
1442 @dots{}
1443 /* @r{Use a safe default.} */
1444 _Bool doit = false;
1445
1446 fputs (gettext ("Do you really want to do this? "), stdout);
1447 fflush (stdout);
1448 /* @r{Prepare the @code{getline} call.} */
1449 line = NULL;
1450 len = 0;
1451 while (getline (&line, &len, stdin) >= 0)
1452 @{
1453 /* @r{Check the response.} */
1454 int res = rpmatch (line);
1455 if (res >= 0)
1456 @{
1457 /* @r{We got a definitive answer.} */
1458 if (res > 0)
1459 doit = true;
1460 break;
1461 @}
1462 @}
1463 /* @r{Free what @code{getline} allocated.} */
1464 free (line);
1465@end smallexample
1466
1467Note that the loop continues until a read error is detected or until a
1468definitive (positive or negative) answer is read.