如何让你的Python程序支持多语言

发布时间:2017-09-12 09:24:51
如何让你的Python程序支持多语言 代码例子

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))

这是我们一个简单的猜数字游戏,我们执行看看过程。

$ 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可以帮助我们。

改造

我们首先用_(),然改造我们的字符串。你可以把_()想象成类似如下函数

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 自带一个工具我们提取使用_()包裹的字符串

pygettext3 -d guess guess.py

它生成了如下文件:

$ 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 ""

,站群系统

企业建站2800元起,携手武汉肥猫科技,做一个有见地的颜值派!更多优惠请戳:上海网站建设 http://www.flpsz.com