Locale: Difference between revisions

From XPUB & Lens-Based wiki
No edit summary
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
Locales are ways for the operating system to group information about different language and regional settings. Python uses locales to make sure certain functions are "smart" and do what is expected of them, based on the conventions of a given region. For instance, when using the regular expressions, the meaning of "\w" (word character) is updated to include accented characters when the locale is set to a French language region. Sorting algorithms are also tweaked to correctly reflect how a language chooses to alphabetize.
Locales are ways for the operating system to group information about different language and regional settings.  


The following is an example of a command on an Ubuntu Linux machine to see the installed locales:
The following is an example of a command on an Ubuntu Linux machine to see the installed locales:
Line 11: Line 11:
</source>
</source>


Once a locale is available, you can set it in Python, for instance:
== Locales in Python ==
 
Python uses locales to make sure certain functions are "smart" and do what is expected of them, based on the conventions of a given region. Rules for alphabetizing, how to display numbers and monetary values, what constitutes a "yes" or a "no" typed value. When using the regular expressions, the meaning of "\w" (word character) is updated so that, for instance, accented characters are included when the locale is set to a French language region.
 
In general, all one needs to do is to call "setlocale" function to make sure that Python's locale is properly setup to match that of the current system:
 
<source lang="python">
import locale
locale.setlocale(locale.LC_ALL, '')
</source>
 
You can also explicitly set a locale, as long as it is available to the system. For instance, the following sets up support for Dutch language (in the Netherlands), using UTF-8 as the preferred character set.
 
<source lang="python">
<source lang="python">
>>> locale.setlocale(locale.LC_ALL, ("nl_NL", "UTF-8"))
>>> locale.setlocale(locale.LC_ALL, ("nl_NL", "UTF-8"))
Line 17: Line 29:
</source>
</source>


If things work, the command returns a string representing the selected locale. If the given locale is not installed on your system, or if the name is given incorrectly you may see the error:
If things work, the setlocale command will return a string representing the selected locale.
 
If the given locale is not installed on your system, or if the name is given incorrectly you may an error like:
<source lang="python">
<source lang="python">
Traceback (most recent call last):
Traceback (most recent call last):
Line 25: Line 39:
locale.Error: unsupported locale setting
locale.Error: unsupported locale setting
</source>
</source>
See: http://docs.python.org/library/locale.html

Latest revision as of 23:28, 5 April 2009

Locales are ways for the operating system to group information about different language and regional settings.

The following is an example of a command on an Ubuntu Linux machine to see the installed locales:

ls -l /usr/share/i18n/locales/

It may be necessary to install support for locales. On a Debian/Ubuntu system this can be done with:

apt-get install locales

Locales in Python

Python uses locales to make sure certain functions are "smart" and do what is expected of them, based on the conventions of a given region. Rules for alphabetizing, how to display numbers and monetary values, what constitutes a "yes" or a "no" typed value. When using the regular expressions, the meaning of "\w" (word character) is updated so that, for instance, accented characters are included when the locale is set to a French language region.

In general, all one needs to do is to call "setlocale" function to make sure that Python's locale is properly setup to match that of the current system:

import locale
locale.setlocale(locale.LC_ALL, '')

You can also explicitly set a locale, as long as it is available to the system. For instance, the following sets up support for Dutch language (in the Netherlands), using UTF-8 as the preferred character set.

>>> locale.setlocale(locale.LC_ALL, ("nl_NL", "UTF-8"))
'nl_NL.UTF8'

If things work, the setlocale command will return a string representing the selected locale.

If the given locale is not installed on your system, or if the name is given incorrectly you may an error like:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.5/locale.py", line 478, in setlocale
    return _setlocale(category, locale)
locale.Error: unsupported locale setting

See: http://docs.python.org/library/locale.html