Files
edx-platform/openedx/core/djangolib/translation_utils.py
Feanil Patel 9cf2f9f298 Run 2to3 -f future . -w
This will remove imports from __future__ that are no longer needed.

https://docs.python.org/3.5/library/2to3.html#2to3fixer-future
2019-12-30 10:35:30 -05:00

35 lines
1.1 KiB
Python

"""
i18n utility functions
"""
from django.utils.translation import ugettext as _, override
from django.utils.formats import dateformat, get_format
def translate_date(date, language, date_format='DATE_FORMAT'):
"""
Converts the provided date object into a string, while translating
its value for the given language. Both the format of the date
as well as its values (i.e., name of the Month) are translated.
If language is Spainish, then the entire date string is returned in
lowercase. This is used to work around a bug in the Spanish django
month translations.
See EDUCATOR-2328 for more details.
For example:
date = datetime.datetime(2017, 12, 23)
date_in_spanish = translate_date(date, 'es')
assert date_in_spanish == '23 de deciembre de 2017'
"""
with override(language):
formatted_date = dateformat.format(
date,
get_format(date_format, lang=language, use_l10n=True),
)
if language and language.startswith('es'):
formatted_date = formatted_date.lower()
return formatted_date