1

我做了一个小 Python 程序:

# -*- coding: utf-8 -*-
""" Program that ask for the birth date and return the day name of the birth """

from datetime import datetime

def ask_birthdate():
    """ Raw input of the birth date """
    date = raw_input("Enter your birth date (DD-MM-YYYY) : ").strip()
    return datetime.strptime(date, '%d-%m-%Y')

def birthday(date):
    """ Localized day of birth """
    return date.strftime('%A')

if __name__ == "__main__":
    date = ask_birthdate()
    print u"You was born a %s" % birthday(date)

很简单,但我想翻译一下。首先是 IHM 文本(输入您的出生日期)和(您的出生日期)然后我还希望翻译当天的名称。

在文档中,我看到它应该是本地化的,但是我应该如何将程序配置为本地化?

4

1 回答 1

1

我认为您正在寻找Locale Module

只需将以下行添加到您的代码中:

import locale

... 

locale.setlocale(locale.LC_ALL, '<desired local>')
# NOTE: using locale.setlocale(locale.LC_ALL, '') will use the machine's default locale defined in the LANG environment variable.

我将 'desired local' 设置为 'de_DE' 并使用您的代码生成以下内容:

Enter your birth date (DD-MM-YYYY) : 15-08-2012
You was born a Mittwoch
于 2012-08-15T15:19:31.760 回答