Internationalization

Most programs present some information to users as text. Such text should be understandable and acceptable to the user. For example, in some countries and cultures, the date “March 7” can be concisely expressed as “3/7”. Elsewhere, “3/7” indicates “July 3”, and the string that means “March 7” is “7/3”. In Python, such cultural conventions are handled with the help of standard module locale.

Similarly, a greeting can be expressed in one natural language by the string “Benvenuti”, while in another language the string to use is “Welcome”. In Python, such translations are handled with the help of standard module gettext.

Both kinds of issues are commonly called internationalization (often abbreviated i18n, as there are 18 letters between i and n in the full spelling). This is actually a misnomer, as the issues also apply to programs used within one nation by users of different languages or cultures.

The locale Module

Python’s support for cultural conventions is patterned on that of C, slightly simplified. In this architecture, a program operates in an environment of cultural conventions known as a locale. The locale setting permeates the program and is typically set early on in the program’s operation. The locale is not thread-specific, and module locale is not thread-safe. In a multithreaded program, set the program’s locale before starting secondary threads.

If a program does not call locale.setlocale, the program operates in a neutral locale known as the C locale. ...

Get Python in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.