9159金沙游艺场-奥门金沙游艺场-[欢迎您]

9159金沙游艺场为您呈现最新的面貌!,目前注册送体验金,欢迎您将成为影视娱乐行业新的风向标,是亚洲的优质娱乐先锋。

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

日期:2020-01-04编辑作者:服务器

如何让你的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 protected]>, YEAR.

#

msgid ""

msgstr ""

"Project-Id-Version: PACKAGE VERSIONn"

"POT-Creation-Date: 2014-12-24 15:35+CSTn"

"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONEn"

"Last-Translator: FULL NAME <[email protected]>n"

"Language-Team: LANGUAGE <[email protected]>n"

"MIME-Version: 1.0n"

"Content-Type: text/plain; charset=UTF-8n"

"Content-Transfer-Encoding: 8bitn"

"Generated-By: pygettext.py 1.5n"

 

 

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

代码例子 import random guessesTaken = 0 print(_(Hello! Whats your name?)) myName = input() number = random.randint(1, 20) print(Well,...

本文由9159金沙游艺场-奥门金沙游艺场-[欢迎您]发布于服务器,转载请注明出处:如何让你的Python程序支持多语言

关键词:

VMware Workstation下的虚拟Linux通过NAT模式共享上网

VMware Workstation下的虚拟Linux通过NAT模式共享上网 在VMwareWorkstation虚拟机下面,Linux虚机要上网,一般是桥接模式,但我...

详细>>

el-get,emacselget

el-get,emacselget Table of Contents 依赖 安装 配置 3.1. 自定义包配置 命令 管理扩展 9159金沙游艺场 ,el-get是一个emacs下的扩...

详细>>

CSS完结的粗略折叠张开多级菜单功用

JS+CSS达成的简洁明了折叠打开多级菜单效率,jscss折叠菜单 正文实例陈说了JS+CSS达成的可折叠展开多级菜单作用。分...

详细>>

诺玛lize.css的应用(【9159金沙游艺场】重新设置

如何使用 normalize.css 首先,安装或从Github下载Normalize.css,接下来有两种主要途径去使用它。 策略一:将 normalize.css...

详细>>