21.1.2.1 The NullTranslations class

Translation classes are what actually implement the translation of original source file message strings to translated message strings. The base class used by all translation classes is NullTranslations; this provides the basic interface you can use to write your own specialized translation classes. Here are the methods of NullTranslations:

__init__( [fp])
Takes an optional file object fp, which is ignored by the base class. Initializes ``protected'' instance variables _info and _charset which are set by derived classes, as well as _fallback, which is set through add_fallback. It then calls self._parse(fp) if fp is not None.

_parse( fp)
No-op'd in the base class, this method takes file object fp, and reads the data from the file, initializing its message catalog. If you have an unsupported message catalog file format, you should override this method to parse your format.

add_fallback( fallback)
Add fallback as the fallback object for the current translation object. A translation object should consult the fallback if it cannot provide a translation for a given message.

gettext( message)
If a fallback has been set, forward gettext() to the fallback. Otherwise, return the translated message. Overridden in derived classes.

lgettext( message)
If a fallback has been set, forward lgettext() to the fallback. Otherwise, return the translated message. Overridden in derived classes.

New in version 2.4.

ugettext( message)
If a fallback has been set, forward ugettext() to the fallback. Otherwise, return the translated message as a Unicode string. Overridden in derived classes.

ngettext( singular, plural, n)
If a fallback has been set, forward ngettext() to the fallback. Otherwise, return the translated message. Overridden in derived classes.

New in version 2.3.

lngettext( singular, plural, n)
If a fallback has been set, forward ngettext() to the fallback. Otherwise, return the translated message. Overridden in derived classes.

New in version 2.4.

ungettext( singular, plural, n)
If a fallback has been set, forward ungettext() to the fallback. Otherwise, return the translated message as a Unicode string. Overridden in derived classes.

New in version 2.3.

info( )
Return the ``protected'' _info variable.

charset( )
Return the ``protected'' _charset variable.

output_charset( )
Return the ``protected'' _output_charset variable, which defines the encoding used to return translated messages.

New in version 2.4.

set_output_charset( charset)
Change the ``protected'' _output_charset variable, which defines the encoding used to return translated messages.

New in version 2.4.

install( [unicode [, names]])
If the unicode flag is false, this method installs self.gettext() into the built-in namespace, binding it to "_". If unicode is true, it binds self.ugettext() instead. By default, unicode is false.

If the names parameter is given, it must be a sequence containing the names of functions you want to install in the builtin namespace in addition to _(). Supported names are 'gettext' (bound to self.gettext() or self.ugettext() according to the unicode flag), 'ngettext' (bound to self.ngettext() or self.ungettext() according to the unicode flag), 'lgettext' and 'lngettext'.

Note that this is only one way, albeit the most convenient way, to make the _ function available to your application. Because it affects the entire application globally, and specifically the built-in namespace, localized modules should never install _. Instead, they should use this code to make _ available to their module:

import gettext
t = gettext.translation('mymodule', ...)
_ = t.gettext

This puts _ only in the module's global namespace and so only affects calls within this module.

Changed in version 2.5: Added the names parameter.

See About this document... for information on suggesting changes.