⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 locale.texi

📁 一个C源代码分析器
💻 TEXI
📖 第 1 页 / 共 2 页
字号:
@node Locales, Searching and Sorting, Extended Characters, Top@chapter Locales and InternationalizationDifferent countries and cultures have varying conventions for how tocommunicate.  These conventions range from very simple ones, such as theformat for representing dates and times, to very complex ones, such asthe language spoken.@cindex internationalization@cindex locales@dfn{Internationalization} of software means programming it to be ableto adapt to the user's favorite conventions.  In ANSI C,internationalization works by means of @dfn{locales}.  Each localespecifies a collection of conventions, one convention for each purpose.The user chooses a set of conventions by specifying a locale (viaenvironment variables).All programs inherit the chosen locale as part of their environment.Provided the programs are written to obey the choice of locale, theywill follow the conventions preferred by the user.@menu* Effects of Locale::           Actions affected by the choice of                                 locale. * Choosing Locale::             How the user specifies a locale.* Locale Categories::           Different purposes for which you can                                 select a locale. * Setting the Locale::          How a program specifies the locale                                 with library functions. * Standard Locales::            Locale names available on all systems.* Numeric Formatting::          How to format numbers according to the                                 chosen locale. @end menu@node Effects of Locale, Choosing Locale,  , Locales@section What Effects a Locale HasEach locale specifies conventions for several purposes, including thefollowing:@itemize @bullet@itemWhat multibyte character sequences are valid, and how they areinterpreted (@pxref{Extended Characters}).@itemClassification of which characters in the local character set areconsidered alphabetic, and upper- and lower-case conversion conventions(@pxref{Character Handling}).@itemThe collating sequence for the local language and character set(@pxref{Collation Functions}).@itemFormatting of numbers and currency amounts (@pxref{Numeric Formatting}).@itemFormatting of dates and times (@pxref{Formatting Date and Time}).@itemWhat language to use for output, including error messages.(The C library doesn't yet help you implement this.)@itemWhat language to use for user answers to yes-or-no questions.@itemWhat language to use for more complex user input.(The C library doesn't yet help you implement this.)@end itemizeSome aspects of adapting to the specified locale are handledautomatically by the library subroutines.  For example, all your programneeds to do in order to use the collating sequence of the chosen localeis to use @code{strcoll} or @code{strxfrm} to compare strings.Other aspects of locales are beyond the comprehension of the library.For example, the library can't automatically translate your program'soutput messages into other languages.  The only way you can supportoutput in the user's favorite language is to program this more or lessby hand.  (Eventually, we hope to provide facilities to make thiseasier.)This chapter discusses the mechanism by which you can modify the currentlocale.  The effects of the current locale on specific library functionsare discussed in more detail in the descriptions of those functions.@node Choosing Locale, Locale Categories, Effects of Locale, Locales@section Choosing a LocaleThe simplest way for the user to choose a locale is to set theenvironment variable @code{LANG}.  This specifies a single locale to usefor all purposes.  For example, a user could specify a hypotheticallocale named @samp{espana-castellano} to use the standard conventions ofmost of Spain.The set of locales supported depends on the operating system you areusing, and so do their names.  We can't make any promises about whatlocales will exist, except for one standard locale called @samp{C} or@samp{POSIX}.@cindex combining localesA user also has the option of specifying different locales for differentpurposes---in effect, choosing a mixture of multiple locales.For example, the user might specify the locale @samp{espana-castellano}for most purposes, but specify the locale @samp{usa-english} forcurrency formatting.  This might make sense if the user is aSpanish-speaking American, working in Spanish, but representing monetaryamounts in US dollars.Note that both locales @samp{espana-castellano} and @samp{usa-english},like all locales, would include conventions for all of the purposes towhich locales apply.  However, the user can choose to use each localefor a particular subset of those purposes.@node Locale Categories, Setting the Locale, Choosing Locale, Locales@section Categories of Activities that Locales Affect@cindex categories for locales@cindex locale categoriesThe purposes that locales serve are grouped into @dfn{categories}, sothat a user or a program can choose the locale for each categoryindependently.  Here is a table of categories; each name is both anenvironment variable that a user can set, and a macro name that you canuse as an argument to @code{setlocale}.@table @code@comment locale.h@comment ANSI@item LC_COLLATE@vindex LC_COLLATEThis category applies to collation of strings (functions @code{strcoll}and @code{strxfrm}); see @ref{Collation Functions}.@comment locale.h@comment ANSI@item LC_CTYPE@vindex LC_CTYPEThis category applies to classification and conversion of characters,and to multibyte and wide characters;see @ref{Character Handling} and @ref{Extended Characters}.@comment locale.h@comment ANSI@item LC_MONETARY@vindex LC_MONETARYThis category applies to formatting monetary values; see @ref{NumericFormatting}.@comment locale.h@comment ANSI@item LC_NUMERIC@vindex LC_NUMERICThis category applies to formatting numeric values that are notmonetary; see @ref{Numeric Formatting}.@comment locale.h@comment ANSI@item LC_TIME@vindex LC_TIMEThis category applies to formatting date and time values; see@ref{Formatting Date and Time}.@ignore   This is apparently a feature that was in some earlydraft of the POSIX.2 standard, but it's not listed in draft 11.  Do westill support this anyway?  Is there a corresponding environmentvariable?@comment locale.h@comment GNU@item LC_RESPONSE@vindex LC_RESPONSEThis category applies to recognizing ``yes'' or ``no'' responses toquestions.@end ignore@comment locale.h@comment ANSI@item LC_ALL@vindex LC_ALLThis is not an environment variable; it is only a macro that you can usewith @code{setlocale} to set a single locale for all purposes.@comment locale.h@comment ANSI@item LANG@vindex LANGIf this environment variable is defined, its value specifies the localeto use for all purposes except as overridden by the variables above.@end table@node Setting the Locale, Standard Locales, Locale Categories, Locales@section How Programs Set the LocaleA C program inherits its locale environment variables when it starts up.This happens automatically.  However, these variables do notautomatically control the locale used by the library functions, becauseANSI C says that all programs start by default in the standard @samp{C}locale.  To use the locales specified by the environment, you must call@code{setlocale}.  Call it as follows:@smallexamplesetlocale (LC_ALL, "");@end smallexample@noindentto select a locale based on the appropriate environment variables.@cindex changing the locale@cindex locale, changingYou can also use @code{setlocale} to specify a particular locale, forgeneral use or for a specific category.@pindex locale.hThe symbols in this section are defined in the header file @file{locale.h}.@comment locale.h@comment ANSI@deftypefun {char *} setlocale (int @var{category}, const char *@var{locale})The function @code{setlocale} sets the current locale for category @var{category} to @var{locale}.If @var{category} is @code{LC_ALL}, this specifies the locale for allpurposes.  The other possible values of @var{category} specify anindividual purpose (@pxref{Locale Categories}).You can also use this function to find out the current locale by passinga null pointer as the @var{locale} argument.  In this case,@code{setlocale} returns a string that is the name of the localecurrently selected for category @var{category}.The string returned by @code{setlocale} can be overwritten by subsequentcalls, so you should make a copy of the string (@pxref{Copying andConcatenation}) if you want to save it past any further calls to@code{setlocale}.  (The standard library is guaranteed never to call@code{setlocale} itself.)You should not modify the string returned by @code{setlocale}.It might be the same string that was passed as an argument in a previous call to @code{setlocale}.When you read the current locale for category @code{LC_ALL}, the valueencodes the entire combination of selected locales for all categories.In this case, the value is not just a single locale name.  In fact, wedon't make any promises about what it looks like.  But if you specifythe same ``locale name'' with @code{LC_ALL} in a subsequent call to@code{setlocale}, it restores the same combination of locale selections.When the @var{locale} argument is not a null pointer, the string returnedby @code{setlocale} reflects the newly modified locale.If you specify an empty string for @var{locale}, this means to read theappropriate environment variable and use its value to select the localefor @var{category}.If you specify an invalid locale name, @code{setlocale} returns a nullpointer and leaves the current locale unchanged.@end deftypefunHere is an example showing how you might use @code{setlocale} totemporarily switch to a new locale.@smallexample#include <stddef.h>#include <locale.h>#include <stdlib.h>#include <string.h>voidwith_other_locale (char *new_locale,                   void (*subroutine) (int),                   int argument)@{  char *old_locale, *saved_locale;  /* @r{Get the name of the current locale.}  */  old_locale = setlocale (LC_ALL, NULL);    /* @r{Copy the name so it won't be clobbered by @code{setlocale}.} */  saved_locale = strdup (old_locale);  if (old_locale == NULL)    fatal ("Out of memory");    /* @r{Now change the locale and do some stuff with it.} */  setlocale (LC_ALL, new_locale);  (*subroutine) (argument);    /* @r{Restore the original locale.} */  setlocale (LC_ALL, saved_locale);  free (saved_locale);@}@end smallexample@strong{Portability Note:} Some ANSI C systems may define additionallocale categories.  For portability, assume that any symbol beginningwith @samp{LC_} might be defined in @file{locale.h}.@node Standard Locales, Numeric Formatting, Setting the Locale, Locales@section Standard LocalesThe only locale names you can count on finding on all operating systems

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -