Introduction to locale module in python

Introduction to locale module in python


Python locale module is part of the standard library for internationalization and localization in Python. The locale module allows developers to deal with certain cultural issues in their application. For instance, it handles formatting numbers as currency, comparing strings for sorting, and working with dates.

Changing the locale can have application-wide ramifications, so the recommended practice is to avoid changing the value in a library and to let the application set it one time.

Based on the official documentation, the locale module is implemented on top of the _locale module. It uses an ANSI C locale implementation if available

The locale module defines the following exception:
exception locale.Error
Exception is raised whenever the locale passed to setlocale() is not recognized.

Here is an example on how the code starts:
import locale
# uses user's default settings
locale.setlocale(locale.LC_ALL, '')
# uses current setting
locale.setlocale(locale.LC_ALL, None)
The first parameter represents the locale category while the second parameter modifies the locale setting for the category. It will use the current setting for the category if the second parameter is not present or None. On the other hand, an empty string will use the user%u2019s default setting.