怎么让您的Python程序接济多语言

作者: 健康新闻  发布:2019-08-22

guess what! pizza's hereguess what! pizza's here

什么令你的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!

次第是绝对漂亮貌了,可是CEO猛然要求你改成中文的,那么我们通常大概选取将字符串全体修改为相应中文,可是主任要让您本人针对差异客户区别语言就劳动了。那有甚办法啊?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,...

guess what! pizza's here

图片 1

图片 2

图片 3

图片 4

图片 5

图片 6

图片 7

图片 8

图片 9

本文由小鱼儿玄机2站开奖发布于健康新闻,转载请注明出处:怎么让您的Python程序接济多语言

关键词: 小鱼儿主

上一篇:送给孤单一个人的你,牛油果拌饭
下一篇:没有了