久久国产成人av_抖音国产毛片_a片网站免费观看_A片无码播放手机在线观看,色五月在线观看,亚洲精品m在线观看,女人自慰的免费网址,悠悠在线观看精品视频,一级日本片免费的,亚洲精品久,国产精品成人久久久久久久

分享

python國(guó)際化(i18n)和中英文切換 - Python - 我的菜園子

 yfmine 2007-02-22
python國(guó)際化(i18n)和中英文切換

Python通過(guò)gettext模塊支持國(guó)際化(i18n),可以實(shí)現(xiàn)程序的多語(yǔ)言界面的支持,下面是我的多語(yǔ)言支持實(shí)現(xiàn):

1.         python安裝目錄下的./Tools/i18n/(windows下例 D:\Program Files\Python25\Tools\i18n)目錄中找到pygettext.py運(yùn)行之,生成翻譯文件模版messages.pot,內(nèi)容大概是這個(gè)樣子:

 

# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR ORGANIZATION
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
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@ADDRESS>\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"

改之,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: 2007-01-18 09:55+China Standard Time\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\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"

那個(gè)msgid” ” msgstr” “不要?jiǎng)?/span>

,翻譯文件已經(jīng)建立了,保存之,文件名改為lang.po.

2.         建立翻譯文件路徑,在主文件目錄下建立中文翻譯路徑 ./locale/cn/LC_MESSAGES/(windows下例D:\ python-prj\src\locale\cn\LC_MESSAGES,其中src目錄是源文件主目錄),英文翻譯路徑  ./locale/en/LC_MESSAGES/(windows下例D:\ python-prj\src\locale\en\LC_MESSAGES,其中src目錄是源文件主目錄)

3.         在主文件中引入gettext模塊:

 

# -*- coding: utf-8 -*-
#!/usr/bin/env python
import gettext
gettext.install(‘lang‘, ‘./locale‘, unicode=False)
gettext.translation(‘lang‘, ‘./locale‘, languages=[‘cn‘]).install(True)

第四行中lang是翻譯文件主名, ./locale是存放翻譯文件的路徑,第三個(gè)參數(shù)是是否使用unicode,第五行languages參數(shù)指定要使用的語(yǔ)言存放的子目錄,這里cn表示使用./locale/cn/LC_MESSAGES/路徑下的翻譯文件.

注意: # -*- coding: utf-8 -*- 一定要寫在前兩行,寫第三行都不會(huì)生效

4.         處理要翻譯的字符串,:

 

print “Hello world!”
print “Python is a good Language.

改之:

print _(“Hello world!”)
print _(“Python is a good Language.)

5.         編寫并生成翻譯文件,打開步驟1中編輯過(guò)的lang.po文件添加翻譯文字,形成如下內(nèi)容:

 

# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR ORGANIZATION
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
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@ADDRESS>\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 "世界你好!"

msgid " Python is a good Language."
msgstr "Python 是門好語(yǔ)言."

保存之,運(yùn)行./Tools/i18n/目錄下的msgfmt.py,Linux下命令為: python msgfmt.py lang.po(widows下的話,lang.po文件拷貝到./Tools/i18n/目錄下,運(yùn)行命令行窗口,到改目錄下鍵入:msgfmt.py lang.po),生成lang.mo,將該文件拷貝至./locale/cn/LC_MESSAGES/目錄下,將步驟1生成的lang.po文件直接生成lang.mo文件拷貝到./locale/en/LC_MESSAGES/目錄下,好了一切都準(zhǔn)備好了,命令行下運(yùn)行你的主文件,看看兩個(gè)print命令是不是輸出了世界你好! Python 是門好語(yǔ)言.,如果將主文件第五行languages參數(shù)置為en,則輸出是Hello world! Python is a good Language.,因?yàn)?/span>en目錄下lang.mo文件未做任何的翻譯,默認(rèn)字符串不被替換.

上述實(shí)現(xiàn)的方法用在界面編程上可以實(shí)現(xiàn)中英文界面的切換,需要作個(gè)配置文件選擇中/英文;或者在主文件開始時(shí)先獲取操作系統(tǒng)本地語(yǔ)言然后根據(jù)獲取結(jié)果再設(shè)置中/英文,這樣就可以實(shí)現(xiàn)軟件語(yǔ)言自適應(yīng);如果想實(shí)現(xiàn)軟件語(yǔ)言在線切換,對(duì)于python來(lái)說(shuō)就比較困難了,需要在軟件中加入界面刷新機(jī)制,不推薦,可以在切換中/英文設(shè)置后軟件下一次啟動(dòng)生效(軟件如Ulipad).

 

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn),。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式,、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙,。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多