gettext — 多语言国际化服务

源代码: Lib/gettext.py


The gettext module provides internationalization (I18N) and localization (L10N) services for your Python modules and applications. It supports both the GNU gettext message catalog API and a higher level, class-based API that may be more appropriate for Python files. The interface described below allows you to write your module and application messages in one natural language, and provide a catalog of translated messages for running under different natural languages.

Some hints on localizing your Python modules and applications are also given.

GNU gettext API

The gettext module defines the following API, which is very similar to the GNU gettext API. If you use this API you will affect the translation of your entire application globally. Often this is what you want if your application is monolingual, with the choice of language dependent on the locale of your user. If you are localizing a Python module, or if your application needs to switch languages on the fly, you probably want to use the class-based API instead.

gettext. bindtextdomain ( domain , localedir = None )

Bind the domain to the locale directory localedir . More concretely, gettext will look for binary .mo files for the given domain using the path (on Unix): localedir/language/LC_MESSAGES/domain.mo ,其中 语言 is searched for in the environment variables LANGUAGE , LC_ALL , LC_MESSAGES ,和 LANG 分别。

localedir 被省略或 None , then the current binding for domain 被返回。 [ 1 ]

gettext. textdomain ( domain = None )

Change or query the current global domain. If domain is None , then the current global domain is returned, otherwise the global domain is set to domain , which is returned.

gettext. gettext ( message )

Return the localized translation of message , based on the current global domain, language, and locale directory. This function is usually aliased as _() in the local namespace (see examples below).

gettext. dgettext ( domain , message )

gettext() , but look the message up in the specified domain .

gettext. ngettext ( singular , plural , n )

gettext() , but consider plural forms. If a translation is found, apply the plural formula to n , and return the resulting message (some languages have more than two plural forms). If no translation is found, return singular if n is 1; return plural 否则。

The Plural formula is taken from the catalog header. It is a C or Python expression that has a free variable n ; the expression evaluates to the index of the plural in the catalog. See GNU gettext 文档编制 for the precise syntax to be used in .po files and the formulas for a variety of languages.

gettext. dngettext ( domain , singular , plural , n )

ngettext() , but look the message up in the specified domain .

gettext. pgettext ( context , message )
gettext. dpgettext ( domain , context , message )
gettext. npgettext ( context , singular , plural , n )
gettext. dnpgettext ( domain , context , singular , plural , n )

Similar to the corresponding functions without the p in the prefix (that is, gettext() , dgettext() , ngettext() , dngettext() ), but the translation is restricted to the given message context .

Added in version 3.8.