[Fixed]-Python set specific locale for date as a string

1👍

I got this to work using Babel.

Here is my code:

from datetime import date, datetime, time
from babel.dates import format_date, format_datetime, format_time

GENERIC_DETAILS_LIVE_PREVIEW_LABELS = {
    'ar': {
        ....
        'nac_generic_descriptor': u"موظف السنة",  # Employee of the year
        'nac_generic_preview_date': format_date(datetime.now(), format='long', locale='ar'),  # todays date localised
        ....
    },
    'en': {
        ....
        'nac_generic_descriptor': u"Employee of the year",  # Employee of the year
        'nac_generic_preview_date': format_date(datetime.now(), format='long', locale='en_US'),  # todays date localised
        ....
    },
    'it': {
        ....
        'nac_generic_descriptor': u"Impiegato dell'anno",  # Employee of the year
        'nac_generic_preview_date': format_date(datetime.now(), format='long', locale='it_IT'),  # todays date localised
        ....
    },
    'de': {
        ....
        'nac_generic_descriptor': u"Mitarbeiter des Jahres",  # Employee of the year
        'nac_generic_preview_date': format_date(datetime.now(), format='long', locale='de_DE'),  # todays date localised
        ....
    },
    'fr': {
        ....
        'nac_generic_descriptor': u"Employé de l'année",  # Employee of the year
        'nac_generic_preview_date': format_date(datetime.now(), format='long', locale='fr_FR'),  # todays date localised
        ....
    },
    'es': {
        ....
        'nac_generic_descriptor': u"Empleado del año",  # Employee of the year
        'nac_generic_preview_date': format_date(datetime.now(), format='long', locale='es_ES'),  # todays date localised
        ....
    },
}

In my template the dates are now displayed as:

en: April 3, 2017

fr: 3 avril 2017

es: 3 de abril de 2017

etcetra, etcetra.

I hope this helps someone.

Leave a comment