Old text backup: Python international support,

Source: Internet
Author: User
Tags i18n

Old text backup: Python international support,

Python supports internationalization (i18n) through the gettext module, and supports the multi-language interface of the program. below is my multi-language support implementation:

# Some descriptive title.

# Copyright (C) YEAR ORGANIZATION

# First author <EMAIL @ ADDRESS>, YEAR.

#

Msgid ""

Msgstr ""

"Project-Id-Version: package version \ n"

"POT-Creation-Date: + China Standard Time \ n"

"PO-Revision-Date: YEAR-MO-DA HO: MI + ZONE \ n"

"Last-Translator: full name <EMAIL @ ADDRESS> \ n"

"Language-Team: LANGUAGE <LL@li.org> \ n"

& Quot; MIME-Version: 1.0 \ n & quot"

"Content-Type: text/plain; charset = CHARSET \ n"

"Content-Transfer-Encoding: ENCODING \ n"

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

 

Charset = gb2312 Content-Transfer-Encoding: utf8:

# Some descriptive title.

# Copyright (C) YEAR ORGANIZATION

# First author <EMAIL @ ADDRESS>, YEAR.

#

Msgid ""

Msgstr ""

"Project-Id-Version: package version \ n"

"POT-Creation-Date: + China Standard Time \ n"

"PO-Revision-Date: YEAR-MO-DA HO: MI + ZONE \ n"

"Last-Translator: full name <EMAIL @ ADDRESS> \ n"

"Language-Team: LANGUAGE <LL@li.org> \ n"

& Quot; MIME-Version: 1.0 \ n & quot"

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

"Content-Transfer-Encoding: utf8 \ n"

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

The msgid "" And msgstr "" do not change

Good. The translation file has been created. Save it and change the file name to lang. po.

#-*-Coding: UTF-8 -*-

#! /Usr/bin/env python

Import gettext

Gettext. install ('lang ','./locale ', unicode = False)

Gettext. translation ('lang ','./locale ', ages = ['cn']). install (True)

In Row 4, lang is the main name of the translation file ,. /locale is the path for storing the translation file, and the third parameter is whether unicode is used. The languages parameter on the fifth line specifies the sub-directory in the language to be used. cn indicates that unicode is used. /locale/cn/LC_MESSAGES/translation file in the path.

Note: #-*-coding: UTF-8-*-it must be written in the first two rows, and the third row will not take effect.

Print "Hello world! "

Print "Python is a good Language ."

Changed:

Print _ ("Hello world! ")

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

# Some descriptive title.

# Copyright (C) YEAR ORGANIZATION

# First author <EMAIL @ ADDRESS>, YEAR.

#

Msgid ""

Msgstr ""

"Project-Id-Version: package version \ n"

"POT-Creation-Date: + China Standard Time \ n"

"PO-Revision-Date: YEAR-MO-DA HO: MI + ZONE \ n"

"Last-Translator: full name <EMAIL @ ADDRESS> \ n"

"Language-Team: LANGUAGE <LL@li.org> \ n"

& Quot; MIME-Version: 1.0 \ n & quot"

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

"Content-Transfer-Encoding: utf8 \ n"

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

 

Msgid "Hello world! "

Msgstr "Hello world! "

 

Msgid "Python is a good Language ."

Msgstr "Python is a good language ."

Save and run. msgfmt in the/Tools/i18n/directory. py, the command in Linux is: python msgfmt. py lang. po (in widows, set lang. copy the po file. in the/Tools/i18n/directory, run the command line window and enter msgfmt in the Change directory. py lang. po) to generate lang.mo and copy the file. in the/locale/cn/LC_MESSAGES/directory, set the lang. the po file directly generates the lang.mo file and copies it. in the/locale/en/LC_MESSAGES/directory, everything is ready. Run your main file in the command line to see if the two print commands output the world! Python is a good language. If you set the fifth line of the main file to en, the output is Hello world! Python is a good Language. Because the lang.mo file in the en directory has not been translated, the default string is not replaced.

The above implementation method can be used in Interface Programming to switch between Chinese and English interfaces. You need to select a configuration file in Chinese/English; you can also obtain the local language of the operating system and then set the Chinese or English language based on the obtained results at the beginning of the main file. In this way, the software language can be adaptive. If you want to achieve online switching of the software language, this is difficult for python. You need to add the interface refresh mechanism to the software. It is not recommended that you enable the software to take effect the next time (such as Ulipad) after switching the configuration in Chinese or English ).

(2007.1.24)

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.