blob: 319d893ec3535488f03221f151d7b0005ed39029 [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{locale} ---
Fred Drakec3845a11999-04-21 17:18:04 +00002 Internationalization services}
3
Fred Drakeb91e9341998-07-23 17:59:49 +00004\declaremodule{standard}{locale}
Fred Drakeb91e9341998-07-23 17:59:49 +00005\modulesynopsis{Internationalization services.}
Martin v. Löwis338bcbc2003-04-18 22:04:34 +00006\moduleauthor{Martin von L\"owis}{martin@v.loewis.de}
7\sectionauthor{Martin von L\"owis}{martin@v.loewis.de}
Fred Drakeb91e9341998-07-23 17:59:49 +00008
Guido van Rossumbc12f781997-11-20 21:04:27 +00009
Fred Drake1491cac2000-10-25 20:59:52 +000010The \module{locale} module opens access to the \POSIX{} locale
11database and functionality. The \POSIX{} locale mechanism allows
12programmers to deal with certain cultural issues in an application,
13without requiring the programmer to know all the specifics of each
14country where the software is executed.
Guido van Rossumbc12f781997-11-20 21:04:27 +000015
Fred Drake193338a1998-03-10 04:23:12 +000016The \module{locale} module is implemented on top of the
17\module{_locale}\refbimodindex{_locale} module, which in turn uses an
Fred Drakec3845a11999-04-21 17:18:04 +000018ANSI C locale implementation if available.
Guido van Rossumbc12f781997-11-20 21:04:27 +000019
Fred Drake193338a1998-03-10 04:23:12 +000020The \module{locale} module defines the following exception and
21functions:
Guido van Rossumbc12f781997-11-20 21:04:27 +000022
Guido van Rossumbc12f781997-11-20 21:04:27 +000023
Fred Drake1491cac2000-10-25 20:59:52 +000024\begin{excdesc}{Error}
25 Exception raised when \function{setlocale()} fails.
26\end{excdesc}
Guido van Rossumbc12f781997-11-20 21:04:27 +000027
Fred Drake1491cac2000-10-25 20:59:52 +000028\begin{funcdesc}{setlocale}{category\optional{, locale}}
29 If \var{locale} is specified, it may be a string, a tuple of the
30 form \code{(\var{language code}, \var{encoding})}, or \code{None}.
31 If it is a tuple, it is converted to a string using the locale
32 aliasing engine. If \var{locale} is given and not \code{None},
33 \function{setlocale()} modifies the locale setting for the
34 \var{category}. The available categories are listed in the data
35 description below. The value is the name of a locale. An empty
36 string specifies the user's default settings. If the modification of
37 the locale fails, the exception \exception{Error} is raised. If
38 successful, the new locale setting is returned.
Guido van Rossumbc12f781997-11-20 21:04:27 +000039
Fred Drake1491cac2000-10-25 20:59:52 +000040 If \var{locale} is omitted or \code{None}, the current setting for
41 \var{category} is returned.
42
43 \function{setlocale()} is not thread safe on most systems.
44 Applications typically start with a call of
45
Fred Drake19479911998-02-13 06:58:54 +000046\begin{verbatim}
Guido van Rossumbc12f781997-11-20 21:04:27 +000047import locale
Fred Drakec01f6e62000-11-30 07:13:58 +000048locale.setlocale(locale.LC_ALL, '')
Fred Drake19479911998-02-13 06:58:54 +000049\end{verbatim}
Guido van Rossumbc12f781997-11-20 21:04:27 +000050
Fred Drake1491cac2000-10-25 20:59:52 +000051 This sets the locale for all categories to the user's default
52 setting (typically specified in the \envvar{LANG} environment
53 variable). If the locale is not changed thereafter, using
54 multithreading should not cause problems.
55
56 \versionchanged[Added support for tuple values of the \var{locale}
57 parameter]{2.0}
58\end{funcdesc}
Fred Drake193338a1998-03-10 04:23:12 +000059
Guido van Rossumbc12f781997-11-20 21:04:27 +000060\begin{funcdesc}{localeconv}{}
Raymond Hettinger999b57c2003-08-25 04:28:05 +000061 Returns the database of the local conventions as a dictionary.
Fred Drake1491cac2000-10-25 20:59:52 +000062 This dictionary has the following strings as keys:
Fred Drake193338a1998-03-10 04:23:12 +000063
Georg Brandlb89316f2006-05-17 15:51:16 +000064 \begin{tableiii}{l|l|p{3in}}{constant}{Category}{Key}{Meaning}
Fred Drakec01f6e62000-11-30 07:13:58 +000065 \lineiii{LC_NUMERIC}{\code{'decimal_point'}}
66 {Decimal point character.}
67 \lineiii{}{\code{'grouping'}}
68 {Sequence of numbers specifying which relative positions
69 the \code{'thousands_sep'} is expected. If the sequence is
70 terminated with \constant{CHAR_MAX}, no further grouping
71 is performed. If the sequence terminates with a \code{0},
72 the last group size is repeatedly used.}
73 \lineiii{}{\code{'thousands_sep'}}
74 {Character used between groups.}\hline
75 \lineiii{LC_MONETARY}{\code{'int_curr_symbol'}}
76 {International currency symbol.}
77 \lineiii{}{\code{'currency_symbol'}}
78 {Local currency symbol.}
Georg Brandlb89316f2006-05-17 15:51:16 +000079 \lineiii{}{\code{'p_cs_precedes/n_cs_precedes'}}
80 {Whether the currency symbol precedes the value (for positive resp.
81 negative values).}
82 \lineiii{}{\code{'p_sep_by_space/n_sep_by_space'}}
83 {Whether the currency symbol is separated from the value
84 by a space (for positive resp. negative values).}
Fred Drakec01f6e62000-11-30 07:13:58 +000085 \lineiii{}{\code{'mon_decimal_point'}}
86 {Decimal point used for monetary values.}
Georg Brandlb89316f2006-05-17 15:51:16 +000087 \lineiii{}{\code{'frac_digits'}}
88 {Number of fractional digits used in local formatting
89 of monetary values.}
90 \lineiii{}{\code{'int_frac_digits'}}
91 {Number of fractional digits used in international
92 formatting of monetary values.}
Fred Drakec01f6e62000-11-30 07:13:58 +000093 \lineiii{}{\code{'mon_thousands_sep'}}
94 {Group separator used for monetary values.}
95 \lineiii{}{\code{'mon_grouping'}}
96 {Equivalent to \code{'grouping'}, used for monetary
97 values.}
98 \lineiii{}{\code{'positive_sign'}}
99 {Symbol used to annotate a positive monetary value.}
100 \lineiii{}{\code{'negative_sign'}}
Neal Norwitzf9f0b212004-07-29 03:48:59 +0000101 {Symbol used to annotate a negative monetary value.}
Georg Brandlb89316f2006-05-17 15:51:16 +0000102 \lineiii{}{\code{'p_sign_posn/n_sign_posn'}}
103 {The position of the sign (for positive resp. negative values), see below.}
Fred Drakec01f6e62000-11-30 07:13:58 +0000104 \end{tableiii}
Georg Brandlb89316f2006-05-17 15:51:16 +0000105
106 All numeric values can be set to \constant{CHAR_MAX} to indicate that
107 there is no value specified in this locale.
Fred Drake193338a1998-03-10 04:23:12 +0000108
Fred Drakec01f6e62000-11-30 07:13:58 +0000109 The possible values for \code{'p_sign_posn'} and
110 \code{'n_sign_posn'} are given below.
Fred Drake1491cac2000-10-25 20:59:52 +0000111
112 \begin{tableii}{c|l}{code}{Value}{Explanation}
113 \lineii{0}{Currency and value are surrounded by parentheses.}
114 \lineii{1}{The sign should precede the value and currency symbol.}
115 \lineii{2}{The sign should follow the value and currency symbol.}
116 \lineii{3}{The sign should immediately precede the value.}
117 \lineii{4}{The sign should immediately follow the value.}
Georg Brandlb89316f2006-05-17 15:51:16 +0000118 \lineii{\constant{CHAR_MAX}}{Nothing is specified in this locale.}
Fred Drake1491cac2000-10-25 20:59:52 +0000119 \end{tableii}
Guido van Rossumbc12f781997-11-20 21:04:27 +0000120\end{funcdesc}
121
Martin v. Löwis9b75dca2001-08-10 13:58:50 +0000122\begin{funcdesc}{nl_langinfo}{option}
123
124Return some locale-specific information as a string. This function is
125not available on all systems, and the set of possible options might
126also vary across platforms. The possible argument values are numbers,
127for which symbolic constants are available in the locale module.
128
129\end{funcdesc}
130
Fred Drake1491cac2000-10-25 20:59:52 +0000131\begin{funcdesc}{getdefaultlocale}{\optional{envvars}}
132 Tries to determine the default locale settings and returns
133 them as a tuple of the form \code{(\var{language code},
134 \var{encoding})}.
135
136 According to \POSIX, a program which has not called
137 \code{setlocale(LC_ALL, '')} runs using the portable \code{'C'}
138 locale. Calling \code{setlocale(LC_ALL, '')} lets it use the
139 default locale as defined by the \envvar{LANG} variable. Since we
140 do not want to interfere with the current locale setting we thus
141 emulate the behavior in the way described above.
142
143 To maintain compatibility with other platforms, not only the
144 \envvar{LANG} variable is tested, but a list of variables given as
145 envvars parameter. The first found to be defined will be
146 used. \var{envvars} defaults to the search path used in GNU gettext;
147 it must always contain the variable name \samp{LANG}. The GNU
148 gettext search path contains \code{'LANGUAGE'}, \code{'LC_ALL'},
Fred Drakef69868f2001-07-20 19:03:44 +0000149 \code{'LC_CTYPE'}, and \code{'LANG'}, in that order.
Fred Drake1491cac2000-10-25 20:59:52 +0000150
151 Except for the code \code{'C'}, the language code corresponds to
152 \rfc{1766}. \var{language code} and \var{encoding} may be
153 \code{None} if their values cannot be determined.
154 \versionadded{2.0}
155\end{funcdesc}
156
157\begin{funcdesc}{getlocale}{\optional{category}}
158 Returns the current setting for the given locale category as
Fred Drakeefb90972002-06-13 17:54:06 +0000159 sequence containing \var{language code}, \var{encoding}.
160 \var{category} may be one of the \constant{LC_*} values except
161 \constant{LC_ALL}. It defaults to \constant{LC_CTYPE}.
Fred Drake1491cac2000-10-25 20:59:52 +0000162
163 Except for the code \code{'C'}, the language code corresponds to
164 \rfc{1766}. \var{language code} and \var{encoding} may be
165 \code{None} if their values cannot be determined.
166 \versionadded{2.0}
167\end{funcdesc}
168
Martin v. Löwisf0a46682002-11-03 17:20:12 +0000169\begin{funcdesc}{getpreferredencoding}{\optional{do_setlocale}}
170 Return the encoding used for text data, according to user
171 preferences. User preferences are expressed differently on
172 different systems, and might not be available programmatically on
173 some systems, so this function only returns a guess.
174
175 On some systems, it is necessary to invoke \function{setlocale}
176 to obtain the user preferences, so this function is not thread-safe.
177 If invoking setlocale is not necessary or desired, \var{do_setlocale}
178 should be set to \code{False}.
179
180 \versionadded{2.3}
181\end{funcdesc}
182
Fred Drake1491cac2000-10-25 20:59:52 +0000183\begin{funcdesc}{normalize}{localename}
184 Returns a normalized locale code for the given locale name. The
185 returned locale code is formatted for use with
186 \function{setlocale()}. If normalization fails, the original name
187 is returned unchanged.
188
189 If the given encoding is not known, the function defaults to
190 the default encoding for the locale code just like
191 \function{setlocale()}.
192 \versionadded{2.0}
193\end{funcdesc}
194
195\begin{funcdesc}{resetlocale}{\optional{category}}
196 Sets the locale for \var{category} to the default setting.
197
198 The default setting is determined by calling
199 \function{getdefaultlocale()}. \var{category} defaults to
200 \constant{LC_ALL}.
201 \versionadded{2.0}
202\end{funcdesc}
203
204\begin{funcdesc}{strcoll}{string1, string2}
205 Compares two strings according to the current
206 \constant{LC_COLLATE} setting. As any other compare function,
207 returns a negative, or a positive value, or \code{0}, depending on
208 whether \var{string1} collates before or after \var{string2} or is
209 equal to it.
Guido van Rossumbc12f781997-11-20 21:04:27 +0000210\end{funcdesc}
211
212\begin{funcdesc}{strxfrm}{string}
Fred Drake1491cac2000-10-25 20:59:52 +0000213 Transforms a string to one that can be used for the built-in
214 function \function{cmp()}\bifuncindex{cmp}, and still returns
215 locale-aware results. This function can be used when the same
216 string is compared repeatedly, e.g. when collating a sequence of
217 strings.
Guido van Rossumbc12f781997-11-20 21:04:27 +0000218\end{funcdesc}
219
Georg Brandlb89316f2006-05-17 15:51:16 +0000220\begin{funcdesc}{format}{format, val\optional{, grouping\optional{, monetary}}}
Fred Drake1491cac2000-10-25 20:59:52 +0000221 Formats a number \var{val} according to the current
222 \constant{LC_NUMERIC} setting. The format follows the conventions
223 of the \code{\%} operator. For floating point values, the decimal
224 point is modified if appropriate. If \var{grouping} is true, also
225 takes the grouping into account.
Georg Brandlb89316f2006-05-17 15:51:16 +0000226
227 If \var{monetary} is true, the conversion uses monetary thousands
228 separator and grouping strings.
229
230 Please note that this function will only work for exactly one \%char
231 specifier. For whole format strings, use \function{format_string()}.
232
233 \versionchanged[Added the \var{monetary} parameter]{2.5}
234\end{funcdesc}
235
236\begin{funcdesc}{format_string}{format, val\optional{, grouping}}
237 Processes formatting specifiers as in \code{format \% val},
238 but takes the current locale settings into account.
239
240 \versionadded{2.5}
241\end{funcdesc}
242
243\begin{funcdesc}{currency}{val\optional{, symbol\optional{, grouping\optional{, international}}}}
244 Formats a number \var{val} according to the current \constant{LC_MONETARY}
245 settings.
246
247 The returned string includes the currency symbol if \var{symbol} is true,
248 which is the default.
249 If \var{grouping} is true (which is not the default), grouping is done with
250 the value.
251 If \var{international} is true (which is not the default), the international
252 currency symbol is used.
253
254 Note that this function will not work with the `C' locale, so you have to set
255 a locale via \function{setlocale()} first.
256
257 \versionadded{2.5}
Guido van Rossumbc12f781997-11-20 21:04:27 +0000258\end{funcdesc}
259
260\begin{funcdesc}{str}{float}
Fred Drake1491cac2000-10-25 20:59:52 +0000261 Formats a floating point number using the same format as the
262 built-in function \code{str(\var{float})}, but takes the decimal
263 point into account.
Guido van Rossumbc12f781997-11-20 21:04:27 +0000264\end{funcdesc}
265
266\begin{funcdesc}{atof}{string}
Fred Drake1491cac2000-10-25 20:59:52 +0000267 Converts a string to a floating point number, following the
268 \constant{LC_NUMERIC} settings.
Guido van Rossumbc12f781997-11-20 21:04:27 +0000269\end{funcdesc}
270
271\begin{funcdesc}{atoi}{string}
Fred Drake1491cac2000-10-25 20:59:52 +0000272 Converts a string to an integer, following the
273 \constant{LC_NUMERIC} conventions.
Guido van Rossumbc12f781997-11-20 21:04:27 +0000274\end{funcdesc}
275
276\begin{datadesc}{LC_CTYPE}
Fred Drake304474f1997-12-17 15:30:07 +0000277\refstmodindex{string}
Fred Drake1491cac2000-10-25 20:59:52 +0000278 Locale category for the character type functions. Depending on the
279 settings of this category, the functions of module
280 \refmodule{string} dealing with case change their behaviour.
Guido van Rossumbc12f781997-11-20 21:04:27 +0000281\end{datadesc}
282
283\begin{datadesc}{LC_COLLATE}
Fred Drake1491cac2000-10-25 20:59:52 +0000284 Locale category for sorting strings. The functions
285 \function{strcoll()} and \function{strxfrm()} of the
286 \module{locale} module are affected.
Guido van Rossumbc12f781997-11-20 21:04:27 +0000287\end{datadesc}
288
289\begin{datadesc}{LC_TIME}
Fred Drake1491cac2000-10-25 20:59:52 +0000290 Locale category for the formatting of time. The function
291 \function{time.strftime()} follows these conventions.
Guido van Rossumbc12f781997-11-20 21:04:27 +0000292\end{datadesc}
293
294\begin{datadesc}{LC_MONETARY}
Fred Drake1491cac2000-10-25 20:59:52 +0000295 Locale category for formatting of monetary values. The available
296 options are available from the \function{localeconv()} function.
Guido van Rossumbc12f781997-11-20 21:04:27 +0000297\end{datadesc}
298
299\begin{datadesc}{LC_MESSAGES}
Fred Drake1491cac2000-10-25 20:59:52 +0000300 Locale category for message display. Python currently does not
301 support application specific locale-aware messages. Messages
302 displayed by the operating system, like those returned by
303 \function{os.strerror()} might be affected by this category.
Guido van Rossumbc12f781997-11-20 21:04:27 +0000304\end{datadesc}
305
306\begin{datadesc}{LC_NUMERIC}
Fred Drake1491cac2000-10-25 20:59:52 +0000307 Locale category for formatting numbers. The functions
308 \function{format()}, \function{atoi()}, \function{atof()} and
309 \function{str()} of the \module{locale} module are affected by that
310 category. All other numeric formatting operations are not
311 affected.
Guido van Rossumbc12f781997-11-20 21:04:27 +0000312\end{datadesc}
313
314\begin{datadesc}{LC_ALL}
Fred Drake1491cac2000-10-25 20:59:52 +0000315 Combination of all locale settings. If this flag is used when the
316 locale is changed, setting the locale for all categories is
317 attempted. If that fails for any category, no category is changed at
318 all. When the locale is retrieved using this flag, a string
319 indicating the setting for all categories is returned. This string
320 can be later used to restore the settings.
Guido van Rossumbc12f781997-11-20 21:04:27 +0000321\end{datadesc}
322
323\begin{datadesc}{CHAR_MAX}
Fred Drake1491cac2000-10-25 20:59:52 +0000324 This is a symbolic constant used for different values returned by
325 \function{localeconv()}.
Guido van Rossumbc12f781997-11-20 21:04:27 +0000326\end{datadesc}
327
Martin v. Löwis9b75dca2001-08-10 13:58:50 +0000328The \function{nl_langinfo} function accepts one of the following keys.
329Most descriptions are taken from the corresponding description in the
330GNU C library.
331
332\begin{datadesc}{CODESET}
333Return a string with the name of the character encoding used in the
334selected locale.
335\end{datadesc}
336
337\begin{datadesc}{D_T_FMT}
338Return a string that can be used as a format string for strftime(3) to
339represent time and date in a locale-specific way.
340\end{datadesc}
341
342\begin{datadesc}{D_FMT}
343Return a string that can be used as a format string for strftime(3) to
344represent a date in a locale-specific way.
345\end{datadesc}
346
347\begin{datadesc}{T_FMT}
348Return a string that can be used as a format string for strftime(3) to
349represent a time in a locale-specific way.
350\end{datadesc}
351
352\begin{datadesc}{T_FMT_AMPM}
353The return value can be used as a format string for `strftime' to
354represent time in the am/pm format.
355\end{datadesc}
356
357\begin{datadesc}{DAY_1 ... DAY_7}
Fred Drake0aa811c2001-10-20 04:24:09 +0000358Return name of the n-th day of the week. \warning{This
Fred Drakeb802a1e2001-09-27 04:16:27 +0000359follows the US convention of \constant{DAY_1} being Sunday, not the
360international convention (ISO 8601) that Monday is the first day of
Fred Drake0aa811c2001-10-20 04:24:09 +0000361the week.}
Martin v. Löwis9b75dca2001-08-10 13:58:50 +0000362\end{datadesc}
363
364\begin{datadesc}{ABDAY_1 ... ABDAY_7}
365Return abbreviated name of the n-th day of the week.
366\end{datadesc}
367
368\begin{datadesc}{MON_1 ... MON_12}
369Return name of the n-th month.
370\end{datadesc}
371
372\begin{datadesc}{ABMON_1 ... ABMON_12}
373Return abbreviated name of the n-th month.
374\end{datadesc}
375
376\begin{datadesc}{RADIXCHAR}
377Return radix character (decimal dot, decimal comma, etc.)
378\end{datadesc}
379
380\begin{datadesc}{THOUSEP}
381Return separator character for thousands (groups of three digits).
382\end{datadesc}
383
384\begin{datadesc}{YESEXPR}
385Return a regular expression that can be used with the regex
386function to recognize a positive response to a yes/no question.
Fred Drake0aa811c2001-10-20 04:24:09 +0000387\warning{The expression is in the syntax suitable for the
Fred Drakeb802a1e2001-09-27 04:16:27 +0000388\cfunction{regex()} function from the C library, which might differ
Fred Drake0aa811c2001-10-20 04:24:09 +0000389from the syntax used in \refmodule{re}.}
Martin v. Löwis9b75dca2001-08-10 13:58:50 +0000390\end{datadesc}
391
392\begin{datadesc}{NOEXPR}
393Return a regular expression that can be used with the regex(3)
394function to recognize a negative response to a yes/no question.
395\end{datadesc}
396
397\begin{datadesc}{CRNCYSTR}
398Return the currency symbol, preceded by "-" if the symbol should
399appear before the value, "+" if the symbol should appear after the
400value, or "." if the symbol should replace the radix character.
401\end{datadesc}
402
403\begin{datadesc}{ERA}
404The return value represents the era used in the current locale.
405
406Most locales do not define this value. An example of a locale which
407does define this value is the Japanese one. In Japan, the traditional
408representation of dates includes the name of the era corresponding to
409the then-emperor's reign.
410
411Normally it should not be necessary to use this value directly.
412Specifying the \code{E} modifier in their format strings causes the
413\function{strftime} function to use this information. The format of the
414returned string is not specified, and therefore you should not assume
415knowledge of it on different systems.
416\end{datadesc}
417
418\begin{datadesc}{ERA_YEAR}
419The return value gives the year in the relevant era of the locale.
420\end{datadesc}
421
422\begin{datadesc}{ERA_D_T_FMT}
423This return value can be used as a format string for
424\function{strftime} to represent dates and times in a locale-specific
425era-based way.
426\end{datadesc}
427
428\begin{datadesc}{ERA_D_FMT}
429This return value can be used as a format string for
430\function{strftime} to represent time in a locale-specific era-based
431way.
432\end{datadesc}
433
434\begin{datadesc}{ALT_DIGITS}
435The return value is a representation of up to 100 values used to
436represent the values 0 to 99.
437\end{datadesc}
438
Guido van Rossumbc12f781997-11-20 21:04:27 +0000439Example:
440
Fred Drake19479911998-02-13 06:58:54 +0000441\begin{verbatim}
Guido van Rossumbc12f781997-11-20 21:04:27 +0000442>>> import locale
Neal Norwitz2562ff22004-10-17 16:36:05 +0000443>>> loc = locale.getlocale(locale.LC_ALL) # get current locale
Martin v. Löwis25f90d52003-09-03 04:50:13 +0000444>>> locale.setlocale(locale.LC_ALL, 'de_DE') # use German locale; name might vary with platform
Ka-Ping Yeefa004ad2001-01-24 17:19:08 +0000445>>> locale.strcoll('f\xe4n', 'foo') # compare a string containing an umlaut
Fred Drakec01f6e62000-11-30 07:13:58 +0000446>>> locale.setlocale(locale.LC_ALL, '') # use user's preferred locale
447>>> locale.setlocale(locale.LC_ALL, 'C') # use default (C) locale
Guido van Rossumd028ca91998-02-22 04:41:51 +0000448>>> locale.setlocale(locale.LC_ALL, loc) # restore saved locale
Fred Drake19479911998-02-13 06:58:54 +0000449\end{verbatim}
Guido van Rossum3ffb7151998-02-22 04:23:51 +0000450
Fred Drake1491cac2000-10-25 20:59:52 +0000451
Guido van Rossum3ffb7151998-02-22 04:23:51 +0000452\subsection{Background, details, hints, tips and caveats}
453
454The C standard defines the locale as a program-wide property that may
455be relatively expensive to change. On top of that, some
456implementation are broken in such a way that frequent locale changes
457may cause core dumps. This makes the locale somewhat painful to use
458correctly.
459
Fred Drake9fee0711998-04-03 06:21:23 +0000460Initially, when a program is started, the locale is the \samp{C} locale, no
Guido van Rossum3ffb7151998-02-22 04:23:51 +0000461matter what the user's preferred locale is. The program must
462explicitly say that it wants the user's preferred locale settings by
Fred Drakec01f6e62000-11-30 07:13:58 +0000463calling \code{setlocale(LC_ALL, '')}.
Guido van Rossum3ffb7151998-02-22 04:23:51 +0000464
Fred Drake193338a1998-03-10 04:23:12 +0000465It is generally a bad idea to call \function{setlocale()} in some library
Guido van Rossum3ffb7151998-02-22 04:23:51 +0000466routine, since as a side effect it affects the entire program. Saving
467and restoring it is almost as bad: it is expensive and affects other
468threads that happen to run before the settings have been restored.
469
470If, when coding a module for general use, you need a locale
471independent version of an operation that is affected by the locale
Raymond Hettingerbf3a7522003-05-12 03:23:51 +0000472(such as \function{string.lower()}, or certain formats used with
473\function{time.strftime()}), you will have to find a way to do it
Fred Drake193338a1998-03-10 04:23:12 +0000474without using the standard library routine. Even better is convincing
475yourself that using locale settings is okay. Only as a last resort
Fred Drake9fee0711998-04-03 06:21:23 +0000476should you document that your module is not compatible with
477non-\samp{C} locale settings.
Guido van Rossum3ffb7151998-02-22 04:23:51 +0000478
Fred Drake193338a1998-03-10 04:23:12 +0000479The case conversion functions in the
Fred Drakee9735ac2001-05-10 15:05:03 +0000480\refmodule{string}\refstmodindex{string} module are affected by the
481locale settings. When a call to the \function{setlocale()} function
482changes the \constant{LC_CTYPE} settings, the variables
Fred Drake193338a1998-03-10 04:23:12 +0000483\code{string.lowercase}, \code{string.uppercase} and
Martin v. Löwisf7f888a2007-02-13 12:08:34 +0000484\code{string.letters} are recalculated. Note that code that uses
Fred Drakee9735ac2001-05-10 15:05:03 +0000485these variable through `\keyword{from} ... \keyword{import} ...',
486e.g.\ \code{from string import letters}, is not affected by subsequent
487\function{setlocale()} calls.
Guido van Rossum3ffb7151998-02-22 04:23:51 +0000488
489The only way to perform numeric operations according to the locale
490is to use the special functions defined by this module:
Fred Drake193338a1998-03-10 04:23:12 +0000491\function{atof()}, \function{atoi()}, \function{format()},
492\function{str()}.
Guido van Rossum3ffb7151998-02-22 04:23:51 +0000493
Fred Drake1491cac2000-10-25 20:59:52 +0000494\subsection{For extension writers and programs that embed Python
495 \label{embedding-locale}}
Guido van Rossum3ffb7151998-02-22 04:23:51 +0000496
Fred Drake193338a1998-03-10 04:23:12 +0000497Extension modules should never call \function{setlocale()}, except to
498find out what the current locale is. But since the return value can
499only be used portably to restore it, that is not very useful (except
Fred Drake9fee0711998-04-03 06:21:23 +0000500perhaps to find out whether or not the locale is \samp{C}).
Guido van Rossum3ffb7151998-02-22 04:23:51 +0000501
Fred Drake193338a1998-03-10 04:23:12 +0000502When Python code uses the \module{locale} module to change the locale,
Fred Draked8a41e61999-02-19 17:54:10 +0000503this also affects the embedding application. If the embedding
Guido van Rossum3ffb7151998-02-22 04:23:51 +0000504application doesn't want this to happen, it should remove the
Fred Drake193338a1998-03-10 04:23:12 +0000505\module{_locale} extension module (which does all the work) from the
506table of built-in modules in the \file{config.c} file, and make sure
507that the \module{_locale} module is not accessible as a shared library.
Martin v. Löwis2e64c342002-03-27 18:49:02 +0000508
Fred Drakee3a3ceb2002-03-28 12:40:45 +0000509
510\subsection{Access to message catalogs \label{locale-gettext}}
Martin v. Löwis2e64c342002-03-27 18:49:02 +0000511
512The locale module exposes the C library's gettext interface on systems
Fred Drakee3a3ceb2002-03-28 12:40:45 +0000513that provide this interface. It consists of the functions
514\function{gettext()}, \function{dgettext()}, \function{dcgettext()},
Gustavo Niemeyer7bd33c52004-07-22 18:44:01 +0000515\function{textdomain()}, \function{bindtextdomain()}, and
516\function{bind_textdomain_codeset()}. These are similar to the same
517functions in the \refmodule{gettext} module, but use the C library's
518binary format for message catalogs, and the C library's search
519algorithms for locating message catalogs.
Martin v. Löwis2e64c342002-03-27 18:49:02 +0000520
521Python applications should normally find no need to invoke these
Fred Drakee3a3ceb2002-03-28 12:40:45 +0000522functions, and should use \refmodule{gettext} instead. A known
523exception to this rule are applications that link use additional C
524libraries which internally invoke \cfunction{gettext()} or
Gustavo Niemeyer7bd33c52004-07-22 18:44:01 +0000525\function{dcgettext()}. For these applications, it may be necessary to
Fred Drakee3a3ceb2002-03-28 12:40:45 +0000526bind the text domain, so that the libraries can properly locate their
527message catalogs.