Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | :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 | |
| 10 | The :mod:`locale` module opens access to the POSIX locale database and |
| 11 | functionality. The POSIX locale mechanism allows programmers to deal with |
| 12 | certain cultural issues in an application, without requiring the programmer to |
| 13 | know all the specifics of each country where the software is executed. |
| 14 | |
| 15 | .. index:: module: _locale |
| 16 | |
| 17 | The :mod:`locale` module is implemented on top of the :mod:`_locale` module, |
| 18 | which in turn uses an ANSI C locale implementation if available. |
| 19 | |
| 20 | The :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 Brandl | cd7f32b | 2009-06-08 09:13:45 +0000 | [diff] [blame] | 28 | .. function:: setlocale(category, locale=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 29 | |
| 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 | |
| 42 | :func:`setlocale` is not thread safe on most systems. Applications typically |
| 43 | 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 52 | |
| 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 | |
Georg Brandl | a85ee5c | 2009-08-13 12:13:42 +0000 | [diff] [blame] | 147 | 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 | |
| 218 | The expression is in the syntax suitable for the :cfunc:`regex` function |
| 219 | 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 | |
| 247 | .. data:: ERA_YEAR |
| 248 | |
| 249 | Get the year in the relevant era of the locale. |
| 250 | |
| 251 | .. data:: ERA_D_T_FMT |
| 252 | |
| 253 | Get a format string for :func:`strftime` to represent dates and times in a |
| 254 | locale-specific era-based way. |
| 255 | |
| 256 | .. data:: ERA_D_FMT |
| 257 | |
| 258 | Get a format string for :func:`strftime` to represent time in a |
| 259 | locale-specific era-based way. |
| 260 | |
| 261 | .. data:: ALT_DIGITS |
| 262 | |
| 263 | Get a representation of up to 100 values used to represent the values |
| 264 | 0 to 99. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 265 | |
| 266 | |
Georg Brandl | a85ee5c | 2009-08-13 12:13:42 +0000 | [diff] [blame] | 267 | .. function:: getdefaultlocale([envvars]) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 268 | |
| 269 | Tries to determine the default locale settings and returns them as a tuple of |
| 270 | the form ``(language code, encoding)``. |
| 271 | |
| 272 | According to POSIX, a program which has not called ``setlocale(LC_ALL, '')`` |
| 273 | runs using the portable ``'C'`` locale. Calling ``setlocale(LC_ALL, '')`` lets |
| 274 | it use the default locale as defined by the :envvar:`LANG` variable. Since we |
| 275 | do not want to interfere with the current locale setting we thus emulate the |
| 276 | behavior in the way described above. |
| 277 | |
| 278 | To maintain compatibility with other platforms, not only the :envvar:`LANG` |
| 279 | variable is tested, but a list of variables given as envvars parameter. The |
Georg Brandl | cd7f32b | 2009-06-08 09:13:45 +0000 | [diff] [blame] | 280 | first found to be defined will be used. *envvars* defaults to the search |
| 281 | path used in GNU gettext; it must always contain the variable name |
| 282 | ``'LANG'``. The GNU gettext search path contains ``'LC_ALL'``, |
| 283 | ``'LC_CTYPE'``, ``'LANG'`` and ``'LANGUAGE'``, in that order. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 284 | |
| 285 | Except for the code ``'C'``, the language code corresponds to :rfc:`1766`. |
| 286 | *language code* and *encoding* may be ``None`` if their values cannot be |
| 287 | determined. |
| 288 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 289 | |
Georg Brandl | cd7f32b | 2009-06-08 09:13:45 +0000 | [diff] [blame] | 290 | .. function:: getlocale(category=LC_CTYPE) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 291 | |
| 292 | Returns the current setting for the given locale category as sequence containing |
| 293 | *language code*, *encoding*. *category* may be one of the :const:`LC_\*` values |
| 294 | except :const:`LC_ALL`. It defaults to :const:`LC_CTYPE`. |
| 295 | |
| 296 | Except for the code ``'C'``, the language code corresponds to :rfc:`1766`. |
| 297 | *language code* and *encoding* may be ``None`` if their values cannot be |
| 298 | determined. |
| 299 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 300 | |
Georg Brandl | cd7f32b | 2009-06-08 09:13:45 +0000 | [diff] [blame] | 301 | .. function:: getpreferredencoding(do_setlocale=True) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 302 | |
| 303 | Return the encoding used for text data, according to user preferences. User |
| 304 | preferences are expressed differently on different systems, and might not be |
| 305 | available programmatically on some systems, so this function only returns a |
| 306 | guess. |
| 307 | |
| 308 | On some systems, it is necessary to invoke :func:`setlocale` to obtain the user |
| 309 | preferences, so this function is not thread-safe. If invoking setlocale is not |
| 310 | necessary or desired, *do_setlocale* should be set to ``False``. |
| 311 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 312 | |
| 313 | .. function:: normalize(localename) |
| 314 | |
| 315 | Returns a normalized locale code for the given locale name. The returned locale |
| 316 | code is formatted for use with :func:`setlocale`. If normalization fails, the |
| 317 | original name is returned unchanged. |
| 318 | |
| 319 | If the given encoding is not known, the function defaults to the default |
| 320 | encoding for the locale code just like :func:`setlocale`. |
| 321 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 322 | |
Georg Brandl | cd7f32b | 2009-06-08 09:13:45 +0000 | [diff] [blame] | 323 | .. function:: resetlocale(category=LC_ALL) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 324 | |
| 325 | Sets the locale for *category* to the default setting. |
| 326 | |
| 327 | The default setting is determined by calling :func:`getdefaultlocale`. |
| 328 | *category* defaults to :const:`LC_ALL`. |
| 329 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 330 | |
| 331 | .. function:: strcoll(string1, string2) |
| 332 | |
| 333 | Compares two strings according to the current :const:`LC_COLLATE` setting. As |
| 334 | any other compare function, returns a negative, or a positive value, or ``0``, |
| 335 | depending on whether *string1* collates before or after *string2* or is equal to |
| 336 | it. |
| 337 | |
| 338 | |
| 339 | .. function:: strxfrm(string) |
| 340 | |
Mark Dickinson | c48d834 | 2009-02-01 14:18:10 +0000 | [diff] [blame] | 341 | Transforms a string to one that can be used in locale-aware |
| 342 | comparisons. For example, ``strxfrm(s1) < strxfrm(s2)`` is |
| 343 | equivalent to ``strcoll(s1, s2) < 0``. This function can be used |
| 344 | when the same string is compared repeatedly, e.g. when collating a |
| 345 | sequence of strings. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 346 | |
| 347 | |
Georg Brandl | cd7f32b | 2009-06-08 09:13:45 +0000 | [diff] [blame] | 348 | .. function:: format(format, val, grouping=False, monetary=False) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 349 | |
| 350 | Formats a number *val* according to the current :const:`LC_NUMERIC` setting. |
| 351 | The format follows the conventions of the ``%`` operator. For floating point |
| 352 | values, the decimal point is modified if appropriate. If *grouping* is true, |
| 353 | also takes the grouping into account. |
| 354 | |
| 355 | If *monetary* is true, the conversion uses monetary thousands separator and |
| 356 | grouping strings. |
| 357 | |
| 358 | Please note that this function will only work for exactly one %char specifier. |
| 359 | For whole format strings, use :func:`format_string`. |
| 360 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 361 | |
Georg Brandl | cd7f32b | 2009-06-08 09:13:45 +0000 | [diff] [blame] | 362 | .. function:: format_string(format, val, grouping=False) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 363 | |
| 364 | Processes formatting specifiers as in ``format % val``, but takes the current |
| 365 | locale settings into account. |
| 366 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 367 | |
Georg Brandl | cd7f32b | 2009-06-08 09:13:45 +0000 | [diff] [blame] | 368 | .. function:: currency(val, symbol=True, grouping=False, international=False) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 369 | |
| 370 | Formats a number *val* according to the current :const:`LC_MONETARY` settings. |
| 371 | |
| 372 | The returned string includes the currency symbol if *symbol* is true, which is |
| 373 | the default. If *grouping* is true (which is not the default), grouping is done |
| 374 | with the value. If *international* is true (which is not the default), the |
| 375 | international currency symbol is used. |
| 376 | |
| 377 | Note that this function will not work with the 'C' locale, so you have to set a |
| 378 | locale via :func:`setlocale` first. |
| 379 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 380 | |
| 381 | .. function:: str(float) |
| 382 | |
| 383 | Formats a floating point number using the same format as the built-in function |
| 384 | ``str(float)``, but takes the decimal point into account. |
| 385 | |
| 386 | |
| 387 | .. function:: atof(string) |
| 388 | |
| 389 | Converts a string to a floating point number, following the :const:`LC_NUMERIC` |
| 390 | settings. |
| 391 | |
| 392 | |
| 393 | .. function:: atoi(string) |
| 394 | |
| 395 | Converts a string to an integer, following the :const:`LC_NUMERIC` conventions. |
| 396 | |
| 397 | |
| 398 | .. data:: LC_CTYPE |
| 399 | |
| 400 | .. index:: module: string |
| 401 | |
| 402 | Locale category for the character type functions. Depending on the settings of |
| 403 | this category, the functions of module :mod:`string` dealing with case change |
| 404 | their behaviour. |
| 405 | |
| 406 | |
| 407 | .. data:: LC_COLLATE |
| 408 | |
| 409 | Locale category for sorting strings. The functions :func:`strcoll` and |
| 410 | :func:`strxfrm` of the :mod:`locale` module are affected. |
| 411 | |
| 412 | |
| 413 | .. data:: LC_TIME |
| 414 | |
| 415 | Locale category for the formatting of time. The function :func:`time.strftime` |
| 416 | follows these conventions. |
| 417 | |
| 418 | |
| 419 | .. data:: LC_MONETARY |
| 420 | |
| 421 | Locale category for formatting of monetary values. The available options are |
| 422 | available from the :func:`localeconv` function. |
| 423 | |
| 424 | |
| 425 | .. data:: LC_MESSAGES |
| 426 | |
| 427 | Locale category for message display. Python currently does not support |
| 428 | application specific locale-aware messages. Messages displayed by the operating |
| 429 | system, like those returned by :func:`os.strerror` might be affected by this |
| 430 | category. |
| 431 | |
| 432 | |
| 433 | .. data:: LC_NUMERIC |
| 434 | |
Georg Brandl | c5605df | 2009-08-13 08:26:44 +0000 | [diff] [blame] | 435 | Locale category for formatting numbers. The functions :func:`.format`, |
| 436 | :func:`atoi`, :func:`atof` and :func:`.str` of the :mod:`locale` module are |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 437 | affected by that category. All other numeric formatting operations are not |
| 438 | affected. |
| 439 | |
| 440 | |
| 441 | .. data:: LC_ALL |
| 442 | |
| 443 | Combination of all locale settings. If this flag is used when the locale is |
| 444 | changed, setting the locale for all categories is attempted. If that fails for |
| 445 | any category, no category is changed at all. When the locale is retrieved using |
| 446 | this flag, a string indicating the setting for all categories is returned. This |
| 447 | string can be later used to restore the settings. |
| 448 | |
| 449 | |
| 450 | .. data:: CHAR_MAX |
| 451 | |
| 452 | This is a symbolic constant used for different values returned by |
| 453 | :func:`localeconv`. |
| 454 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 455 | |
| 456 | Example:: |
| 457 | |
| 458 | >>> import locale |
Benjamin Peterson | f608c61 | 2008-11-16 18:33:53 +0000 | [diff] [blame] | 459 | >>> loc = locale.getlocale() # get current locale |
Georg Brandl | a85ee5c | 2009-08-13 12:13:42 +0000 | [diff] [blame] | 460 | # use German locale; name might vary with platform |
| 461 | >>> locale.setlocale(locale.LC_ALL, 'de_DE') |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 462 | >>> locale.strcoll('f\xe4n', 'foo') # compare a string containing an umlaut |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 463 | >>> locale.setlocale(locale.LC_ALL, '') # use user's preferred locale |
| 464 | >>> locale.setlocale(locale.LC_ALL, 'C') # use default (C) locale |
| 465 | >>> locale.setlocale(locale.LC_ALL, loc) # restore saved locale |
| 466 | |
| 467 | |
| 468 | Background, details, hints, tips and caveats |
| 469 | -------------------------------------------- |
| 470 | |
| 471 | The C standard defines the locale as a program-wide property that may be |
| 472 | relatively expensive to change. On top of that, some implementation are broken |
| 473 | in such a way that frequent locale changes may cause core dumps. This makes the |
| 474 | locale somewhat painful to use correctly. |
| 475 | |
| 476 | Initially, when a program is started, the locale is the ``C`` locale, no matter |
| 477 | what the user's preferred locale is. The program must explicitly say that it |
| 478 | wants the user's preferred locale settings by calling ``setlocale(LC_ALL, '')``. |
| 479 | |
| 480 | It is generally a bad idea to call :func:`setlocale` in some library routine, |
| 481 | since as a side effect it affects the entire program. Saving and restoring it |
| 482 | is almost as bad: it is expensive and affects other threads that happen to run |
| 483 | before the settings have been restored. |
| 484 | |
| 485 | If, when coding a module for general use, you need a locale independent version |
Guido van Rossum | 8d2ef87 | 2007-10-15 15:42:31 +0000 | [diff] [blame] | 486 | of an operation that is affected by the locale (such as |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 487 | certain formats used with :func:`time.strftime`), you will have to find a way to |
| 488 | do it without using the standard library routine. Even better is convincing |
| 489 | yourself that using locale settings is okay. Only as a last resort should you |
| 490 | document that your module is not compatible with non-\ ``C`` locale settings. |
| 491 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 492 | The only way to perform numeric operations according to the locale is to use the |
| 493 | special functions defined by this module: :func:`atof`, :func:`atoi`, |
Georg Brandl | c5605df | 2009-08-13 08:26:44 +0000 | [diff] [blame] | 494 | :func:`.format`, :func:`.str`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 495 | |
Guido van Rossum | 8d2ef87 | 2007-10-15 15:42:31 +0000 | [diff] [blame] | 496 | There is no way to perform case conversions and character classifications |
| 497 | according to the locale. For (Unicode) text strings these are done according |
| 498 | to the character value only, while for byte strings, the conversions and |
| 499 | classifications are done according to the ASCII value of the byte, and bytes |
| 500 | whose high bit is set (i.e., non-ASCII bytes) are never converted or considered |
| 501 | part of a character class such as letter or whitespace. |
| 502 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 503 | |
| 504 | .. _embedding-locale: |
| 505 | |
| 506 | For extension writers and programs that embed Python |
| 507 | ---------------------------------------------------- |
| 508 | |
| 509 | Extension modules should never call :func:`setlocale`, except to find out what |
| 510 | the current locale is. But since the return value can only be used portably to |
| 511 | restore it, that is not very useful (except perhaps to find out whether or not |
| 512 | the locale is ``C``). |
| 513 | |
| 514 | When Python code uses the :mod:`locale` module to change the locale, this also |
| 515 | affects the embedding application. If the embedding application doesn't want |
| 516 | this to happen, it should remove the :mod:`_locale` extension module (which does |
| 517 | all the work) from the table of built-in modules in the :file:`config.c` file, |
| 518 | and make sure that the :mod:`_locale` module is not accessible as a shared |
| 519 | library. |
| 520 | |
| 521 | |
| 522 | .. _locale-gettext: |
| 523 | |
| 524 | Access to message catalogs |
| 525 | -------------------------- |
| 526 | |
| 527 | The locale module exposes the C library's gettext interface on systems that |
| 528 | provide this interface. It consists of the functions :func:`gettext`, |
| 529 | :func:`dgettext`, :func:`dcgettext`, :func:`textdomain`, :func:`bindtextdomain`, |
| 530 | and :func:`bind_textdomain_codeset`. These are similar to the same functions in |
| 531 | the :mod:`gettext` module, but use the C library's binary format for message |
| 532 | catalogs, and the C library's search algorithms for locating message catalogs. |
| 533 | |
| 534 | Python applications should normally find no need to invoke these functions, and |
| 535 | should use :mod:`gettext` instead. A known exception to this rule are |
| 536 | applications that link use additional C libraries which internally invoke |
| 537 | :cfunc:`gettext` or :func:`dcgettext`. For these applications, it may be |
| 538 | necessary to bind the text domain, so that the libraries can properly locate |
| 539 | their message catalogs. |
| 540 | |