# `gettext` --- 多语种国际化服务

源代码： Lib/gettext.py

`gettext` 模块为 Python 模块和应用程序提供国际化 (Internationalization, I18N) 和本地化 (Localization, L10N) 服务。它同时支持 GNU gettext 消息编目 API 和更高级的、基于类的 API，后者可能更适合于 Python 文件。下方描述的接口允许用户使用一种自然语言编写模块和应用程序消息，并提供翻译后的消息编目，以便在不同的自然语言下运行。

同时还给出一些本地化 Python 模块及应用程序的小技巧。

## GNU gettext API

模块 `gettext` 定义了下列 API，这与 gettext API 类似。如果你使用该 API，将会对整个应用程序产生全局的影响。如果你的应用程序支持多语种，而语言选择取决于用户的区域设置，这通常正是你所想要的。而如果你正在本地化某个 Python 模块，或者你的应用程序需要在运行时切换语言，相反你或许想用基于类的API。

`gettext.``bindtextdomain`(domain, localedir=None)

  将 domain 绑定到本地目录 localedir。 更具体地来说，模块 `gettext` 将使用路径 (在 Unix 系统中): `localedir/language/LC_MESSAGES/domain.mo` 查找二进制 `.mo` 文件，此处对应地查找 language 的位置是环境变量 `LANGUAGE`, `LC_ALL`, `LC_MESSAGES` 和 `LANG` 中。

  如果遗漏了 localedir 或者设置为 `None`，那么将返回当前 domain 所绑定的值 1

`gettext.``bind_textdomain_codeset`(domain, codeset=None)

  将 domain 绑定到 codeset，修改 `lgettext()`, `ldgettext()`, `lngettext()` 和 `ldngettext()` 函数返回的字节串的字符编码。如果省略了 codeset，则返回当前绑定的编码集。

  Deprecated since version 3.8, will be removed in version 3.10.

`gettext.``textdomain`(domain=None)

  修改或查询当前的全局域。如果 domain 为 `None`，则返回当前的全局域，不为 `None` 则将全局域设置为 domain，并返回它。

`gettext.``gettext`(message)

  返回 message 的本地化翻译，依据包括当前的全局域、语言和区域目录。本函数在本地命名空间中通常有别名 `_()` （参考下面的示例）。

`gettext.``dgettext`(domain, message)

  与 `gettext()` 类似，但在指定的 domain 中查找 message。

`gettext.``ngettext`(singular, plural, n)

  与 `gettext()` 类似，但考虑了复数形式。如果找到了翻译，则将 n 代入复数公式，然后返回得出的消息（某些语言具有两种以上的复数形式）。如果未找到翻译，则 n 为 1 时返回 singular，为其他数时返回 plural。

  复数公式取自编目头文件。它是 C 或 Python 表达式，有一个自变量 n，该表达式计算的是所需复数形式在编目中的索引号。关于在 `.po` 文件中使用的确切语法和各种语言的公式，请参阅 GNU gettext 文档 。

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

  与 `ngettext()` 类似，但在指定的 domain 中查找 message。

`gettext.``pgettext`(context, message)

`gettext.``dpgettext`(domain, context, message)

`gettext.``npgettext`(context, singular, plural, n)

`gettext.``dnpgettext`(domain, context, singular, plural, n)

  与前缀中没有 `p` 的相应函数类似（即 `gettext()`, `dgettext()`, `ngettext()`, `dngettext()` ），但是仅翻译给定的 message context。

  3.8 新版功能.

`gettext.``lgettext`(message)

`gettext.``ldgettext`(domain, message)

`gettext.``lngettext`(singular, plural, n)

`gettext.``ldngettext`(domain, singular, plural, n)

  与前缀中没有 `l` 的相应函数等效（ `gettext()`, `dgettext()`, `ngettext()` 和 `dngettext()` ），但是如果没有用 `bind_textdomain_codeset()` 显式设置其他编码，则返回的翻译将以首选系统编码来编码字节串。

  警告

  在 Python 3 中应避免使用这些函数，因为它们返回的是编码后的字节串。最好使用返回 Unicode 字符串的其他方法，因为大多数 Python 应用程序都希望将人类可读的文本作为字符串而不是字节来处理。此外，如果翻译后的字符串存在编码问题，则可能会意外出现与 Unicode 相关的异常。

  Deprecated since version 3.8, will be removed in version 3.10.

注意，GNU gettext 还定义了 `dcgettext()` 方法，但它被认为不实用，因此目前没有实现它。

这是该 API 的典型用法示例:

    import gettext
    gettext.bindtextdomain('myapplication', '/path/to/my/language/directory')
    gettext.textdomain('myapplication')
    _ = gettext.gettext
    # ...
    print(_('This is a translatable string.'))

## 基于类的 API

与 GNU gettext API 相比，`gettext` 模块的基于类的 API 提供了更多的灵活性和更强的便利性。这是本地化 Python 应用程序和模块的推荐方法。`gettext` 定义了一个 `GNUTranslations` 类，该类实现了 GNU `.mo` 格式文件的解析，并且具有用于返回字符串的方法。本类的实例也可以将自身作为函数 `_()` 安装到内建命名空间中。

`gettext.``find`(domain, localedir=None, languages=None, all=False)

  本函数实现了标准的 `.mo` 文件搜索算法。它接受一个 domain，它与 `textdomain()` 接受的域相同。可选参数 localedir 与 `bindtextdomain()` 中的相同。可选参数 languages 是多条字符串的列表，其中每条字符串都是一种语言代码。

  如果没有传入 localedir，则使用默认的系统区域设置目录。 2 如果没有传入 languages，则搜索以下环境变量：`LANGUAGE`、`LC_ALL`、`LC_MESSAGES` 和 `LANG`。从这些变量返回的第一个非空值将用作 languages 变量。环境变量应包含一个语言列表，由冒号分隔，该列表会被按冒号拆分，以产生所需的语言代码字符串列表。

  `find()` 将扩展并规范化 language，然后遍历它们，搜索由这些组件构建的现有文件:

  `localedir/language/LC_MESSAGES/domain.mo`

  `find()` 返回找到类似的第一个文件名。如果找不到这样的文件，则返回 `None`。如果传入了 all，它将返回一个列表，包含所有文件名，并按它们在语言列表或环境变量中出现的顺序排列。

`gettext.``translation`(domain, localedir=None, languages=None, class_=None, fallback=False, codeset=None)

  根据 domain、localedir 和 languages，返回 `*Translations` 实例，首先应将前述参数传入 `find()` 以获取关联的 `.mo` 文件路径的列表。名字与 `.mo` 文件名相同的实例将被缓存。如果传入 class_，它将是实际被实例化的类，否则实例化 `GNUTranslations`。类的构造函数必须只接受一个 文件对象 参数。如果传入 codeset，那么在 `lgettext()` 和 `lngettext()` 方法中，对翻译后的字符串进行编码的字符集将被改变。

  如果找到多个文件，后找到的文件将用作先前文件的替补。为了设置替补，将使用 `copy.copy()` 从缓存中克隆每个 translation 对象。实际的实例数据仍在缓存中共享。

  如果 `.mo` 文件未找到，且 fallback 为 false（默认值），则本函数引发 `OSError` 异常，如果 fallback 为 true，则返回一个 `NullTranslations` 实例。

  在 3.3 版更改: `IOError` 代替 `OSError` 被引发。

  Deprecated since version 3.8, will be removed in version 3.10: codeset 参数。

`gettext.``install`(domain, localedir=None, codeset=None, names=None)

  根据传入 `translation()` 函数的 domain、localedir 和 codeset，在 Python 内建命名空间中安装 `_()` 函数。

  names 参数的信息请参阅 translation 对象的 `install()` 方法的描述。

  如下所示，通常将字符串包括在 `_()` 函数的调用中，以标记应用程序中待翻译的字符串，就像这样:

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

  为了方便，一般将 `_()` 函数安装在 Python 内建命名空间中，以便在应用程序的所有模块中轻松访问它。

  Deprecated since version 3.8, will be removed in version 3.10: codeset 参数。

### `NullTranslations` 类

translation 类实际实现的是，将原始源文件消息字符串转换为已翻译的消息字符串。所有 translation 类使用的基类为 `NullTranslations`，它提供了基本的接口，可用于编写自己定制的 translation 类。以下是 `NullTranslations` 的方法：

class `gettext.``NullTranslations`(fp=None)

  接受一个可选参数 文件对象 fp，该参数会被基类忽略。初始化由派生类设置的 "protected" （受保护的）实例变量 _info 和 _charset，与 _fallback 类似，但它是通过 `add_fallback()` 来设置的。如果 fp 不为 `None`，就会调用 `self._parse(fp)`。

  `_parse`(fp)

    在基类中没有操作，本方法接受文件对象 fp，从该文件读取数据，用来初始化消息编目。如果你手头的消息编目文件的格式不受支持，则应重写本方法来解析你的格式。

  `add_fallback`(fallback)

    添加 fallback 为当前 translation 对象的替补对象。如果 translation 对象无法为指定消息提供翻译，则应向替补查询。

  `gettext`(message)

    如果设置了替补，则转发 `gettext()` 给替补。否则返回 message。在派生类中被重写。

  `ngettext`(singular, plural, n)

    如果设置了替补，则转发 `ngettext()` 给替补。否则，n 为 1 时返回 singular，为其他时返回 plural。在派生类中被重写。

  `pgettext`(context, message)

    如果设置了替补，则转发 `pgettext()` 给替补。否则返回已翻译的消息。在派生类中被重写。

    3.8 新版功能.

  `npgettext`(context, singular, plural, n)

    如果设置了替补，则转发 `npgettext()` 给替补。否则返回已翻译的消息。在派生类中被重写。

    3.8 新版功能.

  `lgettext`(message)
  `lngettext`(singular, plural, n)

    与 `gettext()` 和 `ngettext()` 等效，但是如果没有用 `set_output_charset()` 显式设置编码，则返回的翻译将以首选系统编码来编码字节串。在派生类中被重写。

    警告

    应避免在 Python 3 中使用这些方法。请参阅 `lgettext()` 函数的警告。

    Deprecated since version 3.8, will be removed in version 3.10.

  `info`()

    返回 "protected"（受保护的） `_info` 变量，它是一个字典，包含在消息编目文件中找到的元数据。

  `charset`()

    返回消息编目文件的编码。

  `output_charset`()

    返回由 `lgettext()` 和 `lngettext()` 翻译出的消息的编码。

    Deprecated since version 3.8, will be removed in version 3.10.

  `set_output_charset`(charset)

    更改翻译出的消息的编码。

    Deprecated since version 3.8, will be removed in version 3.10.

  `install`(names=None)

    This method installs `gettext()` into the built-in namespace, binding it to `_`.

    If the names parameter is given, it must be a sequence containing the names of functions you want to install in the builtins namespace in addition to `_()`. Supported names are `'gettext'`, `'ngettext'`, `'pgettext'`, `'npgettext'`, `'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.

    在 3.8 版更改: Added `'pgettext'` and `'npgettext'`.

### `GNUTranslations` 类

`gettext` 模块提供了一个派生自 `NullTranslations` 的其他类：`GNUTranslations`。该类重写了 `_parse()`，同时能以大端序和小端序格式读取 GNU gettext 格式的 `.mo` 文件。

`GNUTranslations` parses optional metadata out of the translation catalog. It is convention with GNU gettext to include metadata as the translation for the empty string. 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.

Since message ids are read as Unicode strings too, all `*gettext()` methods will assume message ids as Unicode strings, not byte strings.

The entire set of key/value pairs are placed into a dictionary and set as the "protected" `_info` instance variable.

If the `.mo` file's magic number is invalid, the major version number is unexpected, or if other problems occur while reading the file, instantiating a `GNUTranslations` class can raise `OSError`.

class `gettext.``GNUTranslations`

  The following methods are overridden from the base class implementation:

  `gettext`(message)

    Look up the message id in the catalog and return the corresponding message string, as a Unicode string. If there is no entry in the catalog for the message id, and a fallback has been set, the look up is forwarded to the fallback's `gettext()` method. Otherwise, the message id is returned.

  `ngettext`(singular, plural, n)

    Do a plural-forms lookup of a message id. singular is used as the message id for purposes of lookup in the catalog, while n is used to determine which plural form to use. The returned message string is a Unicode string.

    If the message id is not found in the catalog, and a fallback is specified, the request is forwarded to the fallback's `ngettext()` method. Otherwise, when n is 1 singular is returned, and plural is returned in all other cases.

    例如：

        n = len(os.listdir('.'))
        cat = GNUTranslations(somefile)
        message = cat.ngettext(
            'There is %(num)d file in this directory',
            'There are %(num)d files in this directory',
            n) % {'num': n}

  `pgettext`(context, message)

    Look up the context and message id in the catalog and return the corresponding message string, as a Unicode string. If there is no entry in the catalog for the message id and context, and a fallback has been set, the look up is forwarded to the fallback's `pgettext()` method. Otherwise, the message id is returned.

    3.8 新版功能.

  `npgettext`(context, singular, plural, n)

    Do a plural-forms lookup of a message id. singular is used as the message id for purposes of lookup in the catalog, while n is used to determine which plural form to use.

    If the message id for context is not found in the catalog, and a fallback is specified, the request is forwarded to the fallback's `npgettext()` method. Otherwise, when n is 1 singular is returned, and plural is returned in all other cases.

    3.8 新版功能.

  `lgettext`(message)
  `lngettext`(singular, plural, n)

    Equivalent to `gettext()` and `ngettext()`, but the translation is returned as a byte string encoded in the preferred system encoding if no encoding was explicitly set with `set_output_charset()`.

    警告

    应避免在 Python 3 中使用这些方法。请参阅 `lgettext()` 函数的警告。

    Deprecated since version 3.8, will be removed in version 3.10.

### Solaris message catalog support

The Solaris operating system defines its own binary `.mo` file format, but since no documentation can be found on this format, it is not supported at this time.

### The Catalog constructor

GNOME uses a version of the `gettext` module by James Henstridge, but this version has a slightly different API. Its documented usage was:

    import gettext
    cat = gettext.Catalog(domain, localedir)
    _ = cat.gettext
    print(_('hello world'))

For compatibility with this older module, the function `Catalog()` is an alias for the `translation()` function described above.

One difference between this module and Henstridge's: his catalog objects supported access through a mapping API, but this appears to be unused and so is not currently supported.

## Internationalizing your programs and modules

Internationalization (I18N) refers to the operation by which a program is made aware of multiple languages. Localization (L10N) refers to the adaptation of your program, once internationalized, to the local language and cultural habits. In order to provide multilingual messages for your Python programs, you need to take the following steps:

1. prepare your program or module by specially marking translatable strings

2. run a suite of tools over your marked files to generate raw messages catalogs

3. create language-specific translations of the message catalogs

4. use the `gettext` module so that message strings are properly translated

In order to prepare your code for I18N, you need to look at all the strings in your files. Any string that needs to be translated should be marked by wrapping it in `_('...')` --- that is, a call to the function `_()`. For example:

    filename = 'mylog.txt'
    message = _('writing a log message')
    with open(filename, 'w') as fp:
        fp.write(message)

In this example, the string `'writing a log message'` is marked as a candidate for translation, while the strings `'mylog.txt'` and `'w'` are not.

There are a few tools to extract the strings meant for translation. The original GNU gettext only supported C or C++ source code but its extended version xgettext scans code written in a number of languages, including Python, to find strings marked as translatable. Babel is a Python internationalization library that includes a `pybabel` script to extract and compile message catalogs. François Pinard's program called xpot does a similar job and is available as part of his po-utils package.

(Python also includes pure-Python versions of these programs, called pygettext.py and msgfmt.py; some Python distributions will install them for you. pygettext.py is similar to xgettext, but only understands Python source code and cannot handle other programming languages such as C or C++. pygettext.py supports a command-line interface similar to xgettext; for details on its use, run `pygettext.py --help`. msgfmt.py is binary compatible with GNU msgfmt. With these two programs, you may not need the GNU gettext package to internationalize your Python applications.)

xgettext, pygettext, and similar tools generate `.po` files that are message catalogs. They are structured human-readable files that contain every marked string in the source code, along with a placeholder for the translated versions of these strings.

Copies of these `.po` files are then handed over to the individual human translators who write translations for every supported natural language. They send back the completed language-specific versions as a `<language-name>.po` file that's compiled into a machine-readable `.mo` binary catalog file using the msgfmt program. The `.mo` files are used by the `gettext` module for the actual translation processing at run-time.

How you use the `gettext` module in your code depends on whether you are internationalizing a single module or your entire application. The next two sections will discuss each case.

### Localizing your module

If you are localizing your module, you must take care not to make global changes, e.g. to the built-in namespace. You should not use the GNU gettext API but instead the class-based API.

Let's say your module is called "spam" and the module's various natural language translation `.mo` files reside in `/usr/share/locale` in GNU gettext format. Here's what you would put at the top of your module:

    import gettext
    t = gettext.translation('spam', '/usr/share/locale')
    _ = t.gettext

### Localizing your application

If you are localizing your application, you can install the `_()` function globally into the built-in namespace, usually in the main driver file of your application. This will let all your application-specific files just use `_('...')` without having to explicitly install it in each file.

In the simple case then, you need only add the following bit of code to the main driver file of your application:

    import gettext
    gettext.install('myapplication')

If you need to set the locale directory, you can pass it into the `install()` function:

    import gettext
    gettext.install('myapplication', '/usr/share/locale')

### Changing languages on the fly

If your program needs to support many languages at the same time, you may want to create multiple translation instances and then switch between them explicitly, like so:

    import gettext

    lang1 = gettext.translation('myapplication', languages=['en'])
    lang2 = gettext.translation('myapplication', languages=['fr'])
    lang3 = gettext.translation('myapplication', languages=['de'])

    # start by using language1
    lang1.install()

    # ... time goes by, user selects language 2
    lang2.install()

    # ... more time goes by, user selects language 3
    lang3.install()

### Deferred translations

In most coding situations, strings are translated where they are coded. Occasionally however, you need to mark strings for translation, but defer actual translation until later. A classic example is:

    animals = ['mollusk',
               'albatross',
               'rat',
               'penguin',
               'python', ]
    # ...
    for a in animals:
        print(a)

Here, you want to mark the strings in the `animals` list as being translatable, but you don't actually want to translate them until they are printed.

Here is one way you can handle this situation:

    def _(message): return message

    animals = [_('mollusk'),
               _('albatross'),
               _('rat'),
               _('penguin'),
               _('python'), ]

    del _

    # ...
    for a in animals:
        print(_(a))

This works because the dummy definition of `_()` simply returns the string unchanged. And this dummy definition will temporarily override any definition of `_()` in the built-in namespace (until the `del` command). Take care, though if you have a previous definition of `_()` in the local namespace.

Note that the second use of `_()` will not identify "a" as being translatable to the gettext program, because the parameter is not a string literal.

Another way to handle this is with the following example:

    def N_(message): return message

    animals = [N_('mollusk'),
               N_('albatross'),
               N_('rat'),
               N_('penguin'),
               N_('python'), ]

    # ...
    for a in animals:
        print(_(a))

In this case, you are marking translatable strings with the function `N_()`, which won't conflict with any definition of `_()`. However, you will need to teach your message extraction program to look for translatable strings marked with `N_()`. xgettext, pygettext, `pybabel extract`, and xpot all support this through the use of the `-k` command-line switch. The choice of `N_()` here is totally arbitrary; it could have just as easily been `MarkThisStringForTranslation()`.

## 致谢

以下人员为创建此模块贡献了代码、反馈、设计建议、早期实现和宝贵的经验：

- Peter Funk

- James Henstridge

- Juan David Ibáñez Palomar

- Marc-André Lemburg

- Martin von Löwis

- François Pinard

- Barry Warsaw

- Gustavo Niemeyer

脚注

1

  The default locale directory is system dependent; for example, on RedHat Linux it is `/usr/share/locale`, but on Solaris it is `/usr/lib/locale`. The `gettext` module does not try to support these system dependent defaults; instead its default is `sys.base_prefix/share/locale` (see `sys.base_prefix`). For this reason, it is always best to call `bindtextdomain()` with an explicit absolute path at the start of your application.

2

  See the footnote for `bindtextdomain()` above.