(ptrace) [PTRACE_TRACEME]: Notify the proc server that we are now traced.
[glibc.git] / manual / locale.texi
blobd2d7557ea9bb6b9a55d7f55f99dd31cb024ac213
1 @node Locales, Searching and Sorting, Extended Characters, Top
2 @chapter Locales and Internationalization
4 Different countries and cultures have varying conventions for how to
5 communicate.  These conventions range from very simple ones, such as the
6 format for representing dates and times, to very complex ones, such as
7 the language spoken.
9 @cindex internationalization
10 @cindex locales
11 @dfn{Internationalization} of software means programming it to be able
12 to adapt to the user's favorite conventions.  In ANSI C,
13 internationalization works by means of @dfn{locales}.  Each locale
14 specifies a collection of conventions, one convention for each purpose.
15 The user chooses a set of conventions by specifying a locale (via
16 environment variables).
18 All programs inherit the chosen locale as part of their environment.
19 Provided the programs are written to obey the choice of locale, they
20 will follow the conventions preferred by the user.
22 @menu
23 * Effects of Locale::           Actions affected by the choice of
24                                  locale. 
25 * Choosing Locale::             How the user specifies a locale.
26 * Locale Categories::           Different purposes for which you can
27                                  select a locale. 
28 * Setting the Locale::          How a program specifies the locale
29                                  with library functions. 
30 * Standard Locales::            Locale names available on all systems.
31 * Numeric Formatting::          How to format numbers according to the
32                                  chosen locale. 
33 @end menu
35 @node Effects of Locale, Choosing Locale,  , Locales
36 @section What Effects a Locale Has
38 Each locale specifies conventions for several purposes, including the
39 following:
41 @itemize @bullet
42 @item
43 What multibyte character sequences are valid, and how they are
44 interpreted (@pxref{Extended Characters}).
46 @item
47 Classification of which characters in the local character set are
48 considered alphabetic, and upper- and lower-case conversion conventions
49 (@pxref{Character Handling}).
51 @item
52 The collating sequence for the local language and character set
53 (@pxref{Collation Functions}).
55 @item
56 Formatting of numbers and currency amounts (@pxref{Numeric Formatting}).
58 @item
59 Formatting of dates and times (@pxref{Formatting Date and Time}).
61 @item
62 What language to use for output, including error messages.
63 (The C library doesn't yet help you implement this.)
65 @item
66 What language to use for user answers to yes-or-no questions.
68 @item
69 What language to use for more complex user input.
70 (The C library doesn't yet help you implement this.)
71 @end itemize
73 Some aspects of adapting to the specified locale are handled
74 automatically by the library subroutines.  For example, all your program
75 needs to do in order to use the collating sequence of the chosen locale
76 is to use @code{strcoll} or @code{strxfrm} to compare strings.
78 Other aspects of locales are beyond the comprehension of the library.
79 For example, the library can't automatically translate your program's
80 output messages into other languages.  The only way you can support
81 output in the user's favorite language is to program this more or less
82 by hand.  (Eventually, we hope to provide facilities to make this
83 easier.)
85 This chapter discusses the mechanism by which you can modify the current
86 locale.  The effects of the current locale on specific library functions
87 are discussed in more detail in the descriptions of those functions.
89 @node Choosing Locale, Locale Categories, Effects of Locale, Locales
90 @section Choosing a Locale
92 The simplest way for the user to choose a locale is to set the
93 environment variable @code{LANG}.  This specifies a single locale to use
94 for all purposes.  For example, a user could specify a hypothetical
95 locale named @samp{espana-castellano} to use the standard conventions of
96 most of Spain.
98 The set of locales supported depends on the operating system you are
99 using, and so do their names.  We can't make any promises about what
100 locales will exist, except for one standard locale called @samp{C} or
101 @samp{POSIX}.
103 @cindex combining locales
104 A user also has the option of specifying different locales for different
105 purposes---in effect, choosing a mixture of multiple locales.
107 For example, the user might specify the locale @samp{espana-castellano}
108 for most purposes, but specify the locale @samp{usa-english} for
109 currency formatting.  This might make sense if the user is a
110 Spanish-speaking American, working in Spanish, but representing monetary
111 amounts in US dollars.
113 Note that both locales @samp{espana-castellano} and @samp{usa-english},
114 like all locales, would include conventions for all of the purposes to
115 which locales apply.  However, the user can choose to use each locale
116 for a particular subset of those purposes.
118 @node Locale Categories, Setting the Locale, Choosing Locale, Locales
119 @section Categories of Activities that Locales Affect
120 @cindex categories for locales
121 @cindex locale categories
123 The purposes that locales serve are grouped into @dfn{categories}, so
124 that a user or a program can choose the locale for each category
125 independently.  Here is a table of categories; each name is both an
126 environment variable that a user can set, and a macro name that you can
127 use as an argument to @code{setlocale}.
129 @table @code
130 @comment locale.h
131 @comment ANSI
132 @item LC_COLLATE
133 @vindex LC_COLLATE
134 This category applies to collation of strings (functions @code{strcoll}
135 and @code{strxfrm}); see @ref{Collation Functions}.
137 @comment locale.h
138 @comment ANSI
139 @item LC_CTYPE
140 @vindex LC_CTYPE
141 This category applies to classification and conversion of characters,
142 and to multibyte and wide characters;
143 see @ref{Character Handling} and @ref{Extended Characters}.
145 @comment locale.h
146 @comment ANSI
147 @item LC_MONETARY
148 @vindex LC_MONETARY
149 This category applies to formatting monetary values; see @ref{Numeric
150 Formatting}.
152 @comment locale.h
153 @comment ANSI
154 @item LC_NUMERIC
155 @vindex LC_NUMERIC
156 This category applies to formatting numeric values that are not
157 monetary; see @ref{Numeric Formatting}.
159 @comment locale.h
160 @comment ANSI
161 @item LC_TIME
162 @vindex LC_TIME
163 This category applies to formatting date and time values; see
164 @ref{Formatting Date and Time}.
166 @ignore   This is apparently a feature that was in some early
167 draft of the POSIX.2 standard, but it's not listed in draft 11.  Do we
168 still support this anyway?  Is there a corresponding environment
169 variable?
171 @comment locale.h
172 @comment GNU
173 @item LC_RESPONSE
174 @vindex LC_RESPONSE
175 This category applies to recognizing ``yes'' or ``no'' responses to
176 questions.
177 @end ignore
179 @comment locale.h
180 @comment ANSI
181 @item LC_ALL
182 @vindex LC_ALL
183 This is not an environment variable; it is only a macro that you can use
184 with @code{setlocale} to set a single locale for all purposes.
186 @comment locale.h
187 @comment ANSI
188 @item LANG
189 @vindex LANG
190 If this environment variable is defined, its value specifies the locale
191 to use for all purposes except as overridden by the variables above.
192 @end table
194 @node Setting the Locale, Standard Locales, Locale Categories, Locales
195 @section How Programs Set the Locale
197 A C program inherits its locale environment variables when it starts up.
198 This happens automatically.  However, these variables do not
199 automatically control the locale used by the library functions, because
200 ANSI C says that all programs start by default in the standard @samp{C}
201 locale.  To use the locales specified by the environment, you must call
202 @code{setlocale}.  Call it as follows:
204 @smallexample
205 setlocale (LC_ALL, "");
206 @end smallexample
208 @noindent
209 to select a locale based on the appropriate environment variables.
211 @cindex changing the locale
212 @cindex locale, changing
213 You can also use @code{setlocale} to specify a particular locale, for
214 general use or for a specific category.
216 @pindex locale.h
217 The symbols in this section are defined in the header file @file{locale.h}.
219 @comment locale.h
220 @comment ANSI
221 @deftypefun {char *} setlocale (int @var{category}, const char *@var{locale})
222 The function @code{setlocale} sets the current locale for 
223 category @var{category} to @var{locale}.
225 If @var{category} is @code{LC_ALL}, this specifies the locale for all
226 purposes.  The other possible values of @var{category} specify an
227 individual purpose (@pxref{Locale Categories}).
229 You can also use this function to find out the current locale by passing
230 a null pointer as the @var{locale} argument.  In this case,
231 @code{setlocale} returns a string that is the name of the locale
232 currently selected for category @var{category}.
234 The string returned by @code{setlocale} can be overwritten by subsequent
235 calls, so you should make a copy of the string (@pxref{Copying and
236 Concatenation}) if you want to save it past any further calls to
237 @code{setlocale}.  (The standard library is guaranteed never to call
238 @code{setlocale} itself.)
240 You should not modify the string returned by @code{setlocale}.
241 It might be the same string that was passed as an argument in a 
242 previous call to @code{setlocale}.
244 When you read the current locale for category @code{LC_ALL}, the value
245 encodes the entire combination of selected locales for all categories.
246 In this case, the value is not just a single locale name.  In fact, we
247 don't make any promises about what it looks like.  But if you specify
248 the same ``locale name'' with @code{LC_ALL} in a subsequent call to
249 @code{setlocale}, it restores the same combination of locale selections.
251 When the @var{locale} argument is not a null pointer, the string returned
252 by @code{setlocale} reflects the newly modified locale.
254 If you specify an empty string for @var{locale}, this means to read the
255 appropriate environment variable and use its value to select the locale
256 for @var{category}.
258 If you specify an invalid locale name, @code{setlocale} returns a null
259 pointer and leaves the current locale unchanged.
260 @end deftypefun
262 Here is an example showing how you might use @code{setlocale} to
263 temporarily switch to a new locale.
265 @smallexample
266 #include <stddef.h>
267 #include <locale.h>
268 #include <stdlib.h>
269 #include <string.h>
271 void
272 with_other_locale (char *new_locale,
273                    void (*subroutine) (int),
274                    int argument)
276   char *old_locale, *saved_locale;
278   /* @r{Get the name of the current locale.}  */
279   old_locale = setlocale (LC_ALL, NULL);
280   
281   /* @r{Copy the name so it won't be clobbered by @code{setlocale}.} */
282   saved_locale = strdup (old_locale);
283   if (old_locale == NULL)
284     fatal ("Out of memory");
285   
286   /* @r{Now change the locale and do some stuff with it.} */
287   setlocale (LC_ALL, new_locale);
288   (*subroutine) (argument);
289   
290   /* @r{Restore the original locale.} */
291   setlocale (LC_ALL, saved_locale);
292   free (saved_locale);
294 @end smallexample
296 @strong{Portability Note:} Some ANSI C systems may define additional
297 locale categories.  For portability, assume that any symbol beginning
298 with @samp{LC_} might be defined in @file{locale.h}.
300 @node Standard Locales, Numeric Formatting, Setting the Locale, Locales
301 @section Standard Locales
303 The only locale names you can count on finding on all operating systems
304 are these three standard ones:
306 @table @code
307 @item "C"
308 This is the standard C locale.  The attributes and behavior it provides
309 are specified in the ANSI C standard.  When your program starts up, it
310 initially uses this locale by default.
312 @item "POSIX"
313 This is the standard POSIX locale.  Currently, it is an alias for the
314 standard C locale.
316 @item ""
317 The empty name says to select a locale based on environment variables.
318 @xref{Locale Categories}.
319 @end table
321 Defining and installing named locales is normally a responsibility of
322 the system administrator at your site (or the person who installed the
323 GNU C library).  Some systems may allow users to create locales, but
324 we don't discuss that here.
325 @c ??? If we give the GNU system that capability, this place will have
326 @c ??? to be changed.
328 If your program needs to use something other than the @samp{C} locale,
329 it will be more portable if you use whatever locale the user specifies
330 with the environment, rather than trying to specify some non-standard
331 locale explicitly by name.  Remember, different machines might have
332 different sets of locales installed.
334 @node Numeric Formatting,  , Standard Locales, Locales
335 @section Numeric Formatting
337 When you want to format a number or a currency amount using the
338 conventions of the current locale, you can use the function
339 @code{localeconv} to get the data on how to do it.  The function
340 @code{localeconv} is declared in the header file @file{locale.h}.
341 @pindex locale.h
342 @cindex monetary value formatting
343 @cindex numeric value formatting
345 @comment locale.h
346 @comment ANSI
347 @deftypefun {struct lconv *} localeconv (void)
348 The @code{localeconv} function returns a pointer to a structure whose
349 components contain information about how numeric and monetary values
350 should be formatted in the current locale.
352 You shouldn't modify the structure or its contents.  The structure might
353 be overwritten by subsequent calls to @code{localeconv}, or by calls to
354 @code{setlocale}, but no other function in the library overwrites this
355 value.
356 @end deftypefun
358 @comment locale.h
359 @comment ANSI
360 @deftp {Data Type} {struct lconv}
361 This is the data type of the value returned by @code{localeconv}.
362 @end deftp
364 If a member of the structure @code{struct lconv} has type @code{char},
365 and the value is @code{CHAR_MAX}, it means that the current locale has
366 no value for that parameter.
368 @menu
369 * General Numeric::             Parameters for formatting numbers and
370                                  currency amounts.
371 * Currency Symbol::             How to print the symbol that identifies an
372                                  amount of money (e.g. @samp{$}).
373 * Sign of Money Amount::        How to print the (positive or negative) sign
374                                  for a monetary amount, if one exists.
375 @end menu
377 @node General Numeric, Currency Symbol,  , Numeric Formatting
378 @subsection Generic Numeric Formatting Parameters
380 These are the standard members of @code{struct lconv}; there may be
381 others.
383 @table @code
384 @item char *decimal_point
385 @itemx char *mon_decimal_point
386 These are the decimal-point separators used in formatting non-monetary
387 and monetary quantities, respectively.  In the @samp{C} locale, the
388 value of @code{decimal_point} is @code{"."}, and the value of
389 @code{mon_decimal_point} is @code{""}.
390 @cindex decimal-point separator
392 @item char *thousands_sep
393 @itemx char *mon_thousands_sep
394 These are the separators used to delimit groups of digits to the left of
395 the decimal point in formatting non-monetary and monetary quantities,
396 respectively.  In the @samp{C} locale, both members have a value of
397 @code{""} (the empty string).
399 @item char *grouping
400 @itemx char *mon_grouping
401 These are strings that specify how to group the digits to the left of
402 the decimal point.  @code{grouping} applies to non-monetary quantities
403 and @code{mon_grouping} applies to monetary quantities.  Use either
404 @code{thousands_sep} or @code{mon_thousands_sep} to separate the digit
405 groups.
406 @cindex grouping of digits
408 Each string is made up of decimal numbers separated by semicolons.
409 Successive numbers (from left to right) give the sizes of successive
410 groups (from right to left, starting at the decimal point).  The last
411 number in the string is used over and over for all the remaining groups.
413 If the last integer is @code{-1}, it means that there is no more
414 grouping---or, put another way, any remaining digits form one large
415 group without separators.
417 For example, if @code{grouping} is @code{"4;3;2"}, the correct grouping
418 for the number @code{123456787654321} is @samp{12}, @samp{34},
419 @samp{56}, @samp{78}, @samp{765}, @samp{4321}.  This uses a group of 4
420 digits at the end, preceded by a group of 3 digits, preceded by groups
421 of 2 digits (as many as needed).  With a separator of @samp{,}, the
422 number would be printed as @samp{12,34,56,78,765,4321}.
424 A value of @code{"3"} indicates repeated groups of three digits, as
425 normally used in the U.S.
427 In the standard @samp{C} locale, both @code{grouping} and
428 @code{mon_grouping} have a value of @code{""}.  This value specifies no
429 grouping at all.
431 @item char int_frac_digits
432 @itemx char frac_digits
433 These are small integers indicating how many fractional digits (to the
434 right of the decimal point) should be displayed in a monetary value in
435 international and local formats, respectively.  (Most often, both
436 members have the same value.)
438 In the standard @samp{C} locale, both of these members have the value
439 @code{CHAR_MAX}, meaning ``unspecified''.  The ANSI standard doesn't say
440 what to do when you find this the value; we recommend printing no
441 fractional digits.  (This locale also specifies the empty string for
442 @code{mon_decimal_point}, so printing any fractional digits would be
443 confusing!)
444 @end table
446 @node Currency Symbol, Sign of Money Amount, General Numeric, Numeric Formatting
447 @subsection Printing the Currency Symbol
448 @cindex currency symbols
450 These members of the @code{struct lconv} structure specify how to print
451 the symbol to identify a monetary value---the international analog of
452 @samp{$} for US dollars.
454 Each country has two standard currency symbols.  The @dfn{local currency
455 symbol} is used commonly within the country, while the
456 @dfn{international currency symbol} is used internationally to refer to
457 that country's currency when it is necessary to indicate the country
458 unambiguously.
460 For example, many countries use the dollar as their monetary unit, and
461 when dealing with international currencies it's important to specify
462 that one is dealing with (say) Canadian dollars instead of U.S. dollars
463 or Australian dollars.  But when the context is known to be Canada,
464 there is no need to make this explicit---dollar amounts are implicitly
465 assumed to be in Canadian dollars.
467 @table @code
468 @item char *currency_symbol
469 The local currency symbol for the selected locale.
471 In the standard @samp{C} locale, this member has a value of @code{""}
472 (the empty string), meaning ``unspecified''.  The ANSI standard doesn't
473 say what to do when you find this value; we recommend you simply print
474 the empty string as you would print any other string found in the
475 appropriate member.
477 @item char *int_curr_symbol
478 The international currency symbol for the selected locale.
480 The value of @code{int_curr_symbol} should normally consist of a
481 three-letter abbreviation determined by the international standard
482 @cite{ISO 4217 Codes for the Representation of Currency and Funds},
483 followed by a one-character separator (often a space).
485 In the standard @samp{C} locale, this member has a value of @code{""}
486 (the empty string), meaning ``unspecified''.  We recommend you simply
487 print the empty string as you would print any other string found in the
488 appropriate member.
490 @item char p_cs_precedes
491 @itemx char n_cs_precedes
492 These members are @code{1} if the @code{currency_symbol} string should
493 precede the value of a monetary amount, or @code{0} if the string should
494 follow the value.  The @code{p_cs_precedes} member applies to positive
495 amounts (or zero), and the @code{n_cs_precedes} member applies to
496 negative amounts.
498 In the standard @samp{C} locale, both of these members have a value of
499 @code{CHAR_MAX}, meaning ``unspecified''.  The ANSI standard doesn't say
500 what to do when you find this value, but we recommend printing the
501 currency symbol before the amount.  That's right for most countries.
502 In other words, treat all nonzero values alike in these members.
504 The POSIX standard says that these two members apply to the
505 @code{int_curr_symbol} as well as the @code{currency_symbol}.  The ANSI
506 C standard seems to imply that they should apply only to the
507 @code{currency_symbol}---so the @code{int_curr_symbol} should always
508 precede the amount.
510 We can only guess which of these (if either) matches the usual
511 conventions for printing international currency symbols.  Our guess is
512 that they should always preceed the amount.  If we find out a reliable
513 answer, we will put it here.
515 @item char p_sep_by_space
516 @itemx char n_sep_by_space
517 These members are @code{1} if a space should appear between the
518 @code{currency_symbol} string and the amount, or @code{0} if no space
519 should appear.  The @code{p_sep_by_space} member applies to positive
520 amounts (or zero), and the @code{n_sep_by_space} member applies to
521 negative amounts.
523 In the standard @samp{C} locale, both of these members have a value of
524 @code{CHAR_MAX}, meaning ``unspecified''.  The ANSI standard doesn't say
525 what you should do when you find this value; we suggest you treat it as
526 one (print a space).  In other words, treat all nonzero values alike in
527 these members.
529 These members apply only to @code{currency_symbol}.  When you use
530 @code{int_curr_symbol}, you never print an additional space, because
531 @code{int_curr_symbol} itself contains the appropriate separator.
533 The POSIX standard says that these two members apply to the
534 @code{int_curr_symbol} as well as the @code{currency_symbol}.  But an
535 example in the ANSI C standard clearly implies that they should apply
536 only to the @code{currency_symbol}---that the @code{int_curr_symbol}
537 contains any appropriate separator, so you should never print an
538 additional space.
540 Based on what we know now, we recommend you ignore these members when
541 printing international currency symbols, and print no extra space.
542 @end table
544 @node Sign of Money Amount,  , Currency Symbol, Numeric Formatting
545 @subsection Printing the Sign of an Amount of Money
547 These members of the @code{struct lconv} structure specify how to print
548 the sign (if any) in a monetary value.
550 @table @code
551 @item char *positive_sign
552 @itemx char *negative_sign
553 These are strings used to indicate positive (or zero) and negative
554 (respectively) monetary quantities.
556 In the standard @samp{C} locale, both of these members have a value of
557 @code{""} (the empty string), meaning ``unspecified''.
559 The ANSI standard doesn't say what to do when you find this value; we
560 recommend printing @code{positive_sign} as you find it, even if it is
561 empty.  For a negative value, print @code{negative_sign} as you find it
562 unless both it and @code{positive_sign} are empty, in which case print
563 @samp{-} instead.  (Failing to indicate the sign at all seems rather
564 unreasonable.)
566 @item char p_sign_posn
567 @itemx char n_sign_posn
568 These members have values that are small integers indicating how to
569 position the sign for nonnegative and negative monetary quantities,
570 respectively.  (The string used by the sign is what was specified with
571 @code{positive_sign} or @code{negative_sign}.)  The possible values are
572 as follows:
574 @table @code
575 @item 0
576 The currency symbol and quantity should be surrounded by parentheses.
578 @item 1
579 Print the sign string before the quantity and currency symbol.
581 @item 2
582 Print the sign string after the quantity and currency symbol.
584 @item 3
585 Print the sign string right before the currency symbol.
587 @item 4
588 Print the sign string right after the currency symbol.
590 @item CHAR_MAX
591 ``Unspecified''.  Both members have this value in the standard
592 @samp{C} locale.
593 @end table
595 The ANSI standard doesn't say what you should do when the value is
596 @code{CHAR_MAX}.  We recommend you print the sign after the currency
597 symbol.
598 @end table
600 It is not clear whether you should let these members apply to the
601 international currency format or not.  POSIX says you should, but
602 intuition plus the examples in the ANSI C standard suggest you should
603 not.  We hope that someone who knows well the conventions for formatting
604 monetary quantities will tell us what we should recommend.