WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Python · Expert · question 72 of 100

How do you handle internationalization and localization in Python applications? Discuss the use of gettext and related libraries.?

📕 Buy this interview preparation book: 100 Python questions & answers — PDF + EPUB for $5

Internationalization (i18n) and localization (l10n) are important aspects of developing applications that can be used by users speaking different languages and living in different countries. In Python, the most commonly used library for handling i18n and l10n is the gettext library.

The gettext library provides a simple and standard way of handling i18n and l10n. The library consists of two parts: the gettext API and the GNU gettext utilities.

The gettext API provides the following functions:

gettext(message): Returns the translation of the given message in the current language. ngettext(singular, plural, n): Returns the translation of the given singular or plural message in the current language, based on the value of n. pgettext(context, message): Returns the translation of the given message in the current language and context. npgettext(context, singular, plural, n): Returns the translation of the given singular or plural message in the current language and context, based on the value of n.

The GNU gettext utilities are a set of command-line tools for extracting, merging, and compiling message catalogs. The message catalogs are files that contain translations of messages in different languages. The utilities can be used to extract the messages from the source code, create message catalogs, update existing catalogs with new messages, and compile the catalogs into binary format for faster loading.

To use the gettext library in a Python application, you need to perform the following steps:

Initialize the gettext library with the language you want to use:

    import gettext
    
    lang = 'fr' # French
    gettext.bindtextdomain('myapp', './locale')
    gettext.textdomain('myapp')
    gettext.setlocale(gettext.LC_ALL, lang)

In this example, we’re using the ’myapp’ domain for our translations, and the translations are stored in the ’./locale’ directory. We’re setting the language to ’fr’ for French.

Use the gettext functions to translate the messages in your application:

    message = gettext.gettext('Hello, world!')

This example translates the message ’Hello, world!’ to the current language.

Create message catalogs for different languages:

    xgettext -o locale/messages.pot myapp.py
    msginit -i locale/messages.pot -o locale/fr/LC_MESSAGES/myapp.po -l fr
    msgfmt -o locale/fr/LC_MESSAGES/myapp.mo locale/fr/LC_MESSAGES/myapp.po

These commands extract the messages from the source code into the messages.pot file, create a new message catalog for the French language in the locale/fr/LC_MESSAGES directory, and compile the catalog into binary format.

You can repeat these steps for other languages you want to support.

Distribute the message catalogs with your application.

You need to include the message catalogs for each language with your application, and load the appropriate catalog at runtime based on the user’s language preference.

    import gettext
    
    lang = 'fr' # French
    gettext.bindtextdomain('myapp', './locale')
    gettext.textdomain('myapp')
    gettext.bind_textdomain_codeset('myapp', 'UTF-8')
    gettext.setlocale(gettext.LC_ALL, lang)

The bind_textdomain_codeset function is used to set the encoding of the message catalog files.

In addition to the gettext library, there are several other libraries and frameworks available for handling i18n and l10n in Python, such as Babel and Flask-Babel. These libraries provide additional features and tools for managing translations, such as automatic detection of language preferences.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Python interview — then scores it.
📞 Practice Python — free 15 min
📕 Buy this interview preparation book: 100 Python questions & answers — PDF + EPUB for $5

All 100 Python questions · All topics