Blame manual/locale.texi

Packit Service 82fcde
@node Locales, Message Translation, Character Set Handling, Top
Packit Service 82fcde
@c %MENU% The country and language can affect the behavior of library functions
Packit Service 82fcde
@chapter Locales and Internationalization
Packit Service 82fcde
Packit Service 82fcde
Different countries and cultures have varying conventions for how to
Packit Service 82fcde
communicate.  These conventions range from very simple ones, such as the
Packit Service 82fcde
format for representing dates and times, to very complex ones, such as
Packit Service 82fcde
the language spoken.
Packit Service 82fcde
Packit Service 82fcde
@cindex internationalization
Packit Service 82fcde
@cindex locales
Packit Service 82fcde
@dfn{Internationalization} of software means programming it to be able
Packit Service 82fcde
to adapt to the user's favorite conventions.  In @w{ISO C},
Packit Service 82fcde
internationalization works by means of @dfn{locales}.  Each locale
Packit Service 82fcde
specifies a collection of conventions, one convention for each purpose.
Packit Service 82fcde
The user chooses a set of conventions by specifying a locale (via
Packit Service 82fcde
environment variables).
Packit Service 82fcde
Packit Service 82fcde
All programs inherit the chosen locale as part of their environment.
Packit Service 82fcde
Provided the programs are written to obey the choice of locale, they
Packit Service 82fcde
will follow the conventions preferred by the user.
Packit Service 82fcde
Packit Service 82fcde
@menu
Packit Service 82fcde
* Effects of Locale::           Actions affected by the choice of
Packit Service 82fcde
                                 locale.
Packit Service 82fcde
* Choosing Locale::             How the user specifies a locale.
Packit Service 82fcde
* Locale Categories::           Different purposes for which you can
Packit Service 82fcde
                                 select a locale.
Packit Service 82fcde
* Setting the Locale::          How a program specifies the locale
Packit Service 82fcde
                                 with library functions.
Packit Service 82fcde
* Standard Locales::            Locale names available on all systems.
Packit Service 82fcde
* Locale Names::                Format of system-specific locale names.
Packit Service 82fcde
* Locale Information::          How to access the information for the locale.
Packit Service 82fcde
* Formatting Numbers::          A dedicated function to format numbers.
Packit Service 82fcde
* Yes-or-No Questions::         Check a Response against the locale.
Packit Service 82fcde
@end menu
Packit Service 82fcde
Packit Service 82fcde
@node Effects of Locale, Choosing Locale,  , Locales
Packit Service 82fcde
@section What Effects a Locale Has
Packit Service 82fcde
Packit Service 82fcde
Each locale specifies conventions for several purposes, including the
Packit Service 82fcde
following:
Packit Service 82fcde
Packit Service 82fcde
@itemize @bullet
Packit Service 82fcde
@item
Packit Service 82fcde
What multibyte character sequences are valid, and how they are
Packit Service 82fcde
interpreted (@pxref{Character Set Handling}).
Packit Service 82fcde
Packit Service 82fcde
@item
Packit Service 82fcde
Classification of which characters in the local character set are
Packit Service 82fcde
considered alphabetic, and upper- and lower-case conversion conventions
Packit Service 82fcde
(@pxref{Character Handling}).
Packit Service 82fcde
Packit Service 82fcde
@item
Packit Service 82fcde
The collating sequence for the local language and character set
Packit Service 82fcde
(@pxref{Collation Functions}).
Packit Service 82fcde
Packit Service 82fcde
@item
Packit Service 82fcde
Formatting of numbers and currency amounts (@pxref{General Numeric}).
Packit Service 82fcde
Packit Service 82fcde
@item
Packit Service 82fcde
Formatting of dates and times (@pxref{Formatting Calendar Time}).
Packit Service 82fcde
Packit Service 82fcde
@item
Packit Service 82fcde
What language to use for output, including error messages
Packit Service 82fcde
(@pxref{Message Translation}).
Packit Service 82fcde
Packit Service 82fcde
@item
Packit Service 82fcde
What language to use for user answers to yes-or-no questions
Packit Service 82fcde
(@pxref{Yes-or-No Questions}).
Packit Service 82fcde
Packit Service 82fcde
@item
Packit Service 82fcde
What language to use for more complex user input.
Packit Service 82fcde
(The C library doesn't yet help you implement this.)
Packit Service 82fcde
@end itemize
Packit Service 82fcde
Packit Service 82fcde
Some aspects of adapting to the specified locale are handled
Packit Service 82fcde
automatically by the library subroutines.  For example, all your program
Packit Service 82fcde
needs to do in order to use the collating sequence of the chosen locale
Packit Service 82fcde
is to use @code{strcoll} or @code{strxfrm} to compare strings.
Packit Service 82fcde
Packit Service 82fcde
Other aspects of locales are beyond the comprehension of the library.
Packit Service 82fcde
For example, the library can't automatically translate your program's
Packit Service 82fcde
output messages into other languages.  The only way you can support
Packit Service 82fcde
output in the user's favorite language is to program this more or less
Packit Service 82fcde
by hand.  The C library provides functions to handle translations for
Packit Service 82fcde
multiple languages easily.
Packit Service 82fcde
Packit Service 82fcde
This chapter discusses the mechanism by which you can modify the current
Packit Service 82fcde
locale.  The effects of the current locale on specific library functions
Packit Service 82fcde
are discussed in more detail in the descriptions of those functions.
Packit Service 82fcde
Packit Service 82fcde
@node Choosing Locale, Locale Categories, Effects of Locale, Locales
Packit Service 82fcde
@section Choosing a Locale
Packit Service 82fcde
Packit Service 82fcde
The simplest way for the user to choose a locale is to set the
Packit Service 82fcde
environment variable @code{LANG}.  This specifies a single locale to use
Packit Service 82fcde
for all purposes.  For example, a user could specify a hypothetical
Packit Service 82fcde
locale named @samp{espana-castellano} to use the standard conventions of
Packit Service 82fcde
most of Spain.
Packit Service 82fcde
Packit Service 82fcde
The set of locales supported depends on the operating system you are
Packit Service 82fcde
using, and so do their names, except that the standard locale called
Packit Service 82fcde
@samp{C} or @samp{POSIX} always exist.  @xref{Locale Names}.
Packit Service 82fcde
Packit Service 82fcde
In order to force the system to always use the default locale, the
Packit Service 82fcde
user can set the @code{LC_ALL} environment variable to @samp{C}.
Packit Service 82fcde
Packit Service 82fcde
@cindex combining locales
Packit Service 82fcde
A user also has the option of specifying different locales for
Packit Service 82fcde
different purposes---in effect, choosing a mixture of multiple
Packit Service 82fcde
locales.  @xref{Locale Categories}.
Packit Service 82fcde
Packit Service 82fcde
For example, the user might specify the locale @samp{espana-castellano}
Packit Service 82fcde
for most purposes, but specify the locale @samp{usa-english} for
Packit Service 82fcde
currency formatting.  This might make sense if the user is a
Packit Service 82fcde
Spanish-speaking American, working in Spanish, but representing monetary
Packit Service 82fcde
amounts in US dollars.
Packit Service 82fcde
Packit Service 82fcde
Note that both locales @samp{espana-castellano} and @samp{usa-english},
Packit Service 82fcde
like all locales, would include conventions for all of the purposes to
Packit Service 82fcde
which locales apply.  However, the user can choose to use each locale
Packit Service 82fcde
for a particular subset of those purposes.
Packit Service 82fcde
Packit Service 82fcde
@node Locale Categories, Setting the Locale, Choosing Locale, Locales
Packit Service 82fcde
@section Locale Categories
Packit Service 82fcde
@cindex categories for locales
Packit Service 82fcde
@cindex locale categories
Packit Service 82fcde
Packit Service 82fcde
The purposes that locales serve are grouped into @dfn{categories}, so
Packit Service 82fcde
that a user or a program can choose the locale for each category
Packit Service 82fcde
independently.  Here is a table of categories; each name is both an
Packit Service 82fcde
environment variable that a user can set, and a macro name that you can
Packit Service 82fcde
use as the first argument to @code{setlocale}.
Packit Service 82fcde
Packit Service 82fcde
The contents of the environment variable (or the string in the second
Packit Service 82fcde
argument to @code{setlocale}) has to be a valid locale name.
Packit Service 82fcde
@xref{Locale Names}.
Packit Service 82fcde
Packit Service 82fcde
@vtable @code
Packit Service 82fcde
@item LC_COLLATE
Packit Service 82fcde
@standards{ISO, locale.h}
Packit Service 82fcde
This category applies to collation of strings (functions @code{strcoll}
Packit Service 82fcde
and @code{strxfrm}); see @ref{Collation Functions}.
Packit Service 82fcde
Packit Service 82fcde
@item LC_CTYPE
Packit Service 82fcde
@standards{ISO, locale.h}
Packit Service 82fcde
This category applies to classification and conversion of characters,
Packit Service 82fcde
and to multibyte and wide characters;
Packit Service 82fcde
see @ref{Character Handling}, and @ref{Character Set Handling}.
Packit Service 82fcde
Packit Service 82fcde
@item LC_MONETARY
Packit Service 82fcde
@standards{ISO, locale.h}
Packit Service 82fcde
This category applies to formatting monetary values; see @ref{General Numeric}.
Packit Service 82fcde
Packit Service 82fcde
@item LC_NUMERIC
Packit Service 82fcde
@standards{ISO, locale.h}
Packit Service 82fcde
This category applies to formatting numeric values that are not
Packit Service 82fcde
monetary; see @ref{General Numeric}.
Packit Service 82fcde
Packit Service 82fcde
@item LC_TIME
Packit Service 82fcde
@standards{ISO, locale.h}
Packit Service 82fcde
This category applies to formatting date and time values; see
Packit Service 82fcde
@ref{Formatting Calendar Time}.
Packit Service 82fcde
Packit Service 82fcde
@item LC_MESSAGES
Packit Service 82fcde
@standards{XOPEN, locale.h}
Packit Service 82fcde
This category applies to selecting the language used in the user
Packit Service 82fcde
interface for message translation (@pxref{The Uniforum approach};
Packit Service 82fcde
@pxref{Message catalogs a la X/Open})  and contains regular expressions
Packit Service 82fcde
for affirmative and negative responses.
Packit Service 82fcde
Packit Service 82fcde
@item LC_ALL
Packit Service 82fcde
@standards{ISO, locale.h}
Packit Service 82fcde
This is not a category; it is only a macro that you can use
Packit Service 82fcde
with @code{setlocale} to set a single locale for all purposes.  Setting
Packit Service 82fcde
this environment variable overwrites all selections by the other
Packit Service 82fcde
@code{LC_*} variables or @code{LANG}.
Packit Service 82fcde
Packit Service 82fcde
@item LANG
Packit Service 82fcde
@standards{ISO, locale.h}
Packit Service 82fcde
If this environment variable is defined, its value specifies the locale
Packit Service 82fcde
to use for all purposes except as overridden by the variables above.
Packit Service 82fcde
@end vtable
Packit Service 82fcde
Packit Service 82fcde
@vindex LANGUAGE
Packit Service 82fcde
When developing the message translation functions it was felt that the
Packit Service 82fcde
functionality provided by the variables above is not sufficient.  For
Packit Service 82fcde
example, it should be possible to specify more than one locale name.
Packit Service 82fcde
Take a Swedish user who better speaks German than English, and a program
Packit Service 82fcde
whose messages are output in English by default.  It should be possible
Packit Service 82fcde
to specify that the first choice of language is Swedish, the second
Packit Service 82fcde
German, and if this also fails to use English.  This is
Packit Service 82fcde
possible with the variable @code{LANGUAGE}.  For further description of
Packit Service 82fcde
this GNU extension see @ref{Using gettextized software}.
Packit Service 82fcde
Packit Service 82fcde
@node Setting the Locale, Standard Locales, Locale Categories, Locales
Packit Service 82fcde
@section How Programs Set the Locale
Packit Service 82fcde
Packit Service 82fcde
A C program inherits its locale environment variables when it starts up.
Packit Service 82fcde
This happens automatically.  However, these variables do not
Packit Service 82fcde
automatically control the locale used by the library functions, because
Packit Service 82fcde
@w{ISO C} says that all programs start by default in the standard @samp{C}
Packit Service 82fcde
locale.  To use the locales specified by the environment, you must call
Packit Service 82fcde
@code{setlocale}.  Call it as follows:
Packit Service 82fcde
Packit Service 82fcde
@smallexample
Packit Service 82fcde
setlocale (LC_ALL, "");
Packit Service 82fcde
@end smallexample
Packit Service 82fcde
Packit Service 82fcde
@noindent
Packit Service 82fcde
to select a locale based on the user choice of the appropriate
Packit Service 82fcde
environment variables.
Packit Service 82fcde
Packit Service 82fcde
@cindex changing the locale
Packit Service 82fcde
@cindex locale, changing
Packit Service 82fcde
You can also use @code{setlocale} to specify a particular locale, for
Packit Service 82fcde
general use or for a specific category.
Packit Service 82fcde
Packit Service 82fcde
@pindex locale.h
Packit Service 82fcde
The symbols in this section are defined in the header file @file{locale.h}.
Packit Service 82fcde
Packit Service 82fcde
@deftypefun {char *} setlocale (int @var{category}, const char *@var{locale})
Packit Service 82fcde
@standards{ISO, locale.h}
Packit Service 82fcde
@safety{@prelim{}@mtunsafe{@mtasuconst{:@mtslocale{}} @mtsenv{}}@asunsafe{@asuinit{} @asulock{} @ascuheap{} @asucorrupt{}}@acunsafe{@acuinit{} @acucorrupt{} @aculock{} @acsmem{} @acsfd{}}}
Packit Service 82fcde
@c Uses of the global locale object are unguarded in functions that
Packit Service 82fcde
@c ought to be MT-Safe, so we're ruling out the use of this function
Packit Service 82fcde
@c once threads are started.  It takes a write lock itself, but it may
Packit Service 82fcde
@c return a pointer loaded from the global locale object after releasing
Packit Service 82fcde
@c the lock, or before taking it.
Packit Service 82fcde
@c setlocale @mtasuconst:@mtslocale @mtsenv @asuinit @ascuheap @asulock @asucorrupt @acucorrupt @acsmem @acsfd @aculock
Packit Service 82fcde
@c  libc_rwlock_wrlock @asulock @aculock
Packit Service 82fcde
@c  libc_rwlock_unlock @aculock
Packit Service 82fcde
@c  getenv LOCPATH @mtsenv
Packit Service 82fcde
@c  malloc @ascuheap @acsmem
Packit Service 82fcde
@c  free @ascuheap @acsmem
Packit Service 82fcde
@c  new_composite_name ok
Packit Service 82fcde
@c  setdata ok
Packit Service 82fcde
@c  setname ok
Packit Service 82fcde
@c  _nl_find_locale @mtsenv @asuinit @ascuheap @asulock @asucorrupt @acucorrupt @acsmem @acsfd @aculock
Packit Service 82fcde
@c   getenv LC_ALL and LANG @mtsenv
Packit Service 82fcde
@c   _nl_load_locale_from_archive @ascuheap @acucorrupt @acsmem @acsfd
Packit Service 82fcde
@c    sysconf _SC_PAGE_SIZE ok
Packit Service 82fcde
@c    _nl_normalize_codeset @ascuheap @acsmem
Packit Service 82fcde
@c     isalnum_l ok (C locale)
Packit Service 82fcde
@c     isdigit_l ok (C locale)
Packit Service 82fcde
@c     malloc @ascuheap @acsmem
Packit Service 82fcde
@c     tolower_l ok (C locale)
Packit Service 82fcde
@c    open_not_cancel_2 @acsfd
Packit Service 82fcde
@c    fxstat64 ok
Packit Service 82fcde
@c    close_not_cancel_no_status ok
Packit Service 82fcde
@c    __mmap64 @acsmem
Packit Service 82fcde
@c    calculate_head_size ok
Packit Service 82fcde
@c    __munmap ok
Packit Service 82fcde
@c    compute_hashval ok
Packit Service 82fcde
@c    qsort dup @acucorrupt
Packit Service 82fcde
@c     rangecmp ok
Packit Service 82fcde
@c    malloc @ascuheap @acsmem
Packit Service 82fcde
@c    strdup @ascuheap @acsmem
Packit Service 82fcde
@c    _nl_intern_locale_data @ascuheap @acsmem
Packit Service 82fcde
@c     malloc @ascuheap @acsmem
Packit Service 82fcde
@c     free @ascuheap @acsmem
Packit Service 82fcde
@c   _nl_expand_alias @ascuheap @asulock @acsmem @acsfd @aculock
Packit Service 82fcde
@c    libc_lock_lock @asulock @aculock
Packit Service 82fcde
@c    bsearch ok
Packit Service 82fcde
@c     alias_compare ok
Packit Service 82fcde
@c      strcasecmp ok
Packit Service 82fcde
@c    read_alias_file @ascuheap @asulock @acsmem @acsfd @aculock
Packit Service 82fcde
@c     fopen @ascuheap @asulock @acsmem @acsfd @aculock
Packit Service 82fcde
@c     fsetlocking ok
Packit Service 82fcde
@c     feof_unlocked ok
Packit Service 82fcde
@c     fgets_unlocked ok
Packit Service 82fcde
@c     isspace ok (locale mutex is locked)
Packit Service 82fcde
@c     extend_alias_table @ascuheap @acsmem
Packit Service 82fcde
@c      realloc @ascuheap @acsmem
Packit Service 82fcde
@c     realloc @ascuheap @acsmem
Packit Service 82fcde
@c     fclose @ascuheap @asulock @acsmem @acsfd @aculock
Packit Service 82fcde
@c     qsort @ascuheap @acsmem
Packit Service 82fcde
@c      alias_compare dup
Packit Service 82fcde
@c    libc_lock_unlock @aculock
Packit Service 82fcde
@c   _nl_explode_name @ascuheap @acsmem
Packit Service 82fcde
@c    _nl_find_language ok
Packit Service 82fcde
@c    _nl_normalize_codeset dup @ascuheap @acsmem
Packit Service 82fcde
@c   _nl_make_l10nflist @ascuheap @acsmem
Packit Service 82fcde
@c    malloc @ascuheap @acsmem
Packit Service 82fcde
@c    free @ascuheap @acsmem
Packit Service 82fcde
@c    __argz_stringify ok
Packit Service 82fcde
@c    __argz_count ok
Packit Service 82fcde
@c    __argz_next ok
Packit Service 82fcde
@c   _nl_load_locale @ascuheap @acsmem @acsfd
Packit Service 82fcde
@c    open_not_cancel_2 @acsfd
Packit Service 82fcde
@c    __fxstat64 ok
Packit Service 82fcde
@c    close_not_cancel_no_status ok
Packit Service 82fcde
@c    mmap @acsmem
Packit Service 82fcde
@c    malloc @ascuheap @acsmem
Packit Service 82fcde
@c    read_not_cancel ok
Packit Service 82fcde
@c    free @ascuheap @acsmem
Packit Service 82fcde
@c    _nl_intern_locale_data dup @ascuheap @acsmem
Packit Service 82fcde
@c    munmap ok
Packit Service 82fcde
@c   __gconv_compare_alias @asuinit @ascuheap @asucorrupt @asulock @acsmem@acucorrupt @acsfd @aculock
Packit Service 82fcde
@c    __gconv_read_conf @asuinit @ascuheap @asucorrupt @asulock @acsmem@acucorrupt @acsfd @aculock
Packit Service 82fcde
@c     (libc_once-initializes gconv_cache and gconv_path_envvar; they're
Packit Service 82fcde
@c      never modified afterwards)
Packit Service 82fcde
@c     __gconv_load_cache @ascuheap @acsmem @acsfd
Packit Service 82fcde
@c      getenv GCONV_PATH @mtsenv
Packit Service 82fcde
@c      open_not_cancel @acsfd
Packit Service 82fcde
@c      __fxstat64 ok
Packit Service 82fcde
@c      close_not_cancel_no_status ok
Packit Service 82fcde
@c      mmap @acsmem
Packit Service 82fcde
@c      malloc @ascuheap @acsmem
Packit Service 82fcde
@c      __read ok
Packit Service 82fcde
@c      free @ascuheap @acsmem
Packit Service 82fcde
@c      munmap ok
Packit Service 82fcde
@c     __gconv_get_path @asulock @ascuheap @aculock @acsmem @acsfd
Packit Service 82fcde
@c      getcwd @ascuheap @acsmem @acsfd
Packit Service 82fcde
@c      libc_lock_lock @asulock @aculock
Packit Service 82fcde
@c      malloc @ascuheap @acsmem
Packit Service 82fcde
@c      strtok_r ok
Packit Service 82fcde
@c      libc_lock_unlock @aculock
Packit Service 82fcde
@c     read_conf_file @ascuheap @asucorrupt @asulock @acsmem @acucorrupt @acsfd @aculock
Packit Service 82fcde
@c      fopen @ascuheap @asulock @acsmem @acsfd @aculock
Packit Service 82fcde
@c      fsetlocking ok
Packit Service 82fcde
@c      feof_unlocked ok
Packit Service 82fcde
@c      getdelim @ascuheap @asucorrupt @acsmem @acucorrupt
Packit Service 82fcde
@c      isspace_l ok (C locale)
Packit Service 82fcde
@c      add_alias
Packit Service 82fcde
@c       isspace_l ok (C locale)
Packit Service 82fcde
@c       toupper_l ok (C locale)
Packit Service 82fcde
@c       add_alias2 dup @ascuheap @acucorrupt @acsmem
Packit Service 82fcde
@c      add_module @ascuheap @acsmem
Packit Service 82fcde
@c       isspace_l ok (C locale)
Packit Service 82fcde
@c       toupper_l ok (C locale)
Packit Service 82fcde
@c       strtol ok (@mtslocale but we hold the locale lock)
Packit Service 82fcde
@c       tfind __gconv_alias_db ok
Packit Service 82fcde
@c        __gconv_alias_compare dup ok
Packit Service 82fcde
@c       calloc @ascuheap @acsmem
Packit Service 82fcde
@c       insert_module dup @ascuheap
Packit Service 82fcde
@c     __tfind ok (because the tree is read only by then)
Packit Service 82fcde
@c      __gconv_alias_compare dup ok
Packit Service 82fcde
@c     insert_module @ascuheap
Packit Service 82fcde
@c      free @ascuheap
Packit Service 82fcde
@c     add_alias2 @ascuheap @acucorrupt @acsmem
Packit Service 82fcde
@c      detect_conflict ok, reads __gconv_modules_db
Packit Service 82fcde
@c      malloc @ascuheap @acsmem
Packit Service 82fcde
@c      tsearch __gconv_alias_db @ascuheap @acucorrupt @acsmem [exclusive tree, no @mtsrace]
Packit Service 82fcde
@c       __gconv_alias_compare ok
Packit Service 82fcde
@c      free @ascuheap
Packit Service 82fcde
@c    __gconv_compare_alias_cache ok
Packit Service 82fcde
@c     find_module_idx ok
Packit Service 82fcde
@c    do_lookup_alias ok
Packit Service 82fcde
@c     __tfind ok (because the tree is read only by then)
Packit Service 82fcde
@c      __gconv_alias_compare ok
Packit Service 82fcde
@c   strndup @ascuheap @acsmem
Packit Service 82fcde
@c   strcasecmp_l ok (C locale)
Packit Service 82fcde
The function @code{setlocale} sets the current locale for category
Packit Service 82fcde
@var{category} to @var{locale}.
Packit Service 82fcde
Packit Service 82fcde
If @var{category} is @code{LC_ALL}, this specifies the locale for all
Packit Service 82fcde
purposes.  The other possible values of @var{category} specify a
Packit Service 82fcde
single purpose (@pxref{Locale Categories}).
Packit Service 82fcde
Packit Service 82fcde
You can also use this function to find out the current locale by passing
Packit Service 82fcde
a null pointer as the @var{locale} argument.  In this case,
Packit Service 82fcde
@code{setlocale} returns a string that is the name of the locale
Packit Service 82fcde
currently selected for category @var{category}.
Packit Service 82fcde
Packit Service 82fcde
The string returned by @code{setlocale} can be overwritten by subsequent
Packit Service 82fcde
calls, so you should make a copy of the string (@pxref{Copying Strings
Packit Service 82fcde
and Arrays}) if you want to save it past any further calls to
Packit Service 82fcde
@code{setlocale}.  (The standard library is guaranteed never to call
Packit Service 82fcde
@code{setlocale} itself.)
Packit Service 82fcde
Packit Service 82fcde
You should not modify the string returned by @code{setlocale}.  It might
Packit Service 82fcde
be the same string that was passed as an argument in a previous call to
Packit Service 82fcde
@code{setlocale}.  One requirement is that the @var{category} must be
Packit Service 82fcde
the same in the call the string was returned and the one when the string
Packit Service 82fcde
is passed in as @var{locale} parameter.
Packit Service 82fcde
Packit Service 82fcde
When you read the current locale for category @code{LC_ALL}, the value
Packit Service 82fcde
encodes the entire combination of selected locales for all categories.
Packit Service 82fcde
If you specify the same ``locale name'' with @code{LC_ALL} in a
Packit Service 82fcde
subsequent call to @code{setlocale}, it restores the same combination
Packit Service 82fcde
of locale selections.
Packit Service 82fcde
Packit Service 82fcde
To be sure you can use the returned string encoding the currently selected
Packit Service 82fcde
locale at a later time, you must make a copy of the string.  It is not
Packit Service 82fcde
guaranteed that the returned pointer remains valid over time.
Packit Service 82fcde
Packit Service 82fcde
When the @var{locale} argument is not a null pointer, the string returned
Packit Service 82fcde
by @code{setlocale} reflects the newly-modified locale.
Packit Service 82fcde
Packit Service 82fcde
If you specify an empty string for @var{locale}, this means to read the
Packit Service 82fcde
appropriate environment variable and use its value to select the locale
Packit Service 82fcde
for @var{category}.
Packit Service 82fcde
Packit Service 82fcde
If a nonempty string is given for @var{locale}, then the locale of that
Packit Service 82fcde
name is used if possible.
Packit Service 82fcde
Packit Service 82fcde
The effective locale name (either the second argument to
Packit Service 82fcde
@code{setlocale}, or if the argument is an empty string, the name
Packit Service 82fcde
obtained from the process environment) must be a valid locale name.
Packit Service 82fcde
@xref{Locale Names}.
Packit Service 82fcde
Packit Service 82fcde
If you specify an invalid locale name, @code{setlocale} returns a null
Packit Service 82fcde
pointer and leaves the current locale unchanged.
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
Here is an example showing how you might use @code{setlocale} to
Packit Service 82fcde
temporarily switch to a new locale.
Packit Service 82fcde
Packit Service 82fcde
@smallexample
Packit Service 82fcde
#include <stddef.h>
Packit Service 82fcde
#include <locale.h>
Packit Service 82fcde
#include <stdlib.h>
Packit Service 82fcde
#include <string.h>
Packit Service 82fcde
Packit Service 82fcde
void
Packit Service 82fcde
with_other_locale (char *new_locale,
Packit Service 82fcde
                   void (*subroutine) (int),
Packit Service 82fcde
                   int argument)
Packit Service 82fcde
@{
Packit Service 82fcde
  char *old_locale, *saved_locale;
Packit Service 82fcde
Packit Service 82fcde
  /* @r{Get the name of the current locale.}  */
Packit Service 82fcde
  old_locale = setlocale (LC_ALL, NULL);
Packit Service 82fcde
Packit Service 82fcde
  /* @r{Copy the name so it won't be clobbered by @code{setlocale}.} */
Packit Service 82fcde
  saved_locale = strdup (old_locale);
Packit Service 82fcde
  if (saved_locale == NULL)
Packit Service 82fcde
    fatal ("Out of memory");
Packit Service 82fcde
Packit Service 82fcde
  /* @r{Now change the locale and do some stuff with it.} */
Packit Service 82fcde
  setlocale (LC_ALL, new_locale);
Packit Service 82fcde
  (*subroutine) (argument);
Packit Service 82fcde
Packit Service 82fcde
  /* @r{Restore the original locale.} */
Packit Service 82fcde
  setlocale (LC_ALL, saved_locale);
Packit Service 82fcde
  free (saved_locale);
Packit Service 82fcde
@}
Packit Service 82fcde
@end smallexample
Packit Service 82fcde
Packit Service 82fcde
@strong{Portability Note:} Some @w{ISO C} systems may define additional
Packit Service 82fcde
locale categories, and future versions of the library will do so.  For
Packit Service 82fcde
portability, assume that any symbol beginning with @samp{LC_} might be
Packit Service 82fcde
defined in @file{locale.h}.
Packit Service 82fcde
Packit Service 82fcde
@node Standard Locales, Locale Names, Setting the Locale, Locales
Packit Service 82fcde
@section Standard Locales
Packit Service 82fcde
Packit Service 82fcde
The only locale names you can count on finding on all operating systems
Packit Service 82fcde
are these three standard ones:
Packit Service 82fcde
Packit Service 82fcde
@table @code
Packit Service 82fcde
@item "C"
Packit Service 82fcde
This is the standard C locale.  The attributes and behavior it provides
Packit Service 82fcde
are specified in the @w{ISO C} standard.  When your program starts up, it
Packit Service 82fcde
initially uses this locale by default.
Packit Service 82fcde
Packit Service 82fcde
@item "POSIX"
Packit Service 82fcde
This is the standard POSIX locale.  Currently, it is an alias for the
Packit Service 82fcde
standard C locale.
Packit Service 82fcde
Packit Service 82fcde
@item ""
Packit Service 82fcde
The empty name says to select a locale based on environment variables.
Packit Service 82fcde
@xref{Locale Categories}.
Packit Service 82fcde
@end table
Packit Service 82fcde
Packit Service 82fcde
Defining and installing named locales is normally a responsibility of
Packit Service 82fcde
the system administrator at your site (or the person who installed
Packit Service 82fcde
@theglibc{}).  It is also possible for the user to create private
Packit Service 82fcde
locales.  All this will be discussed later when describing the tool to
Packit Service 82fcde
do so.
Packit Service 82fcde
@comment (@pxref{Building Locale Files}).
Packit Service 82fcde
Packit Service 82fcde
If your program needs to use something other than the @samp{C} locale,
Packit Service 82fcde
it will be more portable if you use whatever locale the user specifies
Packit Service 82fcde
with the environment, rather than trying to specify some non-standard
Packit Service 82fcde
locale explicitly by name.  Remember, different machines might have
Packit Service 82fcde
different sets of locales installed.
Packit Service 82fcde
Packit Service 82fcde
@node Locale Names, Locale Information, Standard Locales, Locales
Packit Service 82fcde
@section Locale Names
Packit Service 82fcde
Packit Service 82fcde
The following command prints a list of locales supported by the
Packit Service 82fcde
system:
Packit Service 82fcde
Packit Service 82fcde
@pindex locale
Packit Service 82fcde
@smallexample
Packit Service 82fcde
  locale -a
Packit Service 82fcde
@end smallexample
Packit Service 82fcde
Packit Service 82fcde
@strong{Portability Note:} With the notable exception of the standard
Packit Service 82fcde
locale names @samp{C} and @samp{POSIX}, locale names are
Packit Service 82fcde
system-specific.
Packit Service 82fcde
Packit Service 82fcde
Most locale names follow XPG syntax and consist of up to four parts:
Packit Service 82fcde
Packit Service 82fcde
@smallexample
Packit Service 82fcde
@var{language}[_@var{territory}[.@var{codeset}]][@@@var{modifier}]
Packit Service 82fcde
@end smallexample
Packit Service 82fcde
Packit Service 82fcde
Beside the first part, all of them are allowed to be missing.  If the
Packit Service 82fcde
full specified locale is not found, less specific ones are looked for.
Packit Service 82fcde
The various parts will be stripped off, in the following order:
Packit Service 82fcde
Packit Service 82fcde
@enumerate
Packit Service 82fcde
@item
Packit Service 82fcde
codeset
Packit Service 82fcde
@item
Packit Service 82fcde
normalized codeset
Packit Service 82fcde
@item
Packit Service 82fcde
territory
Packit Service 82fcde
@item
Packit Service 82fcde
modifier
Packit Service 82fcde
@end enumerate
Packit Service 82fcde
Packit Service 82fcde
For example, the locale name @samp{de_AT.iso885915@@euro} denotes a
Packit Service 82fcde
German-language locale for use in Austria, using the ISO-8859-15
Packit Service 82fcde
(Latin-9) character set, and with the Euro as the currency symbol.
Packit Service 82fcde
Packit Service 82fcde
In addition to locale names which follow XPG syntax, systems may
Packit Service 82fcde
provide aliases such as @samp{german}.  Both categories of names must
Packit Service 82fcde
not contain the slash character @samp{/}.
Packit Service 82fcde
Packit Service 82fcde
If the locale name starts with a slash @samp{/}, it is treated as a
Packit Service 82fcde
path relative to the configured locale directories; see @code{LOCPATH}
Packit Service 82fcde
below.  The specified path must not contain a component @samp{..}, or
Packit Service 82fcde
the name is invalid, and @code{setlocale} will fail.
Packit Service 82fcde
Packit Service 82fcde
@strong{Portability Note:} POSIX suggests that if a locale name starts
Packit Service 82fcde
with a slash @samp{/}, it is resolved as an absolute path.  However,
Packit Service 82fcde
@theglibc{} treats it as a relative path under the directories listed
Packit Service 82fcde
in @code{LOCPATH} (or the default locale directory if @code{LOCPATH}
Packit Service 82fcde
is unset).
Packit Service 82fcde
Packit Service 82fcde
Locale names which are longer than an implementation-defined limit are
Packit Service 82fcde
invalid and cause @code{setlocale} to fail.
Packit Service 82fcde
Packit Service 82fcde
As a special case, locale names used with @code{LC_ALL} can combine
Packit Service 82fcde
several locales, reflecting different locale settings for different
Packit Service 82fcde
categories.  For example, you might want to use a U.S. locale with ISO
Packit Service 82fcde
A4 paper format, so you set @code{LANG} to @samp{en_US.UTF-8}, and
Packit Service 82fcde
@code{LC_PAPER} to @samp{de_DE.UTF-8}.  In this case, the
Packit Service 82fcde
@code{LC_ALL}-style combined locale name is
Packit Service 82fcde
Packit Service 82fcde
@smallexample
Packit Service 82fcde
LC_CTYPE=en_US.UTF-8;LC_TIME=en_US.UTF-8;LC_PAPER=de_DE.UTF-8;@dots{}
Packit Service 82fcde
@end smallexample
Packit Service 82fcde
Packit Service 82fcde
followed by other category settings not shown here.
Packit Service 82fcde
Packit Service 82fcde
@vindex LOCPATH
Packit Service 82fcde
The path used for finding locale data can be set using the
Packit Service 82fcde
@code{LOCPATH} environment variable.  This variable lists the
Packit Service 82fcde
directories in which to search for locale definitions, separated by a
Packit Service 82fcde
colon @samp{:}.
Packit Service 82fcde
Packit Service 82fcde
The default path for finding locale data is system specific.  A typical
Packit Service 82fcde
value for the @code{LOCPATH} default is:
Packit Service 82fcde
Packit Service 82fcde
@smallexample
Packit Service 82fcde
/usr/share/locale
Packit Service 82fcde
@end smallexample
Packit Service 82fcde
Packit Service 82fcde
The value of @code{LOCPATH} is ignored by privileged programs for
Packit Service 82fcde
security reasons, and only the default directory is used.
Packit Service 82fcde
Packit Service 82fcde
@node Locale Information, Formatting Numbers, Locale Names, Locales
Packit Service 82fcde
@section Accessing Locale Information
Packit Service 82fcde
Packit Service 82fcde
There are several ways to access locale information.  The simplest
Packit Service 82fcde
way is to let the C library itself do the work.  Several of the
Packit Service 82fcde
functions in this library implicitly access the locale data, and use
Packit Service 82fcde
what information is provided by the currently selected locale.  This is
Packit Service 82fcde
how the locale model is meant to work normally.
Packit Service 82fcde
Packit Service 82fcde
As an example take the @code{strftime} function, which is meant to nicely
Packit Service 82fcde
format date and time information (@pxref{Formatting Calendar Time}).
Packit Service 82fcde
Part of the standard information contained in the @code{LC_TIME}
Packit Service 82fcde
category is the names of the months.  Instead of requiring the
Packit Service 82fcde
programmer to take care of providing the translations the
Packit Service 82fcde
@code{strftime} function does this all by itself.  @code{%A}
Packit Service 82fcde
in the format string is replaced by the appropriate weekday
Packit Service 82fcde
name of the locale currently selected by @code{LC_TIME}.  This is an
Packit Service 82fcde
easy example, and wherever possible functions do things automatically
Packit Service 82fcde
in this way.
Packit Service 82fcde
Packit Service 82fcde
But there are quite often situations when there is simply no function
Packit Service 82fcde
to perform the task, or it is simply not possible to do the work
Packit Service 82fcde
automatically.  For these cases it is necessary to access the
Packit Service 82fcde
information in the locale directly.  To do this the C library provides
Packit Service 82fcde
two functions: @code{localeconv} and @code{nl_langinfo}.  The former is
Packit Service 82fcde
part of @w{ISO C} and therefore portable, but has a brain-damaged
Packit Service 82fcde
interface.  The second is part of the Unix interface and is portable in
Packit Service 82fcde
as far as the system follows the Unix standards.
Packit Service 82fcde
Packit Service 82fcde
@menu
Packit Service 82fcde
* The Lame Way to Locale Data::   ISO C's @code{localeconv}.
Packit Service 82fcde
* The Elegant and Fast Way::      X/Open's @code{nl_langinfo}.
Packit Service 82fcde
@end menu
Packit Service 82fcde
Packit Service 82fcde
@node The Lame Way to Locale Data, The Elegant and Fast Way, ,Locale Information
Packit Service 82fcde
@subsection @code{localeconv}: It is portable but @dots{}
Packit Service 82fcde
Packit Service 82fcde
Together with the @code{setlocale} function the @w{ISO C} people
Packit Service 82fcde
invented the @code{localeconv} function.  It is a masterpiece of poor
Packit Service 82fcde
design.  It is expensive to use, not extensible, and not generally
Packit Service 82fcde
usable as it provides access to only @code{LC_MONETARY} and
Packit Service 82fcde
@code{LC_NUMERIC} related information.  Nevertheless, if it is
Packit Service 82fcde
applicable to a given situation it should be used since it is very
Packit Service 82fcde
portable.  The function @code{strfmon} formats monetary amounts
Packit Service 82fcde
according to the selected locale using this information.
Packit Service 82fcde
@pindex locale.h
Packit Service 82fcde
@cindex monetary value formatting
Packit Service 82fcde
@cindex numeric value formatting
Packit Service 82fcde
Packit Service 82fcde
@deftypefun {struct lconv *} localeconv (void)
Packit Service 82fcde
@standards{ISO, locale.h}
Packit Service 82fcde
@safety{@prelim{}@mtunsafe{@mtasurace{:localeconv} @mtslocale{}}@asunsafe{}@acsafe{}}
Packit Service 82fcde
@c This function reads from multiple components of the locale object,
Packit Service 82fcde
@c without synchronization, while writing to the static buffer it uses
Packit Service 82fcde
@c as the return value.
Packit Service 82fcde
The @code{localeconv} function returns a pointer to a structure whose
Packit Service 82fcde
components contain information about how numeric and monetary values
Packit Service 82fcde
should be formatted in the current locale.
Packit Service 82fcde
Packit Service 82fcde
You should not modify the structure or its contents.  The structure might
Packit Service 82fcde
be overwritten by subsequent calls to @code{localeconv}, or by calls to
Packit Service 82fcde
@code{setlocale}, but no other function in the library overwrites this
Packit Service 82fcde
value.
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
@deftp {Data Type} {struct lconv}
Packit Service 82fcde
@standards{ISO, locale.h}
Packit Service 82fcde
@code{localeconv}'s return value is of this data type.  Its elements are
Packit Service 82fcde
described in the following subsections.
Packit Service 82fcde
@end deftp
Packit Service 82fcde
Packit Service 82fcde
If a member of the structure @code{struct lconv} has type @code{char},
Packit Service 82fcde
and the value is @code{CHAR_MAX}, it means that the current locale has
Packit Service 82fcde
no value for that parameter.
Packit Service 82fcde
Packit Service 82fcde
@menu
Packit Service 82fcde
* General Numeric::             Parameters for formatting numbers and
Packit Service 82fcde
                                 currency amounts.
Packit Service 82fcde
* Currency Symbol::             How to print the symbol that identifies an
Packit Service 82fcde
                                 amount of money (e.g. @samp{$}).
Packit Service 82fcde
* Sign of Money Amount::        How to print the (positive or negative) sign
Packit Service 82fcde
                                 for a monetary amount, if one exists.
Packit Service 82fcde
@end menu
Packit Service 82fcde
Packit Service 82fcde
@node General Numeric, Currency Symbol, , The Lame Way to Locale Data
Packit Service 82fcde
@subsubsection Generic Numeric Formatting Parameters
Packit Service 82fcde
Packit Service 82fcde
These are the standard members of @code{struct lconv}; there may be
Packit Service 82fcde
others.
Packit Service 82fcde
Packit Service 82fcde
@table @code
Packit Service 82fcde
@item char *decimal_point
Packit Service 82fcde
@itemx char *mon_decimal_point
Packit Service 82fcde
These are the decimal-point separators used in formatting non-monetary
Packit Service 82fcde
and monetary quantities, respectively.  In the @samp{C} locale, the
Packit Service 82fcde
value of @code{decimal_point} is @code{"."}, and the value of
Packit Service 82fcde
@code{mon_decimal_point} is @code{""}.
Packit Service 82fcde
@cindex decimal-point separator
Packit Service 82fcde
Packit Service 82fcde
@item char *thousands_sep
Packit Service 82fcde
@itemx char *mon_thousands_sep
Packit Service 82fcde
These are the separators used to delimit groups of digits to the left of
Packit Service 82fcde
the decimal point in formatting non-monetary and monetary quantities,
Packit Service 82fcde
respectively.  In the @samp{C} locale, both members have a value of
Packit Service 82fcde
@code{""} (the empty string).
Packit Service 82fcde
Packit Service 82fcde
@item char *grouping
Packit Service 82fcde
@itemx char *mon_grouping
Packit Service 82fcde
These are strings that specify how to group the digits to the left of
Packit Service 82fcde
the decimal point.  @code{grouping} applies to non-monetary quantities
Packit Service 82fcde
and @code{mon_grouping} applies to monetary quantities.  Use either
Packit Service 82fcde
@code{thousands_sep} or @code{mon_thousands_sep} to separate the digit
Packit Service 82fcde
groups.
Packit Service 82fcde
@cindex grouping of digits
Packit Service 82fcde
Packit Service 82fcde
Each member of these strings is to be interpreted as an integer value of
Packit Service 82fcde
type @code{char}.  Successive numbers (from left to right) give the
Packit Service 82fcde
sizes of successive groups (from right to left, starting at the decimal
Packit Service 82fcde
point.)  The last member is either @code{0}, in which case the previous
Packit Service 82fcde
member is used over and over again for all the remaining groups, or
Packit Service 82fcde
@code{CHAR_MAX}, in which case there is no more grouping---or, put
Packit Service 82fcde
another way, any remaining digits form one large group without
Packit Service 82fcde
separators.
Packit Service 82fcde
Packit Service 82fcde
For example, if @code{grouping} is @code{"\04\03\02"}, the correct
Packit Service 82fcde
grouping for the number @code{123456787654321} is @samp{12}, @samp{34},
Packit Service 82fcde
@samp{56}, @samp{78}, @samp{765}, @samp{4321}.  This uses a group of 4
Packit Service 82fcde
digits at the end, preceded by a group of 3 digits, preceded by groups
Packit Service 82fcde
of 2 digits (as many as needed).  With a separator of @samp{,}, the
Packit Service 82fcde
number would be printed as @samp{12,34,56,78,765,4321}.
Packit Service 82fcde
Packit Service 82fcde
A value of @code{"\03"} indicates repeated groups of three digits, as
Packit Service 82fcde
normally used in the U.S.
Packit Service 82fcde
Packit Service 82fcde
In the standard @samp{C} locale, both @code{grouping} and
Packit Service 82fcde
@code{mon_grouping} have a value of @code{""}.  This value specifies no
Packit Service 82fcde
grouping at all.
Packit Service 82fcde
Packit Service 82fcde
@item char int_frac_digits
Packit Service 82fcde
@itemx char frac_digits
Packit Service 82fcde
These are small integers indicating how many fractional digits (to the
Packit Service 82fcde
right of the decimal point) should be displayed in a monetary value in
Packit Service 82fcde
international and local formats, respectively.  (Most often, both
Packit Service 82fcde
members have the same value.)
Packit Service 82fcde
Packit Service 82fcde
In the standard @samp{C} locale, both of these members have the value
Packit Service 82fcde
@code{CHAR_MAX}, meaning ``unspecified''.  The ISO standard doesn't say
Packit Service 82fcde
what to do when you find this value; we recommend printing no
Packit Service 82fcde
fractional digits.  (This locale also specifies the empty string for
Packit Service 82fcde
@code{mon_decimal_point}, so printing any fractional digits would be
Packit Service 82fcde
confusing!)
Packit Service 82fcde
@end table
Packit Service 82fcde
Packit Service 82fcde
@node Currency Symbol, Sign of Money Amount, General Numeric, The Lame Way to Locale Data
Packit Service 82fcde
@subsubsection Printing the Currency Symbol
Packit Service 82fcde
@cindex currency symbols
Packit Service 82fcde
Packit Service 82fcde
These members of the @code{struct lconv} structure specify how to print
Packit Service 82fcde
the symbol to identify a monetary value---the international analog of
Packit Service 82fcde
@samp{$} for US dollars.
Packit Service 82fcde
Packit Service 82fcde
Each country has two standard currency symbols.  The @dfn{local currency
Packit Service 82fcde
symbol} is used commonly within the country, while the
Packit Service 82fcde
@dfn{international currency symbol} is used internationally to refer to
Packit Service 82fcde
that country's currency when it is necessary to indicate the country
Packit Service 82fcde
unambiguously.
Packit Service 82fcde
Packit Service 82fcde
For example, many countries use the dollar as their monetary unit, and
Packit Service 82fcde
when dealing with international currencies it's important to specify
Packit Service 82fcde
that one is dealing with (say) Canadian dollars instead of U.S. dollars
Packit Service 82fcde
or Australian dollars.  But when the context is known to be Canada,
Packit Service 82fcde
there is no need to make this explicit---dollar amounts are implicitly
Packit Service 82fcde
assumed to be in Canadian dollars.
Packit Service 82fcde
Packit Service 82fcde
@table @code
Packit Service 82fcde
@item char *currency_symbol
Packit Service 82fcde
The local currency symbol for the selected locale.
Packit Service 82fcde
Packit Service 82fcde
In the standard @samp{C} locale, this member has a value of @code{""}
Packit Service 82fcde
(the empty string), meaning ``unspecified''.  The ISO standard doesn't
Packit Service 82fcde
say what to do when you find this value; we recommend you simply print
Packit Service 82fcde
the empty string as you would print any other string pointed to by this
Packit Service 82fcde
variable.
Packit Service 82fcde
Packit Service 82fcde
@item char *int_curr_symbol
Packit Service 82fcde
The international currency symbol for the selected locale.
Packit Service 82fcde
Packit Service 82fcde
The value of @code{int_curr_symbol} should normally consist of a
Packit Service 82fcde
three-letter abbreviation determined by the international standard
Packit Service 82fcde
@cite{ISO 4217 Codes for the Representation of Currency and Funds},
Packit Service 82fcde
followed by a one-character separator (often a space).
Packit Service 82fcde
Packit Service 82fcde
In the standard @samp{C} locale, this member has a value of @code{""}
Packit Service 82fcde
(the empty string), meaning ``unspecified''.  We recommend you simply print
Packit Service 82fcde
the empty string as you would print any other string pointed to by this
Packit Service 82fcde
variable.
Packit Service 82fcde
Packit Service 82fcde
@item char p_cs_precedes
Packit Service 82fcde
@itemx char n_cs_precedes
Packit Service 82fcde
@itemx char int_p_cs_precedes
Packit Service 82fcde
@itemx char int_n_cs_precedes
Packit Service 82fcde
These members are @code{1} if the @code{currency_symbol} or
Packit Service 82fcde
@code{int_curr_symbol} strings should precede the value of a monetary
Packit Service 82fcde
amount, or @code{0} if the strings should follow the value.  The
Packit Service 82fcde
@code{p_cs_precedes} and @code{int_p_cs_precedes} members apply to
Packit Service 82fcde
positive amounts (or zero), and the @code{n_cs_precedes} and
Packit Service 82fcde
@code{int_n_cs_precedes} members apply to negative amounts.
Packit Service 82fcde
Packit Service 82fcde
In the standard @samp{C} locale, all of these members have a value of
Packit Service 82fcde
@code{CHAR_MAX}, meaning ``unspecified''.  The ISO standard doesn't say
Packit Service 82fcde
what to do when you find this value.  We recommend printing the
Packit Service 82fcde
currency symbol before the amount, which is right for most countries.
Packit Service 82fcde
In other words, treat all nonzero values alike in these members.
Packit Service 82fcde
Packit Service 82fcde
The members with the @code{int_} prefix apply to the
Packit Service 82fcde
@code{int_curr_symbol} while the other two apply to
Packit Service 82fcde
@code{currency_symbol}.
Packit Service 82fcde
Packit Service 82fcde
@item char p_sep_by_space
Packit Service 82fcde
@itemx char n_sep_by_space
Packit Service 82fcde
@itemx char int_p_sep_by_space
Packit Service 82fcde
@itemx char int_n_sep_by_space
Packit Service 82fcde
These members are @code{1} if a space should appear between the
Packit Service 82fcde
@code{currency_symbol} or @code{int_curr_symbol} strings and the
Packit Service 82fcde
amount, or @code{0} if no space should appear.  The
Packit Service 82fcde
@code{p_sep_by_space} and @code{int_p_sep_by_space} members apply to
Packit Service 82fcde
positive amounts (or zero), and the @code{n_sep_by_space} and
Packit Service 82fcde
@code{int_n_sep_by_space} members apply to negative amounts.
Packit Service 82fcde
Packit Service 82fcde
In the standard @samp{C} locale, all of these members have a value of
Packit Service 82fcde
@code{CHAR_MAX}, meaning ``unspecified''.  The ISO standard doesn't say
Packit Service 82fcde
what you should do when you find this value; we suggest you treat it as
Packit Service 82fcde
1 (print a space).  In other words, treat all nonzero values alike in
Packit Service 82fcde
these members.
Packit Service 82fcde
Packit Service 82fcde
The members with the @code{int_} prefix apply to the
Packit Service 82fcde
@code{int_curr_symbol} while the other two apply to
Packit Service 82fcde
@code{currency_symbol}.  There is one specialty with the
Packit Service 82fcde
@code{int_curr_symbol}, though.  Since all legal values contain a space
Packit Service 82fcde
at the end of the string one either prints this space (if the currency
Packit Service 82fcde
symbol must appear in front and must be separated) or one has to avoid
Packit Service 82fcde
printing this character at all (especially when at the end of the
Packit Service 82fcde
string).
Packit Service 82fcde
@end table
Packit Service 82fcde
Packit Service 82fcde
@node Sign of Money Amount, , Currency Symbol, The Lame Way to Locale Data
Packit Service 82fcde
@subsubsection Printing the Sign of a Monetary Amount
Packit Service 82fcde
Packit Service 82fcde
These members of the @code{struct lconv} structure specify how to print
Packit Service 82fcde
the sign (if any) of a monetary value.
Packit Service 82fcde
Packit Service 82fcde
@table @code
Packit Service 82fcde
@item char *positive_sign
Packit Service 82fcde
@itemx char *negative_sign
Packit Service 82fcde
These are strings used to indicate positive (or zero) and negative
Packit Service 82fcde
monetary quantities, respectively.
Packit Service 82fcde
Packit Service 82fcde
In the standard @samp{C} locale, both of these members have a value of
Packit Service 82fcde
@code{""} (the empty string), meaning ``unspecified''.
Packit Service 82fcde
Packit Service 82fcde
The ISO standard doesn't say what to do when you find this value; we
Packit Service 82fcde
recommend printing @code{positive_sign} as you find it, even if it is
Packit Service 82fcde
empty.  For a negative value, print @code{negative_sign} as you find it
Packit Service 82fcde
unless both it and @code{positive_sign} are empty, in which case print
Packit Service 82fcde
@samp{-} instead.  (Failing to indicate the sign at all seems rather
Packit Service 82fcde
unreasonable.)
Packit Service 82fcde
Packit Service 82fcde
@item char p_sign_posn
Packit Service 82fcde
@itemx char n_sign_posn
Packit Service 82fcde
@itemx char int_p_sign_posn
Packit Service 82fcde
@itemx char int_n_sign_posn
Packit Service 82fcde
These members are small integers that indicate how to
Packit Service 82fcde
position the sign for nonnegative and negative monetary quantities,
Packit Service 82fcde
respectively.  (The string used for the sign is what was specified with
Packit Service 82fcde
@code{positive_sign} or @code{negative_sign}.)  The possible values are
Packit Service 82fcde
as follows:
Packit Service 82fcde
Packit Service 82fcde
@table @code
Packit Service 82fcde
@item 0
Packit Service 82fcde
The currency symbol and quantity should be surrounded by parentheses.
Packit Service 82fcde
Packit Service 82fcde
@item 1
Packit Service 82fcde
Print the sign string before the quantity and currency symbol.
Packit Service 82fcde
Packit Service 82fcde
@item 2
Packit Service 82fcde
Print the sign string after the quantity and currency symbol.
Packit Service 82fcde
Packit Service 82fcde
@item 3
Packit Service 82fcde
Print the sign string right before the currency symbol.
Packit Service 82fcde
Packit Service 82fcde
@item 4
Packit Service 82fcde
Print the sign string right after the currency symbol.
Packit Service 82fcde
Packit Service 82fcde
@item CHAR_MAX
Packit Service 82fcde
``Unspecified''.  Both members have this value in the standard
Packit Service 82fcde
@samp{C} locale.
Packit Service 82fcde
@end table
Packit Service 82fcde
Packit Service 82fcde
The ISO standard doesn't say what you should do when the value is
Packit Service 82fcde
@code{CHAR_MAX}.  We recommend you print the sign after the currency
Packit Service 82fcde
symbol.
Packit Service 82fcde
Packit Service 82fcde
The members with the @code{int_} prefix apply to the
Packit Service 82fcde
@code{int_curr_symbol} while the other two apply to
Packit Service 82fcde
@code{currency_symbol}.
Packit Service 82fcde
@end table
Packit Service 82fcde
Packit Service 82fcde
@node The Elegant and Fast Way, , The Lame Way to Locale Data, Locale Information
Packit Service 82fcde
@subsection Pinpoint Access to Locale Data
Packit Service 82fcde
Packit Service 82fcde
When writing the X/Open Portability Guide the authors realized that the
Packit Service 82fcde
@code{localeconv} function is not enough to provide reasonable access to
Packit Service 82fcde
locale information.  The information which was meant to be available
Packit Service 82fcde
in the locale (as later specified in the POSIX.1 standard) requires more
Packit Service 82fcde
ways to access it.  Therefore the @code{nl_langinfo} function
Packit Service 82fcde
was introduced.
Packit Service 82fcde
Packit Service 82fcde
@deftypefun {char *} nl_langinfo (nl_item @var{item})
Packit Service 82fcde
@standards{XOPEN, langinfo.h}
Packit Service 82fcde
@safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
Packit Service 82fcde
@c It calls _nl_langinfo_l with the current locale, which returns a
Packit Service 82fcde
@c pointer into constant strings defined in locale data structures.
Packit Service 82fcde
The @code{nl_langinfo} function can be used to access individual
Packit Service 82fcde
elements of the locale categories.  Unlike the @code{localeconv}
Packit Service 82fcde
function, which returns all the information, @code{nl_langinfo}
Packit Service 82fcde
lets the caller select what information it requires.  This is very
Packit Service 82fcde
fast and it is not a problem to call this function multiple times.
Packit Service 82fcde
Packit Service 82fcde
A second advantage is that in addition to the numeric and monetary
Packit Service 82fcde
formatting information, information from the
Packit Service 82fcde
@code{LC_TIME} and @code{LC_MESSAGES} categories is available.
Packit Service 82fcde
Packit Service 82fcde
@pindex langinfo.h
Packit Service 82fcde
The type @code{nl_item} is defined in @file{nl_types.h}.  The argument
Packit Service 82fcde
@var{item} is a numeric value defined in the header @file{langinfo.h}.
Packit Service 82fcde
The X/Open standard defines the following values:
Packit Service 82fcde
Packit Service 82fcde
@vtable @code
Packit Service 82fcde
@item CODESET
Packit Service 82fcde
@code{nl_langinfo} returns a string with the name of the coded character
Packit Service 82fcde
set used in the selected locale.
Packit Service 82fcde
Packit Service 82fcde
@item ABDAY_1
Packit Service 82fcde
@itemx ABDAY_2
Packit Service 82fcde
@itemx ABDAY_3
Packit Service 82fcde
@itemx ABDAY_4
Packit Service 82fcde
@itemx ABDAY_5
Packit Service 82fcde
@itemx ABDAY_6
Packit Service 82fcde
@itemx ABDAY_7
Packit Service 82fcde
@code{nl_langinfo} returns the abbreviated weekday name.  @code{ABDAY_1}
Packit Service 82fcde
corresponds to Sunday.
Packit Service 82fcde
@item DAY_1
Packit Service 82fcde
@itemx DAY_2
Packit Service 82fcde
@itemx DAY_3
Packit Service 82fcde
@itemx DAY_4
Packit Service 82fcde
@itemx DAY_5
Packit Service 82fcde
@itemx DAY_6
Packit Service 82fcde
@itemx DAY_7
Packit Service 82fcde
Similar to @code{ABDAY_1}, etc.,@: but here the return value is the
Packit Service 82fcde
unabbreviated weekday name.
Packit Service 82fcde
@item ABMON_1
Packit Service 82fcde
@itemx ABMON_2
Packit Service 82fcde
@itemx ABMON_3
Packit Service 82fcde
@itemx ABMON_4
Packit Service 82fcde
@itemx ABMON_5
Packit Service 82fcde
@itemx ABMON_6
Packit Service 82fcde
@itemx ABMON_7
Packit Service 82fcde
@itemx ABMON_8
Packit Service 82fcde
@itemx ABMON_9
Packit Service 82fcde
@itemx ABMON_10
Packit Service 82fcde
@itemx ABMON_11
Packit Service 82fcde
@itemx ABMON_12
Packit Service 82fcde
The return value is the abbreviated name of the month, in the
Packit Service 82fcde
grammatical form used when the month forms part of a complete date.
Packit Service 82fcde
@code{ABMON_1} corresponds to January.
Packit Service 82fcde
@item MON_1
Packit Service 82fcde
@itemx MON_2
Packit Service 82fcde
@itemx MON_3
Packit Service 82fcde
@itemx MON_4
Packit Service 82fcde
@itemx MON_5
Packit Service 82fcde
@itemx MON_6
Packit Service 82fcde
@itemx MON_7
Packit Service 82fcde
@itemx MON_8
Packit Service 82fcde
@itemx MON_9
Packit Service 82fcde
@itemx MON_10
Packit Service 82fcde
@itemx MON_11
Packit Service 82fcde
@itemx MON_12
Packit Service 82fcde
Similar to @code{ABMON_1}, etc.,@: but here the month names are not
Packit Service 82fcde
abbreviated.  Here the first value @code{MON_1} also corresponds to
Packit Service 82fcde
January.
Packit Service 82fcde
@item ALTMON_1
Packit Service 82fcde
@itemx ALTMON_2
Packit Service 82fcde
@itemx ALTMON_3
Packit Service 82fcde
@itemx ALTMON_4
Packit Service 82fcde
@itemx ALTMON_5
Packit Service 82fcde
@itemx ALTMON_6
Packit Service 82fcde
@itemx ALTMON_7
Packit Service 82fcde
@itemx ALTMON_8
Packit Service 82fcde
@itemx ALTMON_9
Packit Service 82fcde
@itemx ALTMON_10
Packit Service 82fcde
@itemx ALTMON_11
Packit Service 82fcde
@itemx ALTMON_12
Packit Service 82fcde
Similar to @code{MON_1}, etc.,@: but here the month names are in the
Packit Service 82fcde
grammatical form used when the month is named by itself.  The
Packit Service 82fcde
@code{strftime} functions use these month names for the conversion
Packit Service 82fcde
specifier @code{%OB} (@pxref{Formatting Calendar Time}).
Packit Service 82fcde
Packit Service 82fcde
Note that not all languages need two different forms of the month names,
Packit Service 82fcde
so the strings returned for @code{MON_@dots{}} and @code{ALTMON_@dots{}}
Packit Service 82fcde
may or may not be the same, depending on the locale.
Packit Service 82fcde
Packit Service 82fcde
@strong{NB:} @code{ABALTMON_@dots{}} constants corresponding to the
Packit Service 82fcde
@code{%Ob} conversion specifier are not currently provided, but are
Packit Service 82fcde
expected to be in a future release.  In the meantime, it is possible
Packit Service 82fcde
to use @code{_NL_ABALTMON_@dots{}}.
Packit Service 82fcde
@item AM_STR
Packit Service 82fcde
@itemx PM_STR
Packit Service 82fcde
The return values are strings which can be used in the representation of time
Packit Service 82fcde
as an hour from 1 to 12 plus an am/pm specifier.
Packit Service 82fcde
Packit Service 82fcde
Note that in locales which do not use this time representation
Packit Service 82fcde
these strings might be empty, in which case the am/pm format
Packit Service 82fcde
cannot be used at all.
Packit Service 82fcde
@item D_T_FMT
Packit Service 82fcde
The return value can be used as a format string for @code{strftime} to
Packit Service 82fcde
represent time and date in a locale-specific way.
Packit Service 82fcde
@item D_FMT
Packit Service 82fcde
The return value can be used as a format string for @code{strftime} to
Packit Service 82fcde
represent a date in a locale-specific way.
Packit Service 82fcde
@item T_FMT
Packit Service 82fcde
The return value can be used as a format string for @code{strftime} to
Packit Service 82fcde
represent time in a locale-specific way.
Packit Service 82fcde
@item T_FMT_AMPM
Packit Service 82fcde
The return value can be used as a format string for @code{strftime} to
Packit Service 82fcde
represent time in the am/pm format.
Packit Service 82fcde
Packit Service 82fcde
Note that if the am/pm format does not make any sense for the
Packit Service 82fcde
selected locale, the return value might be the same as the one for
Packit Service 82fcde
@code{T_FMT}.
Packit Service 82fcde
@item ERA
Packit Service 82fcde
The return value represents the era used in the current locale.
Packit Service 82fcde
Packit Service 82fcde
Most locales do not define this value.  An example of a locale which
Packit Service 82fcde
does define this value is the Japanese one.  In Japan, the traditional
Packit Service 82fcde
representation of dates includes the name of the era corresponding to
Packit Service 82fcde
the then-emperor's reign.
Packit Service 82fcde
Packit Service 82fcde
Normally it should not be necessary to use this value directly.
Packit Service 82fcde
Specifying the @code{E} modifier in their format strings causes the
Packit Service 82fcde
@code{strftime} functions to use this information.  The format of the
Packit Service 82fcde
returned string is not specified, and therefore you should not assume
Packit Service 82fcde
knowledge of it on different systems.
Packit Service 82fcde
@item ERA_YEAR
Packit Service 82fcde
The return value gives the year in the relevant era of the locale.
Packit Service 82fcde
As for @code{ERA} it should not be necessary to use this value directly.
Packit Service 82fcde
@item ERA_D_T_FMT
Packit Service 82fcde
This return value can be used as a format string for @code{strftime} to
Packit Service 82fcde
represent dates and times in a locale-specific era-based way.
Packit Service 82fcde
@item ERA_D_FMT
Packit Service 82fcde
This return value can be used as a format string for @code{strftime} to
Packit Service 82fcde
represent a date in a locale-specific era-based way.
Packit Service 82fcde
@item ERA_T_FMT
Packit Service 82fcde
This return value can be used as a format string for @code{strftime} to
Packit Service 82fcde
represent time in a locale-specific era-based way.
Packit Service 82fcde
@item ALT_DIGITS
Packit Service 82fcde
The return value is a representation of up to @math{100} values used to
Packit Service 82fcde
represent the values @math{0} to @math{99}.  As for @code{ERA} this
Packit Service 82fcde
value is not intended to be used directly, but instead indirectly
Packit Service 82fcde
through the @code{strftime} function.  When the modifier @code{O} is
Packit Service 82fcde
used in a format which would otherwise use numerals to represent hours,
Packit Service 82fcde
minutes, seconds, weekdays, months, or weeks, the appropriate value for
Packit Service 82fcde
the locale is used instead.
Packit Service 82fcde
@item INT_CURR_SYMBOL
Packit Service 82fcde
The same as the value returned by @code{localeconv} in the
Packit Service 82fcde
@code{int_curr_symbol} element of the @code{struct lconv}.
Packit Service 82fcde
@item CURRENCY_SYMBOL
Packit Service 82fcde
@itemx CRNCYSTR
Packit Service 82fcde
The same as the value returned by @code{localeconv} in the
Packit Service 82fcde
@code{currency_symbol} element of the @code{struct lconv}.
Packit Service 82fcde
Packit Service 82fcde
@code{CRNCYSTR} is a deprecated alias still required by Unix98.
Packit Service 82fcde
@item MON_DECIMAL_POINT
Packit Service 82fcde
The same as the value returned by @code{localeconv} in the
Packit Service 82fcde
@code{mon_decimal_point} element of the @code{struct lconv}.
Packit Service 82fcde
@item MON_THOUSANDS_SEP
Packit Service 82fcde
The same as the value returned by @code{localeconv} in the
Packit Service 82fcde
@code{mon_thousands_sep} element of the @code{struct lconv}.
Packit Service 82fcde
@item MON_GROUPING
Packit Service 82fcde
The same as the value returned by @code{localeconv} in the
Packit Service 82fcde
@code{mon_grouping} element of the @code{struct lconv}.
Packit Service 82fcde
@item POSITIVE_SIGN
Packit Service 82fcde
The same as the value returned by @code{localeconv} in the
Packit Service 82fcde
@code{positive_sign} element of the @code{struct lconv}.
Packit Service 82fcde
@item NEGATIVE_SIGN
Packit Service 82fcde
The same as the value returned by @code{localeconv} in the
Packit Service 82fcde
@code{negative_sign} element of the @code{struct lconv}.
Packit Service 82fcde
@item INT_FRAC_DIGITS
Packit Service 82fcde
The same as the value returned by @code{localeconv} in the
Packit Service 82fcde
@code{int_frac_digits} element of the @code{struct lconv}.
Packit Service 82fcde
@item FRAC_DIGITS
Packit Service 82fcde
The same as the value returned by @code{localeconv} in the
Packit Service 82fcde
@code{frac_digits} element of the @code{struct lconv}.
Packit Service 82fcde
@item P_CS_PRECEDES
Packit Service 82fcde
The same as the value returned by @code{localeconv} in the
Packit Service 82fcde
@code{p_cs_precedes} element of the @code{struct lconv}.
Packit Service 82fcde
@item P_SEP_BY_SPACE
Packit Service 82fcde
The same as the value returned by @code{localeconv} in the
Packit Service 82fcde
@code{p_sep_by_space} element of the @code{struct lconv}.
Packit Service 82fcde
@item N_CS_PRECEDES
Packit Service 82fcde
The same as the value returned by @code{localeconv} in the
Packit Service 82fcde
@code{n_cs_precedes} element of the @code{struct lconv}.
Packit Service 82fcde
@item N_SEP_BY_SPACE
Packit Service 82fcde
The same as the value returned by @code{localeconv} in the
Packit Service 82fcde
@code{n_sep_by_space} element of the @code{struct lconv}.
Packit Service 82fcde
@item P_SIGN_POSN
Packit Service 82fcde
The same as the value returned by @code{localeconv} in the
Packit Service 82fcde
@code{p_sign_posn} element of the @code{struct lconv}.
Packit Service 82fcde
@item N_SIGN_POSN
Packit Service 82fcde
The same as the value returned by @code{localeconv} in the
Packit Service 82fcde
@code{n_sign_posn} element of the @code{struct lconv}.
Packit Service 82fcde
Packit Service 82fcde
@item INT_P_CS_PRECEDES
Packit Service 82fcde
The same as the value returned by @code{localeconv} in the
Packit Service 82fcde
@code{int_p_cs_precedes} element of the @code{struct lconv}.
Packit Service 82fcde
@item INT_P_SEP_BY_SPACE
Packit Service 82fcde
The same as the value returned by @code{localeconv} in the
Packit Service 82fcde
@code{int_p_sep_by_space} element of the @code{struct lconv}.
Packit Service 82fcde
@item INT_N_CS_PRECEDES
Packit Service 82fcde
The same as the value returned by @code{localeconv} in the
Packit Service 82fcde
@code{int_n_cs_precedes} element of the @code{struct lconv}.
Packit Service 82fcde
@item INT_N_SEP_BY_SPACE
Packit Service 82fcde
The same as the value returned by @code{localeconv} in the
Packit Service 82fcde
@code{int_n_sep_by_space} element of the @code{struct lconv}.
Packit Service 82fcde
@item INT_P_SIGN_POSN
Packit Service 82fcde
The same as the value returned by @code{localeconv} in the
Packit Service 82fcde
@code{int_p_sign_posn} element of the @code{struct lconv}.
Packit Service 82fcde
@item INT_N_SIGN_POSN
Packit Service 82fcde
The same as the value returned by @code{localeconv} in the
Packit Service 82fcde
@code{int_n_sign_posn} element of the @code{struct lconv}.
Packit Service 82fcde
Packit Service 82fcde
@item DECIMAL_POINT
Packit Service 82fcde
@itemx RADIXCHAR
Packit Service 82fcde
The same as the value returned by @code{localeconv} in the
Packit Service 82fcde
@code{decimal_point} element of the @code{struct lconv}.
Packit Service 82fcde
Packit Service 82fcde
The name @code{RADIXCHAR} is a deprecated alias still used in Unix98.
Packit Service 82fcde
@item THOUSANDS_SEP
Packit Service 82fcde
@itemx THOUSEP
Packit Service 82fcde
The same as the value returned by @code{localeconv} in the
Packit Service 82fcde
@code{thousands_sep} element of the @code{struct lconv}.
Packit Service 82fcde
Packit Service 82fcde
The name @code{THOUSEP} is a deprecated alias still used in Unix98.
Packit Service 82fcde
@item GROUPING
Packit Service 82fcde
The same as the value returned by @code{localeconv} in the
Packit Service 82fcde
@code{grouping} element of the @code{struct lconv}.
Packit Service 82fcde
@item YESEXPR
Packit Service 82fcde
The return value is a regular expression which can be used with the
Packit Service 82fcde
@code{regex} function to recognize a positive response to a yes/no
Packit Service 82fcde
question.  @Theglibc{} provides the @code{rpmatch} function for
Packit Service 82fcde
easier handling in applications.
Packit Service 82fcde
@item NOEXPR
Packit Service 82fcde
The return value is a regular expression which can be used with the
Packit Service 82fcde
@code{regex} function to recognize a negative response to a yes/no
Packit Service 82fcde
question.
Packit Service 82fcde
@item YESSTR
Packit Service 82fcde
The return value is a locale-specific translation of the positive response
Packit Service 82fcde
to a yes/no question.
Packit Service 82fcde
Packit Service 82fcde
Using this value is deprecated since it is a very special case of
Packit Service 82fcde
message translation, and is better handled by the message
Packit Service 82fcde
translation functions (@pxref{Message Translation}).
Packit Service 82fcde
Packit Service 82fcde
The use of this symbol is deprecated.  Instead message translation
Packit Service 82fcde
should be used.
Packit Service 82fcde
@item NOSTR
Packit Service 82fcde
The return value is a locale-specific translation of the negative response
Packit Service 82fcde
to a yes/no question.  What is said for @code{YESSTR} is also true here.
Packit Service 82fcde
Packit Service 82fcde
The use of this symbol is deprecated.  Instead message translation
Packit Service 82fcde
should be used.
Packit Service 82fcde
@end vtable
Packit Service 82fcde
Packit Service 82fcde
The file @file{langinfo.h} defines a lot more symbols but none of them
Packit Service 82fcde
are official.  Using them is not portable, and the format of the
Packit Service 82fcde
return values might change.  Therefore we recommended you not use
Packit Service 82fcde
them.
Packit Service 82fcde
Packit Service 82fcde
Note that the return value for any valid argument can be used
Packit Service 82fcde
in all situations (with the possible exception of the am/pm time formatting
Packit Service 82fcde
codes).  If the user has not selected any locale for the
Packit Service 82fcde
appropriate category, @code{nl_langinfo} returns the information from the
Packit Service 82fcde
@code{"C"} locale.  It is therefore possible to use this function as
Packit Service 82fcde
shown in the example below.
Packit Service 82fcde
Packit Service 82fcde
If the argument @var{item} is not valid, a pointer to an empty string is
Packit Service 82fcde
returned.
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
An example of @code{nl_langinfo} usage is a function which has to
Packit Service 82fcde
print a given date and time in a locale-specific way.  At first one
Packit Service 82fcde
might think that, since @code{strftime} internally uses the locale
Packit Service 82fcde
information, writing something like the following is enough:
Packit Service 82fcde
Packit Service 82fcde
@smallexample
Packit Service 82fcde
size_t
Packit Service 82fcde
i18n_time_n_data (char *s, size_t len, const struct tm *tp)
Packit Service 82fcde
@{
Packit Service 82fcde
  return strftime (s, len, "%X %D", tp);
Packit Service 82fcde
@}
Packit Service 82fcde
@end smallexample
Packit Service 82fcde
Packit Service 82fcde
The format contains no weekday or month names and therefore is
Packit Service 82fcde
internationally usable.  Wrong!  The output produced is something like
Packit Service 82fcde
@code{"hh:mm:ss MM/DD/YY"}.  This format is only recognizable in the
Packit Service 82fcde
USA.  Other countries use different formats.  Therefore the function
Packit Service 82fcde
should be rewritten like this:
Packit Service 82fcde
Packit Service 82fcde
@smallexample
Packit Service 82fcde
size_t
Packit Service 82fcde
i18n_time_n_data (char *s, size_t len, const struct tm *tp)
Packit Service 82fcde
@{
Packit Service 82fcde
  return strftime (s, len, nl_langinfo (D_T_FMT), tp);
Packit Service 82fcde
@}
Packit Service 82fcde
@end smallexample
Packit Service 82fcde
Packit Service 82fcde
Now it uses the date and time format of the locale
Packit Service 82fcde
selected when the program runs.  If the user selects the locale
Packit Service 82fcde
correctly there should never be a misunderstanding over the time and
Packit Service 82fcde
date format.
Packit Service 82fcde
Packit Service 82fcde
@node Formatting Numbers, Yes-or-No Questions, Locale Information, Locales
Packit Service 82fcde
@section A dedicated function to format numbers
Packit Service 82fcde
Packit Service 82fcde
We have seen that the structure returned by @code{localeconv} as well as
Packit Service 82fcde
the values given to @code{nl_langinfo} allow you to retrieve the various
Packit Service 82fcde
pieces of locale-specific information to format numbers and monetary
Packit Service 82fcde
amounts.  We have also seen that the underlying rules are quite complex.
Packit Service 82fcde
Packit Service 82fcde
Therefore the X/Open standards introduce a function which uses such
Packit Service 82fcde
locale information, making it easier for the user to format
Packit Service 82fcde
numbers according to these rules.
Packit Service 82fcde
Packit Service 82fcde
@deftypefun ssize_t strfmon (char *@var{s}, size_t @var{maxsize}, const char *@var{format}, @dots{})
Packit Service 82fcde
@safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{}}}
Packit Service 82fcde
@c It (and strfmon_l) both call vstrfmon_l, which, besides accessing the
Packit Service 82fcde
@c locale object passed to it, accesses the active locale through
Packit Service 82fcde
@c isdigit (but to_digit assumes ASCII digits only).  It may call
Packit Service 82fcde
@c __printf_fp (@mtslocale @ascuheap @acsmem) and guess_grouping (safe).
Packit Service 82fcde
The @code{strfmon} function is similar to the @code{strftime} function
Packit Service 82fcde
in that it takes a buffer, its size, a format string,
Packit Service 82fcde
and values to write into the buffer as text in a form specified
Packit Service 82fcde
by the format string.  Like @code{strftime}, the function
Packit Service 82fcde
also returns the number of bytes written into the buffer.
Packit Service 82fcde
Packit Service 82fcde
There are two differences: @code{strfmon} can take more than one
Packit Service 82fcde
argument, and, of course, the format specification is different.  Like
Packit Service 82fcde
@code{strftime}, the format string consists of normal text, which is
Packit Service 82fcde
output as is, and format specifiers, which are indicated by a @samp{%}.
Packit Service 82fcde
Immediately after the @samp{%}, you can optionally specify various flags
Packit Service 82fcde
and formatting information before the main formatting character, in a
Packit Service 82fcde
similar way to @code{printf}:
Packit Service 82fcde
Packit Service 82fcde
@itemize @bullet
Packit Service 82fcde
@item
Packit Service 82fcde
Immediately following the @samp{%} there can be one or more of the
Packit Service 82fcde
following flags:
Packit Service 82fcde
@table @asis
Packit Service 82fcde
@item @samp{=@var{f}}
Packit Service 82fcde
The single byte character @var{f} is used for this field as the numeric
Packit Service 82fcde
fill character.  By default this character is a space character.
Packit Service 82fcde
Filling with this character is only performed if a left precision
Packit Service 82fcde
is specified.  It is not just to fill to the given field width.
Packit Service 82fcde
@item @samp{^}
Packit Service 82fcde
The number is printed without grouping the digits according to the rules
Packit Service 82fcde
of the current locale.  By default grouping is enabled.
Packit Service 82fcde
@item @samp{+}, @samp{(}
Packit Service 82fcde
At most one of these flags can be used.  They select which format to
Packit Service 82fcde
represent the sign of a currency amount.  By default, and if
Packit Service 82fcde
@samp{+} is given, the locale equivalent of @math{+}/@math{-} is used.  If
Packit Service 82fcde
@samp{(} is given, negative amounts are enclosed in parentheses.  The
Packit Service 82fcde
exact format is determined by the values of the @code{LC_MONETARY}
Packit Service 82fcde
category of the locale selected at program runtime.
Packit Service 82fcde
@item @samp{!}
Packit Service 82fcde
The output will not contain the currency symbol.
Packit Service 82fcde
@item @samp{-}
Packit Service 82fcde
The output will be formatted left-justified instead of right-justified if
Packit Service 82fcde
it does not fill the entire field width.
Packit Service 82fcde
@end table
Packit Service 82fcde
@end itemize
Packit Service 82fcde
Packit Service 82fcde
The next part of the specification is an optional field width.  If no
Packit Service 82fcde
width is specified @math{0} is taken.  During output, the function first
Packit Service 82fcde
determines how much space is required.  If it requires at least as many
Packit Service 82fcde
characters as given by the field width, it is output using as much space
Packit Service 82fcde
as necessary.  Otherwise, it is extended to use the full width by
Packit Service 82fcde
filling with the space character.  The presence or absence of the
Packit Service 82fcde
@samp{-} flag determines the side at which such padding occurs.  If
Packit Service 82fcde
present, the spaces are added at the right making the output
Packit Service 82fcde
left-justified, and vice versa.
Packit Service 82fcde
Packit Service 82fcde
So far the format looks familiar, being similar to the @code{printf} and
Packit Service 82fcde
@code{strftime} formats.  However, the next two optional fields
Packit Service 82fcde
introduce something new.  The first one is a @samp{#} character followed
Packit Service 82fcde
by a decimal digit string.  The value of the digit string specifies the
Packit Service 82fcde
number of @emph{digit} positions to the left of the decimal point (or
Packit Service 82fcde
equivalent).  This does @emph{not} include the grouping character when
Packit Service 82fcde
the @samp{^} flag is not given.  If the space needed to print the number
Packit Service 82fcde
does not fill the whole width, the field is padded at the left side with
Packit Service 82fcde
the fill character, which can be selected using the @samp{=} flag and by
Packit Service 82fcde
default is a space.  For example, if the field width is selected as 6
Packit Service 82fcde
and the number is @math{123}, the fill character is @samp{*} the result
Packit Service 82fcde
will be @samp{***123}.
Packit Service 82fcde
Packit Service 82fcde
The second optional field starts with a @samp{.} (period) and consists
Packit Service 82fcde
of another decimal digit string.  Its value describes the number of
Packit Service 82fcde
characters printed after the decimal point.  The default is selected
Packit Service 82fcde
from the current locale (@code{frac_digits}, @code{int_frac_digits}, see
Packit Service 82fcde
@pxref{General Numeric}).  If the exact representation needs more digits
Packit Service 82fcde
than given by the field width, the displayed value is rounded.  If the
Packit Service 82fcde
number of fractional digits is selected to be zero, no decimal point is
Packit Service 82fcde
printed.
Packit Service 82fcde
Packit Service 82fcde
As a GNU extension, the @code{strfmon} implementation in @theglibc{}
Packit Service 82fcde
allows an optional @samp{L} next as a format modifier.  If this modifier
Packit Service 82fcde
is given, the argument is expected to be a @code{long double} instead of
Packit Service 82fcde
a @code{double} value.
Packit Service 82fcde
Packit Service 82fcde
Finally, the last component is a format specifier.  There are three
Packit Service 82fcde
specifiers defined:
Packit Service 82fcde
Packit Service 82fcde
@table @asis
Packit Service 82fcde
@item @samp{i}
Packit Service 82fcde
Use the locale's rules for formatting an international currency value.
Packit Service 82fcde
@item @samp{n}
Packit Service 82fcde
Use the locale's rules for formatting a national currency value.
Packit Service 82fcde
@item @samp{%}
Packit Service 82fcde
Place a @samp{%} in the output.  There must be no flag, width
Packit Service 82fcde
specifier or modifier given, only @samp{%%} is allowed.
Packit Service 82fcde
@end table
Packit Service 82fcde
Packit Service 82fcde
As for @code{printf}, the function reads the format string
Packit Service 82fcde
from left to right and uses the values passed to the function following
Packit Service 82fcde
the format string.  The values are expected to be either of type
Packit Service 82fcde
@code{double} or @code{long double}, depending on the presence of the
Packit Service 82fcde
modifier @samp{L}.  The result is stored in the buffer pointed to by
Packit Service 82fcde
@var{s}.  At most @var{maxsize} characters are stored.
Packit Service 82fcde
Packit Service 82fcde
The return value of the function is the number of characters stored in
Packit Service 82fcde
@var{s}, including the terminating @code{NULL} byte.  If the number of
Packit Service 82fcde
characters stored would exceed @var{maxsize}, the function returns
Packit Service 82fcde
@math{-1} and the content of the buffer @var{s} is unspecified.  In this
Packit Service 82fcde
case @code{errno} is set to @code{E2BIG}.
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
A few examples should make clear how the function works.  It is
Packit Service 82fcde
assumed that all the following pieces of code are executed in a program
Packit Service 82fcde
which uses the USA locale (@code{en_US}).  The simplest
Packit Service 82fcde
form of the format is this:
Packit Service 82fcde
Packit Service 82fcde
@smallexample
Packit Service 82fcde
strfmon (buf, 100, "@@%n@@%n@@%n@@", 123.45, -567.89, 12345.678);
Packit Service 82fcde
@end smallexample
Packit Service 82fcde
Packit Service 82fcde
@noindent
Packit Service 82fcde
The output produced is
Packit Service 82fcde
@smallexample
Packit Service 82fcde
"@@$123.45@@-$567.89@@$12,345.68@@"
Packit Service 82fcde
@end smallexample
Packit Service 82fcde
Packit Service 82fcde
We can notice several things here.  First, the widths of the output
Packit Service 82fcde
numbers are different.  We have not specified a width in the format
Packit Service 82fcde
string, and so this is no wonder.  Second, the third number is printed
Packit Service 82fcde
using thousands separators.  The thousands separator for the
Packit Service 82fcde
@code{en_US} locale is a comma.  The number is also rounded.
Packit Service 82fcde
@math{.678} is rounded to @math{.68} since the format does not specify a
Packit Service 82fcde
precision and the default value in the locale is @math{2}.  Finally,
Packit Service 82fcde
note that the national currency symbol is printed since @samp{%n} was
Packit Service 82fcde
used, not @samp{i}.  The next example shows how we can align the output.
Packit Service 82fcde
Packit Service 82fcde
@smallexample
Packit Service 82fcde
strfmon (buf, 100, "@@%=*11n@@%=*11n@@%=*11n@@", 123.45, -567.89, 12345.678);
Packit Service 82fcde
@end smallexample
Packit Service 82fcde
Packit Service 82fcde
@noindent
Packit Service 82fcde
The output this time is:
Packit Service 82fcde
Packit Service 82fcde
@smallexample
Packit Service 82fcde
"@@    $123.45@@   -$567.89@@ $12,345.68@@"
Packit Service 82fcde
@end smallexample
Packit Service 82fcde
Packit Service 82fcde
Two things stand out.  Firstly, all fields have the same width (eleven
Packit Service 82fcde
characters) since this is the width given in the format and since no
Packit Service 82fcde
number required more characters to be printed.  The second important
Packit Service 82fcde
point is that the fill character is not used.  This is correct since the
Packit Service 82fcde
white space was not used to achieve a precision given by a @samp{#}
Packit Service 82fcde
modifier, but instead to fill to the given width.  The difference
Packit Service 82fcde
becomes obvious if we now add a width specification.
Packit Service 82fcde
Packit Service 82fcde
@smallexample
Packit Service 82fcde
strfmon (buf, 100, "@@%=*11#5n@@%=*11#5n@@%=*11#5n@@",
Packit Service 82fcde
         123.45, -567.89, 12345.678);
Packit Service 82fcde
@end smallexample
Packit Service 82fcde
Packit Service 82fcde
@noindent
Packit Service 82fcde
The output is
Packit Service 82fcde
Packit Service 82fcde
@smallexample
Packit Service 82fcde
"@@ $***123.45@@-$***567.89@@ $12,456.68@@"
Packit Service 82fcde
@end smallexample
Packit Service 82fcde
Packit Service 82fcde
Here we can see that all the currency symbols are now aligned, and that
Packit Service 82fcde
the space between the currency sign and the number is filled with the
Packit Service 82fcde
selected fill character.  Note that although the width is selected to be
Packit Service 82fcde
@math{5} and @math{123.45} has three digits left of the decimal point,
Packit Service 82fcde
the space is filled with three asterisks.  This is correct since, as
Packit Service 82fcde
explained above, the width does not include the positions used to store
Packit Service 82fcde
thousands separators.  One last example should explain the remaining
Packit Service 82fcde
functionality.
Packit Service 82fcde
Packit Service 82fcde
@smallexample
Packit Service 82fcde
strfmon (buf, 100, "@@%=0(16#5.3i@@%=0(16#5.3i@@%=0(16#5.3i@@",
Packit Service 82fcde
         123.45, -567.89, 12345.678);
Packit Service 82fcde
@end smallexample
Packit Service 82fcde
Packit Service 82fcde
@noindent
Packit Service 82fcde
This rather complex format string produces the following output:
Packit Service 82fcde
Packit Service 82fcde
@smallexample
Packit Service 82fcde
"@@ USD 000123,450 @@(USD 000567.890)@@ USD 12,345.678 @@"
Packit Service 82fcde
@end smallexample
Packit Service 82fcde
Packit Service 82fcde
The most noticeable change is the alternative way of representing
Packit Service 82fcde
negative numbers.  In financial circles this is often done using
Packit Service 82fcde
parentheses, and this is what the @samp{(} flag selected.  The fill
Packit Service 82fcde
character is now @samp{0}.  Note that this @samp{0} character is not
Packit Service 82fcde
regarded as a numeric zero, and therefore the first and second numbers
Packit Service 82fcde
are not printed using a thousands separator.  Since we used the format
Packit Service 82fcde
specifier @samp{i} instead of @samp{n}, the international form of the
Packit Service 82fcde
currency symbol is used.  This is a four letter string, in this case
Packit Service 82fcde
@code{"USD "}.  The last point is that since the precision right of the
Packit Service 82fcde
decimal point is selected to be three, the first and second numbers are
Packit Service 82fcde
printed with an extra zero at the end and the third number is printed
Packit Service 82fcde
without rounding.
Packit Service 82fcde
Packit Service 82fcde
@node Yes-or-No Questions,  , Formatting Numbers , Locales
Packit Service 82fcde
@section Yes-or-No Questions
Packit Service 82fcde
Packit Service 82fcde
Some non GUI programs ask a yes-or-no question.  If the messages
Packit Service 82fcde
(especially the questions) are translated into foreign languages, be
Packit Service 82fcde
sure that you localize the answers too.  It would be very bad habit to
Packit Service 82fcde
ask a question in one language and request the answer in another, often
Packit Service 82fcde
English.
Packit Service 82fcde
Packit Service 82fcde
@Theglibc{} contains @code{rpmatch} to give applications easy
Packit Service 82fcde
access to the corresponding locale definitions.
Packit Service 82fcde
Packit Service 82fcde
@deftypefun int rpmatch (const char *@var{response})
Packit Service 82fcde
@standards{GNU, stdlib.h}
Packit Service 82fcde
@safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@asucorrupt{} @ascuheap{} @asulock{} @ascudlopen{}}@acunsafe{@acucorrupt{} @aculock{} @acsmem{} @acsfd{}}}
Packit Service 82fcde
@c Calls nl_langinfo with YESEXPR and NOEXPR, triggering @mtslocale but
Packit Service 82fcde
@c it's regcomp and regexec that bring in all of the safety issues.
Packit Service 82fcde
@c regfree is also called, but it doesn't introduce any further issues.
Packit Service 82fcde
The function @code{rpmatch} checks the string in @var{response} for whether
Packit Service 82fcde
or not it is a correct yes-or-no answer and if yes, which one.  The
Packit Service 82fcde
check uses the @code{YESEXPR} and @code{NOEXPR} data in the
Packit Service 82fcde
@code{LC_MESSAGES} category of the currently selected locale.  The
Packit Service 82fcde
return value is as follows:
Packit Service 82fcde
Packit Service 82fcde
@table @code
Packit Service 82fcde
@item 1
Packit Service 82fcde
The user entered an affirmative answer.
Packit Service 82fcde
Packit Service 82fcde
@item 0
Packit Service 82fcde
The user entered a negative answer.
Packit Service 82fcde
Packit Service 82fcde
@item -1
Packit Service 82fcde
The answer matched neither the @code{YESEXPR} nor the @code{NOEXPR}
Packit Service 82fcde
regular expression.
Packit Service 82fcde
@end table
Packit Service 82fcde
Packit Service 82fcde
This function is not standardized but available beside in @theglibc{} at
Packit Service 82fcde
least also in the IBM AIX library.
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
@noindent
Packit Service 82fcde
This function would normally be used like this:
Packit Service 82fcde
Packit Service 82fcde
@smallexample
Packit Service 82fcde
  @dots{}
Packit Service 82fcde
  /* @r{Use a safe default.}  */
Packit Service 82fcde
  _Bool doit = false;
Packit Service 82fcde
Packit Service 82fcde
  fputs (gettext ("Do you really want to do this? "), stdout);
Packit Service 82fcde
  fflush (stdout);
Packit Service 82fcde
  /* @r{Prepare the @code{getline} call.}  */
Packit Service 82fcde
  line = NULL;
Packit Service 82fcde
  len = 0;
Packit Service 82fcde
  while (getline (&line, &len, stdin) >= 0)
Packit Service 82fcde
    @{
Packit Service 82fcde
      /* @r{Check the response.}  */
Packit Service 82fcde
      int res = rpmatch (line);
Packit Service 82fcde
      if (res >= 0)
Packit Service 82fcde
        @{
Packit Service 82fcde
          /* @r{We got a definitive answer.}  */
Packit Service 82fcde
          if (res > 0)
Packit Service 82fcde
            doit = true;
Packit Service 82fcde
          break;
Packit Service 82fcde
        @}
Packit Service 82fcde
    @}
Packit Service 82fcde
  /* @r{Free what @code{getline} allocated.}  */
Packit Service 82fcde
  free (line);
Packit Service 82fcde
@end smallexample
Packit Service 82fcde
Packit Service 82fcde
Note that the loop continues until a read error is detected or until a
Packit Service 82fcde
definitive (positive or negative) answer is read.