Old text backup: Python internationalization support

Source: Internet
Author: User
Tags gettext i18n locale

Python supports internationalization through the GetText module (i18n), which supports the multilingual interface of the program, and the following is my multi-language support implementation:

    1. In the directory of./tools/i18n/(Windows example D:\Program files\python25\tools\i18n) in the Python installation directory, locate the pygettext.py run, Generate a translation file template Messages.pot, which is probably the way it looks:

# SOME Descriptive TITLE.

# Copyright (C) year ORGANIZATION

# first AUTHOR <[email Protected]>

#

MsgId ""

Msgstr ""

"Project-id-version:package version\n"

"Pot-creation-date:2007-01-24 15:05+china Standard time\n"

"Po-revision-date:year-mo-da ho:mi+zone\n"

"Last-translator:full NAME <[email protected]>\n"

"Language-team:language <[email protected]>\n"

"Mime-version:1.0\n"

"Content-type:text/plain; Charset=charset\n "

"Content-transfer-encoding:encoding\n"

"Generated-by:pygettext.py 1.5\n"

?

Change it, charset=gb2312 Content-transfer-encoding:utf8 as follows:

# SOME Descriptive TITLE.

# Copyright (C) year ORGANIZATION

# first AUTHOR <[email Protected]>

#

MsgId ""

Msgstr ""

"Project-id-version:package version\n"

"Pot-creation-date:2007-01-18 09:55+china Standard time\n"

"Po-revision-date:year-mo-da ho:mi+zone\n"

"Last-translator:full NAME <[email protected]>\n"

"Language-team:language <[email protected]>\n"

"Mime-version:1.0\n"

"Content-type:text/plain; charset=gb2312\n"

"Content-transfer-encoding: utf8\ n"

"Generated-by:pygettext.py 1.5\n"

That MsgId "" and Msgstr "don't move.

OK, the translation file has been established, save it, the file name is changed to Lang.po.

    1. Establish the path of the translation file and set up the Chinese translation path in the main file directory./locale/cn/lc_messages/(Windows example D:\ python-prj\src\locale\cn\lc_messages, Where the SRC directory is the source file home directory), the English translation path./locale/en/lc_messages/(Windows example D:\ python-prj\src\locale\en\lc_messages, where the SRC directory is the source file home directory )
    2. Introduce the GetText module in the main file:

#-*-Coding:utf-8-*-

#!/usr/bin/env python

Import GetText

Gettext.install (' Lang ', './locale ', Unicode=false)

Gettext.translation (' Lang ', './locale ', languages=[' CN ']). Install (True)

In line four, Lang is the main name of the translation file,./locale is the path to the translation file, the third parameter is whether to use Unicode, and the fifth line languages parameter specifies the subdirectory to use for the language, where CN is used . locale/cn/The translation file under the lc_messages/path.

Note: #-*-Coding:utf-8-*-must be written in the first two lines, the third line will not take effect

    1. To process a string to be translated, for example:

Print "Hello world!"

Print "Python is a good Language."

Change it:

Print _ ("Hello world!")

Print _ ("Python is a good Language.")

    1. Write and generate a translation file and open the Lang.po file you edited in step 1 to add the translated text to the following form:

# SOME Descriptive TITLE.

# Copyright (C) year ORGANIZATION

# first AUTHOR <[email Protected]>

#

MsgId ""

Msgstr ""

"Project-id-version:package version\n"

"Pot-creation-date:2007-01-18 09:55+china Standard time\n"

"Po-revision-date:year-mo-da ho:mi+zone\n"

"Last-translator:full NAME <[email protected]>\n"

"Language-team:language <[email protected]>\n"

"Mime-version:1.0\n"

"Content-type:text/plain; charset=gb2312\n"

"Content-transfer-encoding: utf8\ n"

"Generated-by:pygettext.py 1.5\n"

?

MsgId "Hello world!"

Msgstr "World Hello!"

?

MsgId "Python is a good Language."

Msgstr "Python is a good language."

Save, run. The Msgfmt.py,linux command under the/tools/i18n/directory is: Python msgfmt.py lang.po (widows will lang.po files are copied to . In the/tools/i18n/directory, run the command-line window, type: msgfmt.py lang.po in the directory below, generate LANG.MO, copy the file to the./locale/cn/lc_messages/directory, Copy the Lang.po file generated by step 1 directly to the./locale/en/lc_messages/directory, OK, everything is ready, run your main file under command line, and see if two print commands are output Hello world! Python is a good language. hello world! Python is a good Language.

The above-mentioned method can be used in interface programming to achieve the switch between Chinese and English interface, need to make a configuration file selection in/English, or at the beginning of the main file to obtain the operating system of the local language and then based on the obtained results are then set in/English, so that the software language adaptive; For Python is more difficult, need to add the interface refresh mechanism in the software, not recommended, can be switched in/English after the software next launch (software such as Ulipad).

(2007.1.24)

Old text backup: Python internationalization support

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.