2

I have created a function language which returns English or French. I would like to create a function that would give the date now based on a select language. For instance, if I select French, the function will return Jeudi, 28 mars 2017 or if I select English, it will return Tuesday March 28 2017.

@property
def date_customerprofile(self):
    now = datetime.date.today()
    if language == 'English'
        date = now.strftime("%A, %B %d %Y")
    else:
        date = ...

Could anyone be able to help me to finish this function to get such results?

Thanks!

P.S. Knowing that months in French are Janvier, Février, Mars, Avril, Mai, Juin, Juillet, Août, Septembre, Octobre, Novembre and Décembre. Days of week are Dimanche (Sunday), Lundi, Mardi, Mercredi, Jeudi, Vendredi and Samedi

  • 1
    The `datetime` module works in more than one locale. See [this answer](http://stackoverflow.com/a/4083447/355230) for an example showing weekday names in two languages (see the code at the end). – martineau Mar 28 '17 at 18:18

1 Answers1

1

First you need to have required locale installed on your system. For ubuntu to install french locale, run below command.

$ sudo locale-gen fr_FR.utf8

Below is the solution for the question

from datetime import datetime
import locale

def set_locale(locale_):
    locale.setlocale(category=locale.LC_ALL, locale=locale_)


def date_customerprofile(language):
    now_ = datetime.today()
    if language == 'English':
        set_locale('en_US.utf8')
        date_ = now_.strftime("%A %B %d %Y")
    else:
        set_locale('fr_FR.utf8')
        date_ = now_.strftime("%A %B %d %Y")
    return date_

P.S. Avoid using standard package/function names as variables

Technocrat
  • 99
  • 2
  • 11
  • I am working with a programmer's team. So I don't know if it is appropriate just to load `sudo locale-gen fr_FR.utf8`. I am working in a django project. What are you suggest? –  Mar 28 '17 at 19:12
  • Locale is OS dependent. Python is making use of it. To make use of it one needs to install required locale. – Technocrat Mar 29 '17 at 03:15