Chapter XV: internationalization and localization -gettext: Message catalog - Application and Localization Module

15.1.5 Application module localization
job gamut conversion gettext defines how to install, and how gettext for a body of code.

15.1.5.1 localization is applied
for conversion of the full range of applications, can make use of __builtins__ namespace globally installed similar ungettext () function, which is acceptable because the author can control the top-level application code, and understand the full It needs.

import gettext

gettext.install(
    'example',
    'locale',
    names=['ngettext'],
    )

print(_('This message is in the script.'))

install () function gettext () to bind to __builtins__ namespace name _ (). It also increases the ngettext () and other functions names listed.

15.1.5.2 localization module
for a library or a single module, modify __builtins__ not a good idea, as this may conflict with the values of an application globally. Indeed, it should be re-introduced into or bound conversion function name in front of the module manually.

import gettext

t = gettext.translation(
    'example',
    'locale',
    fallback=False,
    )
_ = t.gettext
ngettext = t.ngettext

print(_('This message is in the script.'))

The switching converter 15.1.6
preceding examples are used throughout the procedure of a conversion. In some cases, especially for a web application, we also need to use a different message catalog at different times, but do not exit and re-set environment. For these cases, class-based API would be more convenient gettext provided. These API calls is practically the same in this section describes the global call, but will release information catalog objects, but can be managed directly, so you can use multiple catalogs.

Guess you like

Origin blog.csdn.net/weixin_43193719/article/details/94762117