1

I did a little Python program :

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

It is quite simple, but I would like to translate it. First there is the IHM text (Enter your birth date) and (You was born a) then I also wish to translate the name of the day.

In the doc, I saw that it should be localized, but how should I configure the program to be localized ?

1 Answer 1

1

I think you're looking for the Locale Module.

simply add the following lines to your code:

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.

I set the 'desired local' to 'de_DE' and it produced the following using your code:

Enter your birth date (DD-MM-YYYY) : 15-08-2012
You was born a Mittwoch
Sign up to request clarification or add additional context in comments.

1 Comment

And if you want to specify the '<desired local>' you have to choose one from your system locales : locale -a

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.