Chapter XV: internationalization and localization -gettext: Message catalog - the source code to create a message catalog

15.1.2 source code to create message catalogs
gettext first looks for a conversion literal string database, string and remove the appropriate conversion. Common mode is appropriate lookup function with the name "_" (single underscore character) binding, so that the code does not accumulate a lot of long name in the function call.
Xgettext message extraction program finds embedded lookup function (catalog lookup function) call message in the catalog. It knows different source language, and are using the appropriate parser. If the lookup function has an alias, or add additional functions, then we have to provide the name of these additional symbols for xgettext, so as to take into account when drawing the message.
The following script provides a message, you can complete the conversion.

import gettext

# Set up message catalog access.
t = gettext.translation(
    'example_domain','locale',
    fallback=True,
    )
_ = t.gettext

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

Text "This message is in the script. " Is to be replaced by a coded message. Fallback mode is enabled here, so if you do not have a message catalog when you run the script, it prints a message inline.
Here Insert Picture Description
The next step is to extract the message and create a .pot file ,, can be used here pygettext.py or xgettext.

The generated output file contains the following content.
Here Insert Picture Description

Message catalog is mounted to the directory by the domain (Domain) and language (language) tissue. Domain or libraries provided by the application, typically a unique value, such as the application name. Here, gettext_example.py the domain is example_domian. Linguistic environment provided by the user via an environment variable (LANGYAGE, LC_ALL, LC_MESSAGE or LANG) at run time, depending on its configuration and platform. Examples of this chapter at runtime language will be set to en_US.
Since the template is ready, the next step is to create the necessary directory structure and copy the template to the appropriate location.
Here Insert Picture Description
Use msgformat build the catalog from .po files.
Here Insert Picture Description
Here Insert Picture Description

gettext_example.py the domain is example_domain, but the file named example.pot. Gettext want to find the right to convert the file name must match.

t = gettext.translation(
    'example','locale',
    fallback=True,
    )

Now when you run the script, a message will be printed catalogs instead of inline string.

Guess you like

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