gettext


Internationalization (I18N) and Localization (L10N)

Internationalization (I18N)

  • I18N is the process of making your software available to multiple languages.

  • It involves translating the text in your software into different languages.

Localization (L10N)

  • L10N is the process of adapting your software to a specific region or culture.

  • It involves translating the text in your software into a specific language and adapting it to the cultural conventions of that region.

GNU :program:gettext API

  • The GNU :program:gettext API is a set of functions that you can use to translate text in your Python software.

  • The API is similar to the API used by the GNU :program:gettext program, which is a popular tool for internationalizing software.

  • The GNU :program:gettext API is used to create and manage translation catalogs.

  • A translation catalog is a file that contains the translations of the text in your software.

  • The GNU :program:gettext API can be used to translate text in your Python software at runtime.

  • This means that you can change the language of your software at runtime without having to recompile the software.

Class-Based API

  • The class-based API is a higher-level API than the GNU :program:gettext API.

  • It is designed to make it easier to internationalize Python modules and applications.

  • The class-based API provides a set of classes that you can use to create and manage translation catalogs.

  • The class-based API can be used to translate text in your Python software at runtime.

  • This means that you can change the language of your software at runtime without having to recompile the software.

Real-World Complete Code Implementations and Examples

The following code snippet shows how to use the GNU :program:gettext API to translate text in a Python module:

import gettext

# Create a translation catalog.
catalog = gettext.translation('mymodule', 'locale', languages=['en'])

# Translate a string.
translated_string = catalog.gettext('Hello, world!')

# Print the translated string.
print(translated_string)

The following code snippet shows how to use the class-based API to translate text in a Python module:

from gettext import gettext

# Create a translation catalog.
catalog = gettext.Catalog('mymodule')

# Translate a string.
translated_string = catalog.gettext('Hello, world!')

# Print the translated string.
print(translated_string)

Potential Applications in Real World

  • The GNU :program:gettext API and the class-based API can be used to internationalize and localize any Python software.

  • This includes Python modules, applications, and websites.

  • I18N and L10N can help you reach a wider audience and make your software more accessible to people from all over the world.


gettext.bindtextdomain() Method in Python

Simplified Explanation

Imagine you have a website with text in different languages. To make the text appear in the correct language for each user, you use gettext.

bindtextdomain() connects a specific domain (like your website's name) to a locale directory (where the translations are stored).

Detailed Explanation

Domain

Think of the domain as a unique identifier for your app or project. For example, if your website is called "MySite," you would set the domain to "MySite."

Locale Directory

This is the folder where the translated text files are stored. These files have a .mo extension, and they contain the translations for different languages.

Binding

bindtextdomain() creates a link between the domain and the locale directory. When your app needs to display text in a specific language, bindtextdomain() tells it where to find the translation.

Searching for Translations

When your app looks for a translation, it checks the locale directory for a .mo file with the following name:

{localedir}/{language}/LC_MESSAGES/{domain}.mo

where:

  • {localedir} is the locale directory you specified

  • {language} is the user's language (determined from their browser or other settings)

  • {domain} is the domain you set in bindtextdomain()

Example

import gettext

# Set the domain to "MySite" and the locale directory to "/translations"
gettext.bindtextdomain("MySite", "/translations")

# Translate the string "Hello"
print(gettext.gettext("Hello"))

Output:

Bonjour

If the user's language is French, bindtextdomain() will look for the file /translations/fr/LC_MESSAGES/MySite.mo. If it finds it, it will use the translation in that file.

Real-World Applications

bindtextdomain() is essential for internationalizing your app or website. It allows you to provide users with text in their own language, which improves their user experience.

Code Snippet

import gettext

# Set the domain and locale directory
gettext.bindtextdomain("MySite", "/translations")

# Translate text for strings in code
gettext.textdomain("MySite")
print(gettext.gettext("Hello"))

Function: textdomain

Purpose: Manage the current global domain (language setting) for gettext translations.

Simplified Explanation:

Imagine you have a multi-language website. You want visitors to see the content in their preferred language. Gettext helps you do this by providing translations for different languages. The global domain is like the "language mode" of your website. It tells gettext which language to use for translations.

How to Use:

To change the global domain, you can call textdomain like this:

textdomain("my_new_domain")

This sets the global domain to "my_new_domain".

To get the current global domain, you can call textdomain without any arguments:

current_domain = textdomain()

This returns the current global domain as a string.

Real-World Example:

Let's say you have a website that displays content in English and Spanish. You can use gettext to translate the content into these languages. To do this, you would create translation files for each language and then set the global domain to the appropriate language when displaying the content.

Potential Applications:

  • Translating user interfaces for multi-language software

  • Providing localized error messages and help documentation

  • Creating websites that automatically display content in the visitor's preferred language


gettext is a Python module that helps translate text into different languages. It allows you to create programs that can be used in different countries or regions, each with their own language.

gettext works by storing translations in special files called "translation catalogs". These files contain the original text (the "source string") and its translation into different languages.

To use gettext, you first need to create a translation catalog for your program. This can be done using the xgettext command-line tool. xgettext will scan your program's source code for all the text that needs to be translated, and it will create a translation catalog file.

Once you have a translation catalog, you can use the gettext function to translate text in your program. The gettext function takes two arguments:

  • The source string: This is the original text that you want to translate.

  • The target language: This is the language that you want to translate the text into.

The gettext function will look up the translation for the source string in the translation catalog, and it will return the translated text. If the translation for the source string is not found in the catalog, the gettext function will return the source string itself.

Here is an example of how to use the gettext function:

import gettext

# Load the translation catalog for the current language
# The 'myapp' parameter is the name of your program
gettext.bindtextdomain("myapp", "/path/to/translations")

# Gettext is used as a function to translate our string
print(gettext("Hello world!"))

Output:

¡Hola mundo!

In this example, the gettext function is used to translate the string "Hello world!" into Spanish. The translation catalog for the Spanish language is located at /path/to/translations.

gettext is a powerful tool that can help you create programs that can be used by people all over the world. It is simple to use and it can save you a lot of time and effort.

Here are some potential applications in the real world for the gettext module:

  • Internationalization of software: gettext can be used to translate software into different languages, making it accessible to a wider audience.

  • Localization of websites: gettext can be used to translate websites into different languages, making them more accessible to visitors from different countries.

  • Translation of documentation: gettext can be used to translate documentation into different languages, making it more useful to users from different countries.

  • Creation of language dictionaries: gettext can be used to create dictionaries of words and phrases in different languages, which can be used by translators and language learners.


Simplified explanation of dgettext function from Python's gettext module:

What is dgettext?

dgettext is a function that helps you translate text in your Python program into different languages. It's like the gettext function, but it allows you to specify a specific "domain" for the translation.

What's a "domain"?

A domain is a collection of messages that are related to a particular part of your program. For example, you might have a domain for all the messages related to your user interface, and another domain for all the messages related to your database.

Why use domains?

Domains are useful because they allow you to translate different parts of your program independently. This can make it easier to manage your translations and ensure that they're consistent across your program.

How to use dgettext?

To use dgettext, you need to pass it two arguments:

  • The domain name (as a string)

  • The message you want to translate (also as a string)

For example, the following code translates the message "Hello world" into the Spanish language, using the "ui" domain:

import gettext

# Set the domain
gettext.bindtextdomain("ui", "/path/to/ui_es.mo")

# Gettext the message
print(gettext.dgettext("ui", "Hello world"))

Real-world examples:

dgettext is used in many real-world applications, including:

  • Translating user interfaces

  • Translating documentation

  • Translating error messages

Potential applications:

Here are some potential applications for dgettext in real-world scenarios:

  • Translating a website: You can use dgettext to translate the text on your website into different languages, making it accessible to a wider audience.

  • Translating a mobile app: You can use dgettext to translate the text in your mobile app into different languages, making it easier for users to understand and use.

  • Translating a documentation: You can use dgettext to translate your documentation into different languages, making it easier for users to learn about your products or services.


ngettext Function

English Explanation:

Imagine having a message to translate that changes based on how many items you're talking about:

"I have 1 book." "I have 2 or more books."

When you translate, you need to know if you're using the singular ("book") or plural ("books") form of the message.

The ngettext() function helps you with this. It takes three arguments:

  1. Singular message: The message for when there's only one item.

  2. Plural message: The message for when there are two or more items.

  3. Number of items: The number you want to translate the message for.

Depending on the number of items, ngettext() will automatically return the correct translation.

Code Example:

# Translation file (en.po):
msgid "You have 1 book."
msgstr "Tu tienes 1 libro."

msgid "You have %d books."
msgstr "Tu tienes %d libros."
# Python code:
from gettext import ngettext

number_of_books = 5
translation = ngettext("You have 1 book.", "You have %d books.", number_of_books)
print(translation)  # Output: "Tu tienes 5 libros."

Real-World Application:

Translating messages in an e-commerce website: Displaying the correct number of items in a shopping cart or product listing.

Benefits:

  • Ensures accurate translations in different languages.

  • Improves user experience by displaying the correct amount of items in different contexts.

Additional Notes:

  • The plural formula is specified in the translation file .po.

  • There can be multiple plural forms in some languages.

  • ngettext() uses the built-in _() function to retrieve translations.


What is dngettext?

dngettext is a function in Python's gettext module that allows you to retrieve translated messages from a specific domain. It's similar to ngettext, except that it searches for the message in a specific translation domain, which is a collection of related translations.

How does dngettext work?

dngettext takes four arguments:

  • domain: The name of the translation domain to search for the message in.

  • singular: The singular form of the message.

  • plural: The plural form of the message.

  • n: The number that determines which form of the message to return.

Example:

Let's say we have a translation domain called "messages" and we want to retrieve the translated message for the following string:

"You have {n} message."

If n is 1, we would return the singular form of the message:

dngettext("messages", "You have {n} message.", "You have {n} messages.", n)

If n is greater than 1, we would return the plural form of the message:

dngettext("messages", "You have {n} message.", "You have {n} messages.", n)

Real-world applications:

dngettext is used in applications where you want to display translated messages to users based on the translation domain. For example, a website might have different translation domains for different sections of the site, such as "user interface", "help", and "documentation". By using dngettext, the website can retrieve the translated message for the correct domain, ensuring that users see the message in the correct language.

Improved example:

Here's an improved example of how to use dngettext in a real-world application:

import gettext

# Set the translation domain
gettext.bindtextdomain("messages")

# Translate the message using the correct domain
translated_message = dngettext("messages", "You have {n} message.", "You have {n} messages.", n)

# Display the translated message
print(translated_message)

This code will retrieve the translated message from the "messages" translation domain and display it to the user.


pgettext Function in Python's gettext Module

Simplified Explanation:

The pgettext() function helps you translate messages in your program by providing a context for the translation. Here's how it works:

Topics:

1. Context:

  • A context is like a label that you give to a message.

  • It helps the translation tool know when to use a specific translation.

2. Message:

  • The message is the text that you want to translate.

Usage:

from gettext import pgettext

# Set the context to "Greeting"
context = "Greeting"

# Message to be translated
message = "Hello"

# Get the translated message
translated_message = pgettext(context, message)

Output:

The translated message will depend on the context and the available translations.

Real-World Example:

Suppose you have a program that displays different types of messages based on the user's location.

from gettext import pgettext

# Set the context to the user's location
context = "Location"

# Message to be translated
message = "Welcome to our website"

# Get the translated message based on the location context
translated_message = pgettext(context, message)

# Print the translated message
print(translated_message)

Potential Applications:

  • Translating user interfaces and messages in applications

  • Localization of websites and software for different languages and regions

  • Providing context-sensitive translations to improve user experience


dpgettext() Function

The dpgettext() function in Python's gettext module is used for translating context-dependent messages. It takes three arguments:

  • domain: The domain (or message catalog) in which to search for the translation.

  • context: The context of the message, which helps distinguish between different translations for the same message.

  • message: The message to be translated.

How it Works

When you call dpgettext() with a specific domain, context, and message, it will look up the appropriate translation in the message catalog. If a translation is found, it will be returned. Otherwise, the original message will be returned.

Example

Suppose you have a message catalog named "my_messages" with the following translation for the message "Hello, world!":

[my_messages]
Hello, world! = Hola, mundo!

You can use dpgettext() to translate this message in a specific context, such as a greeting for a Spanish-speaking user:

import gettext

gettext.bindtextdomain("my_messages", "/path/to/my_messages")
gettext.textdomain("my_messages")

print(_("Hello, world!"))  # Output: "Hola, mundo!"

In this example:

  • gettext.bindtextdomain() sets the domain and path to the message catalog.

  • gettext.textdomain() sets the current domain.

  • _() is a shortcut function that calls gettext.dpgettext() with the current domain and a context of None.

Real-World Applications

dpgettext() is useful for translating messages that are context-dependent, such as greetings or user interface elements. By providing a specific context argument, you can ensure that the correct translation is used.

For example, you could use dpgettext() to translate a greeting message based on the user's language and the current time of day:

import gettext

gettext.bindtextdomain("my_messages", "/path/to/my_messages")
gettext.textdomain("my_messages")

greeting = _(
    "Good morning!",  # Message for morning
    "Good afternoon!",  # Message for afternoon
    "Good evening!",  # Message for evening
).format(gettext.ngettext("morning", "afternoon", "evening"))

print(greeting)

In this example, the translation function will return the appropriate greeting based on the current time of day:

  • In the morning, it will return "Good morning!".

  • In the afternoon, it will return "Good afternoon!".

  • In the evening, it will return "Good evening!".


npgettext() Function in Python's gettext Module

Simplified Explanation:

The npgettext() function is used to translate text that depends on the number of objects being counted.

Detailed Explanation:

  • context: A string that provides additional context for the translation.

  • singular: The translation to use when the count is 1.

  • plural: The translation to use when the count is not 1.

  • n: The count of objects.

Code Snippet:

from gettext import npgettext

context = "Items"
singular = "one item"
plural = "{} items"
count = 5

translation = npgettext(context, singular, plural, count)  # -> "5 items"

Real-World Complete Code Implementation:

Suppose you have a shopping cart with multiple items. You want to display the number of items in the cart:

from gettext import npgettext

context = "Shopping Cart"
singular = "1 item"
plural = "{} items"
count = len(items_in_cart)

cart_message = npgettext(context, singular, plural, count)

Potential Applications in the Real World:

  • Translating the number of items in a shopping cart.

  • Translating the number of followers on a social media platform.

  • Translating the number of days left until an event.

  • Translating the number of messages in a message thread.


Simplified Explanation of dnpgettext Function

The dnpgettext function is a localization function that allows you to translate text based on both the message and its context. It's similar to the ngettext function, but takes an additional context parameter.

Detailed Explanation:

Parameters:

  • domain: The name of the message catalog (collection of translations) to use.

  • context: A unique identifier for the context of the message.

  • singular: The singular form of the message.

  • plural: The plural form of the message.

  • n: The number used to determine which form of the message to use (singular or plural).

Return Value:

  • A translated string based on the specified context and number.

Example:

import gettext

# Set the message catalog and domain
gettext.bindtextdomain('myapp', '/path/to/translations')
gettext.textdomain('myapp')

# Define the gettext function
_ = gettext.dngettext

# Translate a message with context
print(_('Message', context='greeting', n=1))  # "Hello"
print(_('Message', context='greeting', n=2))  # "Hellos"

Real-World Application:

The dnpgettext function is used when you want to translate text that has different meanings depending on the context. For example, in a GUI application, you might have different translations for the "OK" button in different menus.

Potential Applications:

  • Localizing software applications

  • Translating websites

  • Internationalizing documentation

Class-Based API

The gettext module also provides a class-based API for localization. This API gives you more flexibility and control over the translation process.

Class:

  • GNUTranslations: A class that implements the parsing of GNU .mo format translation files.

Methods:

  • gettext(message): Translates a single message.

  • ngettext(singular, plural, n): Translates a message based on the specified number.

Installation:

from gettext import GNUTranslations

# Create an instance of the GNUTranslations class
translations = GNUTranslations('myapp', 'path/to/translations')

# Install the translations as the gettext function
translations.install()

# Translate a message
print(gettext('Message'))

Benefits of Class-Based API:

  • Allows you to load multiple message catalogs.

  • Provides more control over the translation process.

  • Can be used to create custom localization systems.


Function: find

Purpose:

To find a .mo file for a specific language and domain. A .mo file contains translated text for a program or application.

Parameters:

  • domain: The name of the program or application.

  • localedir (optional): The directory where .mo files are stored. If not provided, the default system locale directory is used.

  • languages (optional): A list of language codes to search for. If not provided, environment variables are checked for language preferences.

  • all (optional): If True, returns a list of all found file names. If False (default), returns the first found file name.

How it Works:

  1. If localedir is not provided, the default system locale directory is used.

  2. If languages is not provided, environment variables are checked for language preferences.

  3. The languages in languages are normalized and expanded. For example, "en-US" could be expanded to "en_US" or "en-US.UTF-8".

  4. The function iterates through the languages, searching for a .mo file with the following format:

localedir/language/LC_MESSAGES/domain.mo
  1. The first such file name that exists is returned. If no file is found, None is returned.

  2. If all is True, a list of all found file names is returned.

Example:

from gettext import find

# Find the `.mo` file for the "messages" domain and "en-US" language.
file_name = find("messages", languages=["en-US"])

if file_name:
    print(f"Found `.mo` file: {file_name}")
else:
    print("No `.mo` file found.")

Potential Applications:

The find function is used in programs and applications to load translated text for different languages. This allows users to choose the language they want to see the program in. For example, a language translation tool could use find to locate the .mo file for the user's chosen language and then load the translated text into the tool.


gettext.translation()

The gettext.translation() function in Python is used to translate text and messages in your application based on the user's language preferences. This function returns a Translations object, which represents all the translated messages for a specific domain, such as a plugin or module.

Parameters

  • domain (str): The domain is a unique string that identifies a particular set of translations within your application. Domains are typically defined for different modules or plugins.

  • localedir (str, optional): This is the path to a directory on your computer where the .mo files for translation are stored. By default, the function will search for .mo files in a directory named 'locale', which is typically located under the current working directory or the installation directory of your application.

  • languages (str or list of str, optional): It defines which languages you want your application to support. The function will search for .mo files for these specific languages. It can take a single language code (e.g., 'en') or a list of language codes (e.g., ['en', 'de', 'fr']).

  • class_ (Translations subclass, optional): By default, the function will use the GNUTranslations class to create the Translations object. You can pass in a different Translations subclass if you want to customize how translations are handled.

  • fallback: This is a boolean parameter (False by default). When set to True, the function will fall back to using the default language (typically 'en') if the requested language is not available. If False (default), an error will be raised.

Return Value

The translation() function returns an object of type Translations. You can then use this object to get translated strings for the current user's language:

# Example: Translate a string
from gettext import translation

# Define the domain for translation
domain = "my_plugin"

# Load translations from the locale directory
translations = translation(domain=domain, localedir="path/to/locale", languages=["en", "de"])

# Get translated string for current locale
translated_str = translations.gettext("Hello")

Applications

  • Translating user interfaces: You can set your application's default language based on the user's system language and allow them to switch languages at any time.

  • Localizing documentation: Create documentation in different languages to make your app accessible to users around the world.

  • Internationalizing software: Gettext is used in many open-source projects to enable translations in multiple languages and make their software accessible worldwide.


install

The install function in Python's gettext module is used to enable text translation in your application.

What is text translation?

Text translation allows you to display different translations of your text based on the user's language. For example, if your application has English and French translations, a French-speaking user would see the French version of your text.

How to use install

To use the install function, you need to provide two arguments:

  • domain: A string that identifies the group of strings you want to translate. For example, you could have a domain called "messages" for all the user-facing text in your application.

  • localedir: A string that specifies the directory where the translation files are located. For example, you could have a directory called "locale" that contains translation files for different languages.

Optional arguments

In addition to the required arguments, you can also provide an optional names argument. This argument is a list of strings that specify the names of the functions that you want to install. For example, you could install functions called _ and gettext to allow users to translate strings using the _() and gettext() functions.

Code example

import gettext

# Install the _() function in the builtins namespace
gettext.install("messages", localedir="locale", names=["_"])

# Example: translate a string
print(_("Hello, world!"))

Real-world applications

Text translation is useful in any application that needs to support multiple languages. For example, you could use text translation in a website, a mobile app, or a software application. By supporting multiple languages, you can make your application more accessible to a wider audience.


gettext Module

The gettext module in Python helps you translate text from one language to another in your programs.

Function _

The _ function is a shortcut that lets you easily translate strings in your code. You use it like this:

print(_('This string will be translated.'))

This will print the translated version of the string in the user's language. For example, if the user's language is Spanish, it might print "Esta cadena será traducida".

To make the _ function easy to use, you can add it to Python's builtins namespace. This means you can use it in any of your Python modules.

import builtins
builtins._ = _

Keyword-Only Parameter names

When using the _ function, you can now use the names keyword-only parameter to specify the names of variables in the string. For example:

print(_('Welcome, {name}!', name='Alice'))

This would print something like "Welcome, Alice!"

NullTranslations Class

The NullTranslations class is a base class for all translation classes. It provides the basic methods that translation classes need.

Here are the methods of the NullTranslations class:

  • gettext(message): Returns the translated version of the message.

  • ugettext(message): Returns the Unicode-encoded version of the translated message.

  • lgettext(message): Returns the translated version of the message in the current language.

  • ngettext(singular, plural, n): Returns the singular or plural translated version of the message based on the value of n.

Real-World Examples

Here's a complete example of how to use the _ function and the NullTranslations class:

import gettext

# Create a `NullTranslations` object.
translations = gettext.NullTranslations()

# Install the `NullTranslations` object in the builtins namespace.
import builtins
builtins._ = translations.gettext

# Use the `_` function to translate strings.
print(_('Welcome to my program!'))

Potential Applications

You can use the gettext module in any Python program that needs to support multiple languages. Here are some potential applications:

  • Creating websites and web applications

  • Writing desktop applications

  • Developing games

  • Translating documentation and other text files


NullTranslations Class:

Imagine a mailman, but instead of delivering letters, it's a translator who doesn't actually translate anything! That's the NullTranslations class.

Key Features:

  • It can be created without any arguments or with an optional file object (but don't worry, it won't read this file).

  • It sets some internal variables to help other translation classes work properly, like the language and character set (_info and _charset).

  • It has a method called _parse that does nothing (just like our mailman who doesn't deliver letters), but it's here for other translation classes to override and do actual translations.

Real-World Application:

Imagine you have a program that can display text in different languages. You want to add a feature where users can choose to see the text in their preferred language. If the user selects a language that your program doesn't support, you can use the NullTranslations class to display the text in the default language without any translations.

Code Example:

# Create a NullTranslations object
translations = gettext.NullTranslations()

# Get the list of supported languages
languages = translations.languages()

# Check if the user's preferred language is supported
if translations.is_language_supported(user_preferred_language):
    # Display the text in the user's preferred language
    translations.set_language(user_preferred_language)
    text = translations.ugettext("Hello, world!")
else:
    # Display the text in the default language
    text = translations.ugettext("Hello, world!")

_parse method in gettext module

Explanation

The _parse method is a method that is called by the gettext module to parse the data from a message catalog file. A message catalog file is a file that contains translations for a set of messages. The _parse method takes a file object as an argument, and reads the data from the file, and initializes the message catalog with the data.

Simplified Explanation

Imagine that you have a program that prints out messages in English. You want to translate this program into Spanish, so you create a message catalog file that contains the Spanish translations of the English messages. The _parse method is the method that reads the data from the message catalog file, and makes the Spanish translations available to the program.

Code Snippet

import gettext

# Create a message catalog object
catalog = gettext.Catalog('my_catalog')

# Parse the data from the message catalog file
catalog._parse(open('my_catalog.mo', 'rb'))

# Get the Spanish translation of a message
translation = catalog.gettext('Hello, world!')

Real-World Complete Code Implementation and Example

The following is a complete code implementation and example of how to use the _parse method:

import gettext

# Create a message catalog object
catalog = gettext.Catalog('my_catalog')

# Parse the data from the message catalog file
catalog._parse(open('my_catalog.mo', 'rb'))

# Get the Spanish translation of a message
translation = catalog.gettext('Hello, world!')

# Print the Spanish translation
print(translation)

Potential Applications in Real World

The _parse method is used in a variety of real-world applications, including:

  • Translating user interfaces

  • Internationalizing software

  • Generating localized documentation


Adding Fallback for Translations

What is a Fallback?

Imagine you have a translation dictionary for English to Spanish. If you look for the Spanish translation of "cat," you'll find it. But what if you look for "dog"? If the dictionary doesn't have it, it's like hitting a wall.

A fallback is like a backup dictionary. If the main dictionary doesn't have a translation, it checks the fallback dictionary to see if it has it.

How to Add a Fallback

To add a fallback, you use the add_fallback() method. It takes a single parameter, which is the fallback object.

fallback_dict = {"dog": "perro"}
translator.add_fallback(fallback_dict)

Now, when you look for "dog," it will first check the main dictionary. If it doesn't find it there, it will look in the fallback dictionary and return "perro."

Real-World Example

Let's say you have a website that supports multiple languages. The main translation dictionary is for English to Spanish, but you want to add French as a supported language.

french_dict = {"cat": "chat", "dog": "chien"}
translator.add_fallback(french_dict)

Now, when a French user visits the website, the translator will first check the English to Spanish dictionary. If it doesn't find the translation there, it will look in the French dictionary.

Potential Applications

Fallbacks can be useful in various situations:

  • Multiple languages: Supporting multiple languages on a website or application.

  • Incomplete dictionaries: Translations are often added over time, so a fallback can help fill in the gaps until the main dictionary is complete.

  • Special cases: There might be specific cases where translations in the main dictionary are unreliable or outdated. A fallback can provide more accurate translations in such cases.


Simplified Explanation:

The gettext() method is used to translate a message into the current language. It returns the translated message or the original message if no translation is available.

Detailed Explanation:

The gettext() method is part of the gettext module, which provides support for internationalization (i18n) in Python. It allows you to create multilingual applications by providing translations for different languages.

How it works:

  1. You start by creating a .po (portable object) file, which contains the original messages and their translations.

  2. You then use gettext to load the .po file and set the current language.

  3. When you call gettext(message), it looks for the translation of message in the .po file.

  4. If a translation is found, it returns the translated message.

  5. If no translation is found, it returns the original message, so that the application can still function.

Potential Applications:

  • Website localization: Translating the content of a website for different languages.

  • Localization of software: Translating the user interface and error messages of a software application.

  • Internationalization of documents: Translating text in documents, such as instruction manuals or legal agreements.

Code Example:

# Load the default translations
import gettext
translations = gettext.translation('messages', 'locale', languages=['en', 'es'])

# Set the current language
translations.set_language('es')

# Get the translated message
translated_message = translations.gettext('Hello world!')

# Print the translated message
print(translated_message)  # Hola mundo!

ngettext() Method in Python's gettext Module

The ngettext() method in Python's gettext module is used for translating messages in a way that handles both singular and plural forms of the message. It takes three arguments:

  1. singular: The message to use when the number is 1.

  2. plural: The message to use when the number is greater than 1.

  3. n: The number that determines whether to use the singular or plural message.

If no fallback has been set, ngettext() returns the singular message if n is 1, and the plural message otherwise. Overridden in derived classes.

Example:

from gettext import ngettext

# Get the translation for "dog"
dog = ngettext("dog", "dogs", 2)

# Print the result
print(dog)  # Output: "dogs"

Real-World Applications:

  • Translating messages in user interfaces that display dynamic content.

  • Generating reports or summaries that require formatting numbers and their corresponding units (e.g., "1 dog" vs. "2 dogs").

  • Creating multilingual websites or applications where the language can change dynamically based on user preferences.


Simplified Explanation:

The pgettext() method in Python's gettext module allows you to translate a message based on a context. It's similar to the gettext() function but takes an additional context argument.

Details:

  • Context: A string that provides additional information about the message. It's used to distinguish between messages with the same text but different meanings in different contexts.

  • Message: The text you want translated.

How it Works:

If you have set a fallback translation, the pgettext() method will forward the request to the fallback. Otherwise, it will return the translated message from the current domain.

Real-World Example:

Suppose you have a message that can have different translations depending on the context:

context = "greeting"
message = "Hello"

You can use pgettext() to translate the message based on the context:

translation = gettext.pgettext(context, message)

If the translation for "Hello" in the "greeting" context exists, it will be used. Otherwise, the default translation for "Hello" will be returned.

Potential Applications:

  • Translating messages in user interfaces where different contexts can have different meanings (e.g., error messages vs. success messages).

  • Translating content in documentation that may have multiple interpretations.

  • Creating specialized dictionaries for specific domains or contexts to improve translation accuracy.

Code Implementation:

import gettext

# Load the translation from a file
gettext.install("messages", "path/to/directory")

# Get the translation for the message
translation = gettext.pgettext("greeting", "Hello")

# Use the translation
print(translation)

Simplified Explanation:

The npgettext() method is used for translating a message that depends on the number (n) of objects being referred to. It works in the following ways:

  1. If there's a "backup translator", called a fallback, then npgettext() passes the translation request to that fallback translator.

  2. If there's no fallback or the fallback can't translate the message, then npgettext() itself does the translation. This is done by selecting either the singular message (when n = 1) or the plural message.

Code Implementation:

The npgettext() method is part of the gettext module in Python and is typically used with Python's ngettext() function, which takes the singular and plural messages and the count n as input. Here's an example:

from gettext import npgettext

singular_text = "One cat is playing."
plural_text   = "{n} cats are playing."
message = npgettext("messages", singular_text, plural_text, count)

When count is 1:

  • message is "One cat is playing."

When count is > 1:

  • message is "{count} cats are playing"

Fallback Translators:

Fallback translators can be useful if you don't have translations for certain languages and want to fall back on English or another default language. You can set the fallback by using setlocale():

import locale  
locale.setlocale(locale.LC_ALL, "C.UTF-8")  

Applications:

npgettext() is useful in applications where translations depend on the quantity being described. For example:

  • Translating a message that says how many items are in the shopping bag.

    • "There is 1 item" vs "There are n" item"

    • In this context "1" is singular and the value n is used for the plural translation.

  • Translating a confirmation dialog that asks if you’ll delete one email or several emails at a time

Further Notes on Translations

  • The singular message is used when n equals one (1st person singular form).

  • The plural message can be set up for different scenarios

  • plural can specify plural rules for singular and plural translations (eg 1 cat vs 2 cats),


gettext module

The gettext module in Python provides functions for internationalizing and localizing your code and messages.

info() method

The info() method in the gettext module returns a dictionary that contains metadata found in the message catalog file.

Real-World Example

Consider the following Python script that uses the gettext module to internationalize a message:

import gettext

# Initialize gettext with the specified locale
gettext.setlocale(gettext.LC_ALL, 'fr')

# Get the message catalog
message_catalog = gettext.translation('messages', localedir='locales', languages=['fr'])

# Get the message
message = message_catalog.gettext('Hello world!')

# Print the message
print(message)  # Outputs: "Bonjour le monde !"

In this example, the info() method can be used to retrieve metadata from the message catalog file. For instance, the following code snippet retrieves the language of the message catalog:

language = message_catalog.info()['language']

The output of the above code snippet would be "fr", indicating that the message catalog is in French.

Potential Applications

The gettext module can be used in various real-world applications, such as:

  • Internationalizing websites and web applications

  • Localizing desktop and mobile applications

  • Translating documentation and other written materials


Method: charset()

Simplified Explanation:

Imagine your message catalog is like a bookshelf filled with books written in different languages. The charset() method tells you which language or encoding is used in your message catalog file.

Code Snippet:

import gettext

# Create a message catalog object
message_catalog = gettext.Catalog("messages")

# Get the encoding of the message catalog file
charset = message_catalog.charset()

print(charset)  # Output: "utf-8"

Real-World Application:

Suppose you have a message catalog file that contains translations in English and Spanish. The English messages are encoded in ASCII, while the Spanish messages are encoded in UTF-8. To correctly display the messages in your application, you need to know the encoding of the message catalog file. You can use the charset() method to determine the encoding and ensure that the messages are displayed properly.


What is the gettext module?

The gettext module in Python helps you translate strings in your program into different languages. It's like having a dictionary for your text, but instead of looking up words based on their meaning, you look them up based on the language you want to translate to.

Installing the gettext method

To use the gettext module, you need to install it. You can do this by calling the install() method. This will make the _ function available to you, which is used to translate strings.

from gettext import install

install()

Example of using the gettext module

Let's say you have a program that prints the message "Hello, world!" in English. To translate it into Spanish, you can use the _ function like this:

from gettext import install, translation

install()
t = translation('mymodule', 'es')
_ = t.gettext

print(_("Hello, world!"))

This will print "Hola, mundo!" because the _ function will look up the Spanish translation for "Hello, world!" in the es translation catalog.

The GNUTranslations class

The GNUTranslations class is a subclass of the NullTranslations class. It can read GNU gettext format .mo files in both big-endian and little-endian format.

The GNUTranslations class parses optional metadata out of the translation catalog. This metadata is in RFC 822-style key: value pairs, and should contain the Project-Id-Version key. If the key Content-Type is found, then the charset property is used to initialize the "protected" _charset instance variable, defaulting to None if not found. If the charset encoding is specified, then all message ids and message strings read from the catalog are converted to Unicode using this encoding, else ASCII is assumed.

Potential applications in real world

The gettext module is used in many real-world applications, including:

  • Translating user interfaces

  • Localizing documentation

  • Internationalizing websites

It's a powerful tool that can help you make your programs more accessible to users around the world.


GNUTranslations - Overridden Base Class Methods

Class: GNUTranslations

Purpose: Provides internationalization support using the GNU gettext library.

Overridden Methods:

Overridden Methods Explained:

  1. GNUTranslations.gettext(msgid)

    • Purpose: Translates a message string (msgid) using the GNUTranslations catalog.

    • Simplified Explanation: Gets a translated version of a message.

    • Example:

      from gettext import GNUTranslations
      
      translations = GNUTranslations(localedir='locale')
      translated_message = translations.gettext("Hello world!")
  2. GNUTranslations.dgettext(domain, msgid)

    • Purpose: Translates a message string (msgid) from a specific domain (domain).

    • Simplified Explanation: Gets a translated message from a specific part of the application (domain).

    • Example:

      from gettext import GNUTranslations
      
      translations = GNUTranslations(localedir='locale', domains=['messages'])
      translated_message = translations.dgettext("messages", "Hello world!")
  3. GNUTranslations.ngettext(msgid, msgid_plural, n)

    • Purpose: Translates a message string for singular or plural forms (msgid and msgid_plural) based on the count (n).

    • Simplified Explanation: Knows how to translate messages that change based on a number (e.g., 1 apple vs. 2 apples).

    • Example:

      from gettext import GNUTranslations
      
      translations = GNUTranslations(localedir='locale')
      translated_message = translations.ngettext(
          "You have 1 new message", "You have %(num)s new messages", 1
      )
  4. GNUTranslations.ldtext(domain)

    • Purpose: Loads the message translations from a specific domain (domain).

    • Simplified Explanation: Loads the translations for a specific part of the application.

    • Example:

      from gettext import GNUTranslations
      
      translations = GNUTranslations(localedir='locale')
      translations.ldtext("messages")

Applications in the Real World

GNUTranslations is widely used in applications for internationalization and localization. Here are some potential applications:

  • Translating user interfaces and messages for different languages and regions.

  • Enabling users to select their preferred language for a better user experience.

  • Localizing content to meet cultural and regional expectations.

  • Maintaining multiple language translations without code duplication.

  • Simplifying software development by separating localization from the core application code.


gettext is a function that looks up a message in a catalog and returns the corresponding message string. A catalog is a collection of message translations. A message is a string that needs to be translated. A message id is a unique identifier for a message.

How does gettext work?

  1. You call gettext(message_id) with a message id.

  2. gettext looks up the message id in the current catalog.

  3. If the message id is found, gettext returns the corresponding message string.

  4. If the message id is not found, and a fallback has been set, gettext forwards the look up to the fallback's gettext method.

  5. If no fallback has been set, or the fallback's gettext method also doesn't find the message id, gettext returns the message id.

Real-world example Let's say you have an application that is translated into multiple languages. You have a message catalog for each language. When you want to display a message in a particular language, you call gettext(message_id) with the message id and the current language. gettext will look up the message id in the current language's catalog and return the corresponding message string.

Potential applications

  • Translating applications into multiple languages

  • Localizing applications for different regions

  • Creating custom translations for specific purposes

Here is a complete code implementation in Python:

import gettext

# Set the language
gettext.setlocale(locale.LC_ALL, 'fr_FR')

# Load the catalog
catalog = gettext.translation('messages', localedir='./locale', languages=['fr'])

# Get the translator
translator = catalog.gettext

# Translate a message
message = translator('Hello world')

# Print the translated message
print(message)

This code will print "Bonjour le monde" to the console.


ngettext Method in Python's gettext Module

Simplified Explanation

The ngettext method helps you translate messages that change based on the number you want to say. It's like having two different versions of the same message, one for when the number is 1 and another for when it's not.

Usage

To use ngettext, you need three things:

  1. The message in singular form (used when the number is 1).

  2. The message in plural form (used when the number is not 1).

  3. The number you want to use.

For example, let's translate the message "There is 1 file in this directory" to Spanish, where the plural form is "There are N files in this directory."

from gettext import GNUTranslations

cat = GNUTranslations("messages.es")  # Load the Spanish translations
num_files = 1
message = cat.ngettext(
    "There is %(num)d file in this directory",
    "There are %(num)d files in this directory",
    num_files
) % {"num": num_files}

print(message)  # Output: "Hay 1 archivo en este directorio"

Real-World Example

This method is useful when you need to display messages that depend on quantities. For example:

  • In a shopping cart: "You have 1 item in your cart" vs "You have N items in your cart"

  • In a social media post: "1 person likes this post" vs "N people like this post"

  • In a news article: "The population is 1 million" vs "The population is N million"

Potential Applications

Here are some potential applications of the ngettext method:

  • Internationalizing user interfaces (UIs)

  • Translating error messages

  • Displaying statistics and reports

  • Generating financial reports and invoices


Simplified Explanation of pgettext Method in gettext Module

What is pgettext?

pgettext (plural gettext) is a function in Python's gettext module that helps you translate messages while considering their context.

What does context mean?

Context refers to the surrounding information that gives a message its meaning. For example, a word like "book" can have a different translation depending on whether it's talking about a children's book or a school textbook.

How to use pgettext?

To use pgettext, you provide two arguments:

  • context: A string that describes the context of the message.

  • message: The message you want to translate.

For example:

from gettext import pgettext

# Translate the message "This is a book."
translation = pgettext("context", "This is a book.")

What happens when you call pgettext?

pgettext first checks if there's a translation for the given context and message in the current language. If there is, it returns that translation.

If there's no translation found, pgettext does the following:

  • If a fallback language has been set, it passes the context and message to the fallback's pgettext method.

  • If there's no fallback, it returns the message (without translating it).

Real-World Example

Imagine you have a website with a button that says "Buy Now" for English-speaking users. For Spanish-speaking users, you want the button to say "Comprar Ahora."

You can use pgettext to handle this translation:

from gettext import pgettext

# Set the current language to Spanish
gettext.setlocale(locale.LC_ALL, "es")

# Translate the button's text
button_text = pgettext("button", "Buy Now")

# Display the button with the translated text
print(f"<button>{button_text}</button>")

In this example, pgettext checks for a translation of "Buy Now" in the "button" context for the Spanish language. If it finds a translation, it uses that for the button's text. If not, it returns "Buy Now" (without translating it).

Applications in Real World

pgettext is useful in any application that requires contextual translation, such as:

  • Websites and other user interfaces

  • Software and applications

  • Documentation and tutorials


Internationalizing Python Applications using gettext

What is Internationalization (I18N)?

It's making sure your program can be easily adapted to different languages.

How to Internationalize Your Code

  1. Mark strings for translation: Wrap translatable strings in _('...') calls, like:

    _('This is the sentence I want to translate')
  2. Extract translatable strings: Use tools like xgettext or pygettext to extract these strings from your code.

  3. Translate the strings: Human translators translate these strings into different languages.

  4. Compile translations: The translated strings are compiled into binary .mo files using msgfmt.

Using the gettext Module

For Single Modules:

  • Import gettext and create a translation object:

    import gettext
    t = gettext.translation('my_module', 'path/to/locale')
  • Use the gettext method to translate strings:

    _ = t.gettext
    print(_('This string will be translated'))

For Entire Applications:

  • Install the _ function globally into the built-in namespace:

    import gettext
    gettext.install('my_application')
  • Now, you can use _('...') to translate strings anywhere in your application.

Deferred Translations:

  • Sometimes, you need to mark strings for translation but translate them later.

  • Use a dummy definition of _ that simply returns the string, like:

    def _(message): return message
  • Mark strings with _('...'), then remove the dummy definition later.

Real-World Applications:

  • Making your website available in multiple languages.

  • Translating error messages or documentation in your application.

  • Allowing users to switch between languages in your program.

Example:

Let's say you want to translate a simple greeting program to French.

hello.py:

from gettext import translation

t = translation('hello', 'locale')
_ = t.gettext

print(_('Hello, world!'))

locale/fr/LC_MESSAGES/hello.po:

msgid "Hello, world!"
msgstr "Bonjour, le monde !"

Command to compile translations:

msgfmt --output-file=hello.mo locale/fr/LC_MESSAGES/hello.po

Running the translated program:

LANG=fr ./hello.py

Output:

Bonjour, le monde !