Locale

From XPUB & Lens-Based wiki

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.

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

Once a locale is available, you can set it in Python, for instance:

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

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:

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