本文介紹如何經過Python標準庫gettext
幫助你的程序支持多語言。css
import random
guessesTaken = 0 print(_("Hello! What's your name?")) myName = input() number = random.randint(1, 20) print("Well, {}, I am thinking of a number between 1 and 20.".format(myName)) while guessesTaken < 6: print("Take a guess.") guess = input() try: guess = int(guess) except ValueError: print("You should give me a number.") continue if guess < number: print("Your guess is too low.") if guess > number: print("You guess is too high.") if guess == number: break if guess == number: print("Good job, {}! You guessed my number in {} guesses!".format( myName, guessesTaken)) if guess != number: print("Nope. The number I was thinking of was {}.".format(number))
這是咱們一個簡單的猜數字遊戲,咱們執行看看過程。python
$ python3 guess.py
Hello! What's your name? Aidan Well, Aidan, I am thinking of a number between 1 and 20. Take a guess. 13 You guess is too high. Take a guess. 2 Your guess is too low. Take a guess. 12 You guess is too high. Take a guess. 1 Your guess is too low. Take a guess. 10 You guess is too high. Take a guess. 9 Good job, Aidan! You guessed my number in 6 guesses!
程序是很漂亮了,但是老闆忽然要求你改爲中文的,那麼咱們一般可能選擇將字符串所有修改成相應中文,可是老闆要讓你本身針對不一樣用戶不一樣語言就麻煩了。那有啥辦法呢?Python標準庫gettext
能夠幫助咱們。nginx
咱們首先用_(),然改造咱們的字符串。你能夠把_()想象成相似以下函數dom
def _(s): spanishStrings = {'Hello world!': 'Hola Mundo!'} frenchStrings = {'Hello world!': 'Bonjour le monde!'} germanStrings = {'Hello world!': 'Hallo Welt!'} if LANGUAGE == 'English': return s if LANGUAGE == 'Spanish': return spanishStrings[s] if LANGUAGE == 'French': return frenchStrings[s] if LANGUAGE == 'German': return germanStrings[s]
不過,當我查看他類型時,我發現他應該是某種繼承了list的擴展類型。函數
>>> type(_) <class 'list'>
改造後咱們的代碼以下工具
import random
guessesTaken = 0 print(_("Hello! What's your name?")) myName = input() number = random.randint(1, 20) print(_("Well, {}, I am thinking of a number between 1 and 20.").format(myName)) while guessesTaken < 6: guessesTaken += 1 print(_("Take a guess.")) guess = input() try: guess = int(guess) except ValueError: print(_("You should give me a number.")) continue if guess < number: print(_("Your guess is too low.")) if guess > number: print(_("You guess is too high.")) if guess == number: break if guess == number: print(_("Good job, {}! You guessed my number in {} guesses!").format( myName, guessesTaken)) if guess != number: print(_("Nope. The number I was thinking of was {}.").format(number))
Python 自帶一個工具咱們提取使用_()包裹的字符串spa
pygettext3 -d guess guess.py
它生成了以下文件:.net
$ cat guess.pot
# SOME DESCRIPTIVE TITLE. # Copyright (C) YEAR ORGANIZATION # FIRST AUTHOR <EMAIL@ADDRESS>, YEAR. # msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "POT-Creation-Date: 2014-12-24 15:35+CST\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" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=UTF-8\n" "Content-Transfer-Encoding: 8bit\n" "Generated-By: pygettext.py 1.5\n" #: guess.py:5 msgid "Hello! What's your name?" msgstr "" #: guess.py:9 msgid "Well, {}, I am thinking of a number between 1 and 20." msgstr "" #: guess.py:12 msgid "Take a guess." msgstr "" #: guess.py:17 msgid "You should give me a number." msgstr "" #: guess.py:21 msgid "Your guess is too low." msgstr "" #: guess.py:24 msgid "You guess is too high." msgstr "" #: guess.py:30 msgid "Good job, {}! You guessed my number in {} guesses!" msgstr "" #: guess.py:34 msgid "Nope. The number I was thinking of was {}." msgstr ""
咱們經過使用poedit這個很是好用的工具對其翻譯成咱們想要的目標文件,該工具還帶有翻譯詞典,跨平臺。地址:http://poedit.net/翻譯
翻譯後生成code
guess.po
# SOME DESCRIPTIVE TITLE. # Copyright (C) YEAR ORGANIZATION # FIRST AUTHOR <EMAIL@ADDRESS>, YEAR. # msgid "" msgstr "" "Project-Id-Version: \n" "POT-Creation-Date: 2014-12-24 15:35+CST\n" "PO-Revision-Date: 2014-12-24 16:23+0800\n" "Last-Translator: \n" "Language-Team: \n" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=UTF-8\n" "Content-Transfer-Encoding: 8bit\n" "Generated-By: pygettext.py 1.5\n" "X-Generator: Poedit 1.7.1\n" "Plural-Forms: nplurals=1; plural=0;\n" "Language: zh\n" #: guess.py:5 msgid "Hello! What's your name?" msgstr "你好!你叫什麼名字?" #: guess.py:9 msgid "Well, {}, I am thinking of a number between 1 and 20." msgstr "好的,{},我內心想了個1到20之間的數。" #: guess.py:12 msgid "Take a guess." msgstr "猜猜看。" #: guess.py:17 msgid "You should give me a number." msgstr "你應該給我個數字。" #: guess.py:21 msgid "Your guess is too low." msgstr "你猜低了。" #: guess.py:24 msgid "You guess is too high." msgstr "你猜高了。" #: guess.py:30 msgid "Good job, {}! You guessed my number in {} guesses!" msgstr "漂亮,{}!你用了{}次猜中了個人數字。" #: guess.py:34 msgid "Nope. The number I was thinking of was {}." msgstr "不對。我想的數字是{}。"
還有一個mo擴展的文件,MO 文件是面向計算機的、由 PO 文件經過 gettext 軟件包編譯而成的二進制文件。
咱們以下放置這兩個文件
├── guess.py
├── locale
│ └── zh_CN
│ └── LC_MESSAGES
│ ├── guess.mo
│ └── guess.po
而後咱們在代碼加入
import gettext es = gettext.translation('guess', localedir='locale', languages=['zh_CN']) es.install()
import random import gettext es = gettext.translation('guess', localedir='locale', languages=['zh_CN']) es.install() guessesTaken = 0 print(_("Hello! What's your name?")) myName = input() number = random.randint(1, 20) print(_("Well, {}, I am thinking of a number between 1 and 20.").format(myName)) while guessesTaken < 6: guessesTaken += 1 print(_("Take a guess.")) guess = input() try: guess = int(guess) except ValueError: print(_("You should give me a number.")) continue if guess < number: print(_("Your guess is too low.")) if guess > number: print(_("You guess is too high.")) if guess == number: break if guess == number: print(_("Good job, {}! You guessed my number in {} guesses!").format( myName, guessesTaken)) if guess != number: print(_("Nope. The number I was thinking of was {}.").format(number))
$ python3 guess.py 你好!你叫什麼名字? 大熊 好的,大熊,我內心想了個1到20之間的數。 猜猜看。 10 你猜高了。 猜猜看。 8 你猜高了。 猜猜看。 6 你猜高了。 猜猜看。 4 漂亮,大熊!你用了4次猜中了個人數字。
Python 在國際化操做方面很簡單,你是否有更好的方案?