Previous: The Lame Way to Locale Data, Up: Locale Information
When writing the X/Open Portability Guide the authors realized that the
localeconv function is not enough to provide reasonable access to
locale information. The information which was meant to be available
in the locale (as later specified in the POSIX.1 standard) requires more
ways to access it. Therefore the nl_langinfo function
was introduced.
The
nl_langinfofunction can be used to access individual elements of the locale categories. Unlike thelocaleconvfunction, which returns all the information,nl_langinfolets the caller select what information it requires. This is very fast and it is not a problem to call this function multiple times.A second advantage is that in addition to the numeric and monetary formatting information, information from the
LC_TIMEandLC_MESSAGEScategories is available.The type
nl_typeis defined in nl_types.h. The argument item is a numeric value defined in the header langinfo.h. The X/Open standard defines the following values:
CODESETnl_langinforeturns a string with the name of the coded character set used in the selected locale.ABDAY_1ABDAY_2ABDAY_3ABDAY_4ABDAY_5ABDAY_6ABDAY_7nl_langinforeturns the abbreviated weekday name.ABDAY_1corresponds to Sunday.DAY_1DAY_2DAY_3DAY_4DAY_5DAY_6DAY_7- Similar to
ABDAY_1etc., but here the return value is the unabbreviated weekday name.ABMON_1ABMON_2ABMON_3ABMON_4ABMON_5ABMON_6ABMON_7ABMON_8ABMON_9ABMON_10ABMON_11ABMON_12- The return value is abbreviated name of the month.
ABMON_1corresponds to January.MON_1MON_2MON_3MON_4MON_5MON_6MON_7MON_8MON_9MON_10MON_11MON_12- Similar to
ABMON_1etc., but here the month names are not abbreviated. Here the first valueMON_1also corresponds to January.AM_STRPM_STR- The return values are strings which can be used in the representation of time as an hour from 1 to 12 plus an am/pm specifier.
Note that in locales which do not use this time representation these strings might be empty, in which case the am/pm format cannot be used at all.
D_T_FMT- The return value can be used as a format string for
strftimeto represent time and date in a locale-specific way.D_FMT- The return value can be used as a format string for
strftimeto represent a date in a locale-specific way.T_FMT- The return value can be used as a format string for
strftimeto represent time in a locale-specific way.T_FMT_AMPM- The return value can be used as a format string for
strftimeto represent time in the am/pm format.Note that if the am/pm format does not make any sense for the selected locale, the return value might be the same as the one for
T_FMT.ERA- The return value represents the era used in the current locale.
Most locales do not define this value. An example of a locale which does define this value is the Japanese one. In Japan, the traditional representation of dates includes the name of the era corresponding to the then-emperor's reign.
Normally it should not be necessary to use this value directly. Specifying the
Emodifier in their format strings causes thestrftimefunctions to use this information. The format of the returned string is not specified, and therefore you should not assume knowledge of it on different systems.ERA_YEAR- The return value gives the year in the relevant era of the locale. As for
ERAit should not be necessary to use this value directly.ERA_D_T_FMT- This return value can be used as a format string for
strftimeto represent dates and times in a locale-specific era-based way.ERA_D_FMT- This return value can be used as a format string for
strftimeto represent a date in a locale-specific era-based way.ERA_T_FMT- This return value can be used as a format string for
strftimeto represent time in a locale-specific era-based way.ALT_DIGITS- The return value is a representation of up to 100 values used to represent the values 0 to 99. As for
ERAthis value is not intended to be used directly, but instead indirectly through thestrftimefunction. When the modifierOis used in a format which would otherwise use numerals to represent hours, minutes, seconds, weekdays, months, or weeks, the appropriate value for the locale is used instead.INT_CURR_SYMBOL- The same as the value returned by
localeconvin theint_curr_symbolelement of thestruct lconv.CURRENCY_SYMBOLCRNCYSTR- The same as the value returned by
localeconvin thecurrency_symbolelement of thestruct lconv.
CRNCYSTRis a deprecated alias still required by Unix98.MON_DECIMAL_POINT- The same as the value returned by
localeconvin themon_decimal_pointelement of thestruct lconv.MON_THOUSANDS_SEP- The same as the value returned by
localeconvin themon_thousands_sepelement of thestruct lconv.MON_GROUPING- The same as the value returned by
localeconvin themon_groupingelement of thestruct lconv.POSITIVE_SIGN- The same as the value returned by
localeconvin thepositive_signelement of thestruct lconv.NEGATIVE_SIGN- The same as the value returned by
localeconvin thenegative_signelement of thestruct lconv.INT_FRAC_DIGITS- The same as the value returned by
localeconvin theint_frac_digitselement of thestruct lconv.FRAC_DIGITS- The same as the value returned by
localeconvin thefrac_digitselement of thestruct lconv.P_CS_PRECEDES- The same as the value returned by
localeconvin thep_cs_precedeselement of thestruct lconv.P_SEP_BY_SPACE- The same as the value returned by
localeconvin thep_sep_by_spaceelement of thestruct lconv.N_CS_PRECEDES- The same as the value returned by
localeconvin then_cs_precedeselement of thestruct lconv.N_SEP_BY_SPACE- The same as the value returned by
localeconvin then_sep_by_spaceelement of thestruct lconv.P_SIGN_POSN- The same as the value returned by
localeconvin thep_sign_posnelement of thestruct lconv.N_SIGN_POSN- The same as the value returned by
localeconvin then_sign_posnelement of thestruct lconv.INT_P_CS_PRECEDES- The same as the value returned by
localeconvin theint_p_cs_precedeselement of thestruct lconv.INT_P_SEP_BY_SPACE- The same as the value returned by
localeconvin theint_p_sep_by_spaceelement of thestruct lconv.INT_N_CS_PRECEDES- The same as the value returned by
localeconvin theint_n_cs_precedeselement of thestruct lconv.INT_N_SEP_BY_SPACE- The same as the value returned by
localeconvin theint_n_sep_by_spaceelement of thestruct lconv.INT_P_SIGN_POSN- The same as the value returned by
localeconvin theint_p_sign_posnelement of thestruct lconv.INT_N_SIGN_POSN- The same as the value returned by
localeconvin theint_n_sign_posnelement of thestruct lconv.DECIMAL_POINTRADIXCHAR- The same as the value returned by
localeconvin thedecimal_pointelement of thestruct lconv.The name
RADIXCHARis a deprecated alias still used in Unix98.THOUSANDS_SEPTHOUSEP- The same as the value returned by
localeconvin thethousands_sepelement of thestruct lconv.The name
THOUSEPis a deprecated alias still used in Unix98.GROUPING- The same as the value returned by
localeconvin thegroupingelement of thestruct lconv.YESEXPR- The return value is a regular expression which can be used with the
regexfunction to recognize a positive response to a yes/no question. The GNU C library provides therpmatchfunction for easier handling in applications.NOEXPR- The return value is a regular expression which can be used with the
regexfunction to recognize a negative response to a yes/no question.YESSTR- The return value is a locale-specific translation of the positive response to a yes/no question.
Using this value is deprecated since it is a very special case of message translation, and is better handled by the message translation functions (see Message Translation).
The use of this symbol is deprecated. Instead message translation should be used.
NOSTR- The return value is a locale-specific translation of the negative response to a yes/no question. What is said for
YESSTRis also true here.The use of this symbol is deprecated. Instead message translation should be used.
The file langinfo.h defines a lot more symbols but none of them is official. Using them is not portable, and the format of the return values might change. Therefore we recommended you not use them.
Note that the return value for any valid argument can be used for in all situations (with the possible exception of the am/pm time formatting codes). If the user has not selected any locale for the appropriate category,
nl_langinforeturns the information from the"C"locale. It is therefore possible to use this function as shown in the example below.If the argument item is not valid, a pointer to an empty string is returned.
An example of nl_langinfo usage is a function which has to
print a given date and time in a locale-specific way. At first one
might think that, since strftime internally uses the locale
information, writing something like the following is enough:
size_t
i18n_time_n_data (char *s, size_t len, const struct tm *tp)
{
return strftime (s, len, "%X %D", tp);
}
The format contains no weekday or month names and therefore is
internationally usable. Wrong! The output produced is something like
"hh:mm:ss MM/DD/YY". This format is only recognizable in the
USA. Other countries use different formats. Therefore the function
should be rewritten like this:
size_t
i18n_time_n_data (char *s, size_t len, const struct tm *tp)
{
return strftime (s, len, nl_langinfo (D_T_FMT), tp);
}
Now it uses the date and time format of the locale selected when the program runs. If the user selects the locale correctly there should never be a misunderstanding over the time and date format.