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

时间:2021-12-06 18:31:04

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

本文介绍如何通过Python标准库gettext帮助你的程序支持多语言。

代码例子

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

翻译工作

我们通过使用poedit这个非常好用的工具对其翻译成我们想要的目标文件,该工具还带有翻译词典,跨平台。地址:http://poedit.net/

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

翻译后的结果

翻译后生成

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

武装gettext

然后我们在代码加入

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 在国际化操作方面很简单,你是否有更好的方案?