blob: a3a7b487b9cb07b8a676fbbaa7bf80480159ab0d [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001:mod:`locale` --- Internationalization services
2===============================================
3
4.. module:: locale
5 :synopsis: Internationalization services.
6.. moduleauthor:: Martin von Löwis <martin@v.loewis.de>
7.. sectionauthor:: Martin von Löwis <martin@v.loewis.de>
8
9
10The :mod:`locale` module opens access to the POSIX locale database and
11functionality. The POSIX locale mechanism allows programmers to deal with
12certain cultural issues in an application, without requiring the programmer to
13know all the specifics of each country where the software is executed.
14
15.. index:: module: _locale
16
17The :mod:`locale` module is implemented on top of the :mod:`_locale` module,
18which in turn uses an ANSI C locale implementation if available.
19
20The :mod:`locale` module defines the following exception and functions:
21
22
23.. exception:: Error
24
25 Exception raised when :func:`setlocale` fails.
26
27
Georg Brandlcd7f32b2009-06-08 09:13:45 +000028.. function:: setlocale(category, locale=None)
Georg Brandl116aa622007-08-15 14:28:22 +000029
30 If *locale* is specified, it may be a string, a tuple of the form ``(language
31 code, encoding)``, or ``None``. If it is a tuple, it is converted to a string
32 using the locale aliasing engine. If *locale* is given and not ``None``,
33 :func:`setlocale` modifies the locale setting for the *category*. The available
34 categories are listed in the data description below. The value is the name of a
35 locale. An empty string specifies the user's default settings. If the
36 modification of the locale fails, the exception :exc:`Error` is raised. If
37 successful, the new locale setting is returned.
38
39 If *locale* is omitted or ``None``, the current setting for *category* is
40 returned.
41
Georg Brandlf285bcc2010-10-19 21:07:16 +000042 :func:`setlocale` is not thread-safe on most systems. Applications typically
Georg Brandl116aa622007-08-15 14:28:22 +000043 start with a call of ::
44
45 import locale
46 locale.setlocale(locale.LC_ALL, '')
47
48 This sets the locale for all categories to the user's default setting (typically
49 specified in the :envvar:`LANG` environment variable). If the locale is not
50 changed thereafter, using multithreading should not cause problems.
51
Georg Brandl116aa622007-08-15 14:28:22 +000052
53.. function:: localeconv()
54
55 Returns the database of the local conventions as a dictionary. This dictionary
56 has the following strings as keys:
57
58 +----------------------+-------------------------------------+--------------------------------+
59 | Category | Key | Meaning |
60 +======================+=====================================+================================+
61 | :const:`LC_NUMERIC` | ``'decimal_point'`` | Decimal point character. |
62 +----------------------+-------------------------------------+--------------------------------+
63 | | ``'grouping'`` | Sequence of numbers specifying |
64 | | | which relative positions the |
65 | | | ``'thousands_sep'`` is |
66 | | | expected. If the sequence is |
67 | | | terminated with |
68 | | | :const:`CHAR_MAX`, no further |
69 | | | grouping is performed. If the |
70 | | | sequence terminates with a |
71 | | | ``0``, the last group size is |
72 | | | repeatedly used. |
73 +----------------------+-------------------------------------+--------------------------------+
74 | | ``'thousands_sep'`` | Character used between groups. |
75 +----------------------+-------------------------------------+--------------------------------+
76 | :const:`LC_MONETARY` | ``'int_curr_symbol'`` | International currency symbol. |
77 +----------------------+-------------------------------------+--------------------------------+
78 | | ``'currency_symbol'`` | Local currency symbol. |
79 +----------------------+-------------------------------------+--------------------------------+
80 | | ``'p_cs_precedes/n_cs_precedes'`` | Whether the currency symbol |
81 | | | precedes the value (for |
82 | | | positive resp. negative |
83 | | | values). |
84 +----------------------+-------------------------------------+--------------------------------+
85 | | ``'p_sep_by_space/n_sep_by_space'`` | Whether the currency symbol is |
86 | | | separated from the value by a |
87 | | | space (for positive resp. |
88 | | | negative values). |
89 +----------------------+-------------------------------------+--------------------------------+
90 | | ``'mon_decimal_point'`` | Decimal point used for |
91 | | | monetary values. |
92 +----------------------+-------------------------------------+--------------------------------+
93 | | ``'frac_digits'`` | Number of fractional digits |
94 | | | used in local formatting of |
95 | | | monetary values. |
96 +----------------------+-------------------------------------+--------------------------------+
97 | | ``'int_frac_digits'`` | Number of fractional digits |
98 | | | used in international |
99 | | | formatting of monetary values. |
100 +----------------------+-------------------------------------+--------------------------------+
101 | | ``'mon_thousands_sep'`` | Group separator used for |
102 | | | monetary values. |
103 +----------------------+-------------------------------------+--------------------------------+
104 | | ``'mon_grouping'`` | Equivalent to ``'grouping'``, |
105 | | | used for monetary values. |
106 +----------------------+-------------------------------------+--------------------------------+
107 | | ``'positive_sign'`` | Symbol used to annotate a |
108 | | | positive monetary value. |
109 +----------------------+-------------------------------------+--------------------------------+
110 | | ``'negative_sign'`` | Symbol used to annotate a |
111 | | | negative monetary value. |
112 +----------------------+-------------------------------------+--------------------------------+
113 | | ``'p_sign_posn/n_sign_posn'`` | The position of the sign (for |
114 | | | positive resp. negative |
115 | | | values), see below. |
116 +----------------------+-------------------------------------+--------------------------------+
117
118 All numeric values can be set to :const:`CHAR_MAX` to indicate that there is no
119 value specified in this locale.
120
121 The possible values for ``'p_sign_posn'`` and ``'n_sign_posn'`` are given below.
122
123 +--------------+-----------------------------------------+
124 | Value | Explanation |
125 +==============+=========================================+
126 | ``0`` | Currency and value are surrounded by |
127 | | parentheses. |
128 +--------------+-----------------------------------------+
129 | ``1`` | The sign should precede the value and |
130 | | currency symbol. |
131 +--------------+-----------------------------------------+
132 | ``2`` | The sign should follow the value and |
133 | | currency symbol. |
134 +--------------+-----------------------------------------+
135 | ``3`` | The sign should immediately precede the |
136 | | value. |
137 +--------------+-----------------------------------------+
138 | ``4`` | The sign should immediately follow the |
139 | | value. |
140 +--------------+-----------------------------------------+
141 | ``CHAR_MAX`` | Nothing is specified in this locale. |
142 +--------------+-----------------------------------------+
143
144
145.. function:: nl_langinfo(option)
146
Alexandre Vassalotti711ed4a2009-07-17 10:42:05 +0000147 Return some locale-specific information as a string. This function is not
148 available on all systems, and the set of possible options might also vary
149 across platforms. The possible argument values are numbers, for which
150 symbolic constants are available in the locale module.
151
152 The :func:`nl_langinfo` function accepts one of the following keys. Most
153 descriptions are taken from the corresponding description in the GNU C
154 library.
155
156 .. data:: CODESET
157
158 Get a string with the name of the character encoding used in the
159 selected locale.
160
161 .. data:: D_T_FMT
162
163 Get a string that can be used as a format string for :func:`strftime` to
164 represent time and date in a locale-specific way.
165
166 .. data:: D_FMT
167
168 Get a string that can be used as a format string for :func:`strftime` to
169 represent a date in a locale-specific way.
170
171 .. data:: T_FMT
172
173 Get a string that can be used as a format string for :func:`strftime` to
174 represent a time in a locale-specific way.
175
176 .. data:: T_FMT_AMPM
177
178 Get a format string for :func:`strftime` to represent time in the am/pm
179 format.
180
181 .. data:: DAY_1 ... DAY_7
182
183 Get the name of the n-th day of the week.
184
185 .. note::
186
187 This follows the US convention of :const:`DAY_1` being Sunday, not the
188 international convention (ISO 8601) that Monday is the first day of the
189 week.
190
191 .. data:: ABDAY_1 ... ABDAY_7
192
193 Get the abbreviated name of the n-th day of the week.
194
195 .. data:: MON_1 ... MON_12
196
197 Get the name of the n-th month.
198
199 .. data:: ABMON_1 ... ABMON_12
200
201 Get the abbreviated name of the n-th month.
202
203 .. data:: RADIXCHAR
204
205 Get the radix character (decimal dot, decimal comma, etc.)
206
207 .. data:: THOUSEP
208
209 Get the separator character for thousands (groups of three digits).
210
211 .. data:: YESEXPR
212
213 Get a regular expression that can be used with the regex function to
214 recognize a positive response to a yes/no question.
215
216 .. note::
217
Georg Brandl60203b42010-10-06 10:11:56 +0000218 The expression is in the syntax suitable for the :c:func:`regex` function
Alexandre Vassalotti711ed4a2009-07-17 10:42:05 +0000219 from the C library, which might differ from the syntax used in :mod:`re`.
220
221 .. data:: NOEXPR
222
223 Get a regular expression that can be used with the regex(3) function to
224 recognize a negative response to a yes/no question.
225
226 .. data:: CRNCYSTR
227
228 Get the currency symbol, preceded by "-" if the symbol should appear before
229 the value, "+" if the symbol should appear after the value, or "." if the
230 symbol should replace the radix character.
231
232 .. data:: ERA
233
234 Get a string that represents the era used in the current locale.
235
236 Most locales do not define this value. An example of a locale which does
237 define this value is the Japanese one. In Japan, the traditional
238 representation of dates includes the name of the era corresponding to the
239 then-emperor's reign.
240
241 Normally it should not be necessary to use this value directly. Specifying
242 the ``E`` modifier in their format strings causes the :func:`strftime`
243 function to use this information. The format of the returned string is not
244 specified, and therefore you should not assume knowledge of it on different
245 systems.
246
Alexandre Vassalotti711ed4a2009-07-17 10:42:05 +0000247 .. data:: ERA_D_T_FMT
248
249 Get a format string for :func:`strftime` to represent dates and times in a
250 locale-specific era-based way.
251
252 .. data:: ERA_D_FMT
253
254 Get a format string for :func:`strftime` to represent time in a
255 locale-specific era-based way.
256
257 .. data:: ALT_DIGITS
258
259 Get a representation of up to 100 values used to represent the values
260 0 to 99.
Georg Brandl116aa622007-08-15 14:28:22 +0000261
262
Alexandre Vassalotti711ed4a2009-07-17 10:42:05 +0000263.. function:: getdefaultlocale([envvars])
Georg Brandl116aa622007-08-15 14:28:22 +0000264
265 Tries to determine the default locale settings and returns them as a tuple of
266 the form ``(language code, encoding)``.
267
268 According to POSIX, a program which has not called ``setlocale(LC_ALL, '')``
269 runs using the portable ``'C'`` locale. Calling ``setlocale(LC_ALL, '')`` lets
270 it use the default locale as defined by the :envvar:`LANG` variable. Since we
271 do not want to interfere with the current locale setting we thus emulate the
272 behavior in the way described above.
273
274 To maintain compatibility with other platforms, not only the :envvar:`LANG`
275 variable is tested, but a list of variables given as envvars parameter. The
Georg Brandlcd7f32b2009-06-08 09:13:45 +0000276 first found to be defined will be used. *envvars* defaults to the search
277 path used in GNU gettext; it must always contain the variable name
278 ``'LANG'``. The GNU gettext search path contains ``'LC_ALL'``,
279 ``'LC_CTYPE'``, ``'LANG'`` and ``'LANGUAGE'``, in that order.
Georg Brandl116aa622007-08-15 14:28:22 +0000280
281 Except for the code ``'C'``, the language code corresponds to :rfc:`1766`.
282 *language code* and *encoding* may be ``None`` if their values cannot be
283 determined.
284
Georg Brandl116aa622007-08-15 14:28:22 +0000285
Georg Brandlcd7f32b2009-06-08 09:13:45 +0000286.. function:: getlocale(category=LC_CTYPE)
Georg Brandl116aa622007-08-15 14:28:22 +0000287
288 Returns the current setting for the given locale category as sequence containing
289 *language code*, *encoding*. *category* may be one of the :const:`LC_\*` values
290 except :const:`LC_ALL`. It defaults to :const:`LC_CTYPE`.
291
292 Except for the code ``'C'``, the language code corresponds to :rfc:`1766`.
293 *language code* and *encoding* may be ``None`` if their values cannot be
294 determined.
295
Georg Brandl116aa622007-08-15 14:28:22 +0000296
Georg Brandlcd7f32b2009-06-08 09:13:45 +0000297.. function:: getpreferredencoding(do_setlocale=True)
Georg Brandl116aa622007-08-15 14:28:22 +0000298
299 Return the encoding used for text data, according to user preferences. User
300 preferences are expressed differently on different systems, and might not be
301 available programmatically on some systems, so this function only returns a
302 guess.
303
304 On some systems, it is necessary to invoke :func:`setlocale` to obtain the user
305 preferences, so this function is not thread-safe. If invoking setlocale is not
306 necessary or desired, *do_setlocale* should be set to ``False``.
307
Georg Brandl116aa622007-08-15 14:28:22 +0000308
309.. function:: normalize(localename)
310
311 Returns a normalized locale code for the given locale name. The returned locale
312 code is formatted for use with :func:`setlocale`. If normalization fails, the
313 original name is returned unchanged.
314
315 If the given encoding is not known, the function defaults to the default
316 encoding for the locale code just like :func:`setlocale`.
317
Georg Brandl116aa622007-08-15 14:28:22 +0000318
Georg Brandlcd7f32b2009-06-08 09:13:45 +0000319.. function:: resetlocale(category=LC_ALL)
Georg Brandl116aa622007-08-15 14:28:22 +0000320
321 Sets the locale for *category* to the default setting.
322
323 The default setting is determined by calling :func:`getdefaultlocale`.
324 *category* defaults to :const:`LC_ALL`.
325
Georg Brandl116aa622007-08-15 14:28:22 +0000326
327.. function:: strcoll(string1, string2)
328
329 Compares two strings according to the current :const:`LC_COLLATE` setting. As
330 any other compare function, returns a negative, or a positive value, or ``0``,
331 depending on whether *string1* collates before or after *string2* or is equal to
332 it.
333
334
335.. function:: strxfrm(string)
336
Mark Dickinsonc48d8342009-02-01 14:18:10 +0000337 Transforms a string to one that can be used in locale-aware
338 comparisons. For example, ``strxfrm(s1) < strxfrm(s2)`` is
339 equivalent to ``strcoll(s1, s2) < 0``. This function can be used
340 when the same string is compared repeatedly, e.g. when collating a
341 sequence of strings.
Georg Brandl116aa622007-08-15 14:28:22 +0000342
343
Georg Brandlcd7f32b2009-06-08 09:13:45 +0000344.. function:: format(format, val, grouping=False, monetary=False)
Georg Brandl116aa622007-08-15 14:28:22 +0000345
346 Formats a number *val* according to the current :const:`LC_NUMERIC` setting.
347 The format follows the conventions of the ``%`` operator. For floating point
348 values, the decimal point is modified if appropriate. If *grouping* is true,
349 also takes the grouping into account.
350
351 If *monetary* is true, the conversion uses monetary thousands separator and
352 grouping strings.
353
354 Please note that this function will only work for exactly one %char specifier.
355 For whole format strings, use :func:`format_string`.
356
Georg Brandl116aa622007-08-15 14:28:22 +0000357
Georg Brandlcd7f32b2009-06-08 09:13:45 +0000358.. function:: format_string(format, val, grouping=False)
Georg Brandl116aa622007-08-15 14:28:22 +0000359
360 Processes formatting specifiers as in ``format % val``, but takes the current
361 locale settings into account.
362
Georg Brandl116aa622007-08-15 14:28:22 +0000363
Georg Brandlcd7f32b2009-06-08 09:13:45 +0000364.. function:: currency(val, symbol=True, grouping=False, international=False)
Georg Brandl116aa622007-08-15 14:28:22 +0000365
366 Formats a number *val* according to the current :const:`LC_MONETARY` settings.
367
368 The returned string includes the currency symbol if *symbol* is true, which is
369 the default. If *grouping* is true (which is not the default), grouping is done
370 with the value. If *international* is true (which is not the default), the
371 international currency symbol is used.
372
373 Note that this function will not work with the 'C' locale, so you have to set a
374 locale via :func:`setlocale` first.
375
Georg Brandl116aa622007-08-15 14:28:22 +0000376
377.. function:: str(float)
378
379 Formats a floating point number using the same format as the built-in function
380 ``str(float)``, but takes the decimal point into account.
381
382
383.. function:: atof(string)
384
385 Converts a string to a floating point number, following the :const:`LC_NUMERIC`
386 settings.
387
388
389.. function:: atoi(string)
390
391 Converts a string to an integer, following the :const:`LC_NUMERIC` conventions.
392
393
394.. data:: LC_CTYPE
395
396 .. index:: module: string
397
398 Locale category for the character type functions. Depending on the settings of
399 this category, the functions of module :mod:`string` dealing with case change
400 their behaviour.
401
402
403.. data:: LC_COLLATE
404
405 Locale category for sorting strings. The functions :func:`strcoll` and
406 :func:`strxfrm` of the :mod:`locale` module are affected.
407
408
409.. data:: LC_TIME
410
411 Locale category for the formatting of time. The function :func:`time.strftime`
412 follows these conventions.
413
414
415.. data:: LC_MONETARY
416
417 Locale category for formatting of monetary values. The available options are
418 available from the :func:`localeconv` function.
419
420
421.. data:: LC_MESSAGES
422
423 Locale category for message display. Python currently does not support
424 application specific locale-aware messages. Messages displayed by the operating
425 system, like those returned by :func:`os.strerror` might be affected by this
426 category.
427
428
429.. data:: LC_NUMERIC
430
Georg Brandl502d9a52009-07-26 15:02:41 +0000431 Locale category for formatting numbers. The functions :func:`.format`,
432 :func:`atoi`, :func:`atof` and :func:`.str` of the :mod:`locale` module are
Georg Brandl116aa622007-08-15 14:28:22 +0000433 affected by that category. All other numeric formatting operations are not
434 affected.
435
436
437.. data:: LC_ALL
438
439 Combination of all locale settings. If this flag is used when the locale is
440 changed, setting the locale for all categories is attempted. If that fails for
441 any category, no category is changed at all. When the locale is retrieved using
442 this flag, a string indicating the setting for all categories is returned. This
443 string can be later used to restore the settings.
444
445
446.. data:: CHAR_MAX
447
448 This is a symbolic constant used for different values returned by
449 :func:`localeconv`.
450
Georg Brandl116aa622007-08-15 14:28:22 +0000451
452Example::
453
454 >>> import locale
Benjamin Petersonf608c612008-11-16 18:33:53 +0000455 >>> loc = locale.getlocale() # get current locale
Alexandre Vassalotti711ed4a2009-07-17 10:42:05 +0000456 # use German locale; name might vary with platform
457 >>> locale.setlocale(locale.LC_ALL, 'de_DE')
Georg Brandl48310cd2009-01-03 21:18:54 +0000458 >>> locale.strcoll('f\xe4n', 'foo') # compare a string containing an umlaut
Georg Brandl116aa622007-08-15 14:28:22 +0000459 >>> locale.setlocale(locale.LC_ALL, '') # use user's preferred locale
460 >>> locale.setlocale(locale.LC_ALL, 'C') # use default (C) locale
461 >>> locale.setlocale(locale.LC_ALL, loc) # restore saved locale
462
463
464Background, details, hints, tips and caveats
465--------------------------------------------
466
467The C standard defines the locale as a program-wide property that may be
468relatively expensive to change. On top of that, some implementation are broken
469in such a way that frequent locale changes may cause core dumps. This makes the
470locale somewhat painful to use correctly.
471
472Initially, when a program is started, the locale is the ``C`` locale, no matter
473what the user's preferred locale is. The program must explicitly say that it
474wants the user's preferred locale settings by calling ``setlocale(LC_ALL, '')``.
475
476It is generally a bad idea to call :func:`setlocale` in some library routine,
477since as a side effect it affects the entire program. Saving and restoring it
478is almost as bad: it is expensive and affects other threads that happen to run
479before the settings have been restored.
480
481If, when coding a module for general use, you need a locale independent version
Guido van Rossum8d2ef872007-10-15 15:42:31 +0000482of an operation that is affected by the locale (such as
Georg Brandl116aa622007-08-15 14:28:22 +0000483certain formats used with :func:`time.strftime`), you will have to find a way to
484do it without using the standard library routine. Even better is convincing
485yourself that using locale settings is okay. Only as a last resort should you
486document that your module is not compatible with non-\ ``C`` locale settings.
487
Georg Brandl116aa622007-08-15 14:28:22 +0000488The only way to perform numeric operations according to the locale is to use the
489special functions defined by this module: :func:`atof`, :func:`atoi`,
Georg Brandl502d9a52009-07-26 15:02:41 +0000490:func:`.format`, :func:`.str`.
Georg Brandl116aa622007-08-15 14:28:22 +0000491
Guido van Rossum8d2ef872007-10-15 15:42:31 +0000492There is no way to perform case conversions and character classifications
493according to the locale. For (Unicode) text strings these are done according
494to the character value only, while for byte strings, the conversions and
495classifications are done according to the ASCII value of the byte, and bytes
496whose high bit is set (i.e., non-ASCII bytes) are never converted or considered
497part of a character class such as letter or whitespace.
498
Georg Brandl116aa622007-08-15 14:28:22 +0000499
500.. _embedding-locale:
501
502For extension writers and programs that embed Python
503----------------------------------------------------
504
505Extension modules should never call :func:`setlocale`, except to find out what
506the current locale is. But since the return value can only be used portably to
507restore it, that is not very useful (except perhaps to find out whether or not
508the locale is ``C``).
509
510When Python code uses the :mod:`locale` module to change the locale, this also
511affects the embedding application. If the embedding application doesn't want
512this to happen, it should remove the :mod:`_locale` extension module (which does
513all the work) from the table of built-in modules in the :file:`config.c` file,
514and make sure that the :mod:`_locale` module is not accessible as a shared
515library.
516
517
518.. _locale-gettext:
519
520Access to message catalogs
521--------------------------
522
523The locale module exposes the C library's gettext interface on systems that
524provide this interface. It consists of the functions :func:`gettext`,
525:func:`dgettext`, :func:`dcgettext`, :func:`textdomain`, :func:`bindtextdomain`,
526and :func:`bind_textdomain_codeset`. These are similar to the same functions in
527the :mod:`gettext` module, but use the C library's binary format for message
528catalogs, and the C library's search algorithms for locating message catalogs.
529
530Python applications should normally find no need to invoke these functions, and
531should use :mod:`gettext` instead. A known exception to this rule are
532applications that link use additional C libraries which internally invoke
Georg Brandl60203b42010-10-06 10:11:56 +0000533:c:func:`gettext` or :func:`dcgettext`. For these applications, it may be
Georg Brandl116aa622007-08-15 14:28:22 +0000534necessary to bind the text domain, so that the libraries can properly locate
535their message catalogs.
536