Files
edx-platform/openedx/core/djangolib/translation_utils.py
Kyle McCormick 9aefd6f986 style: django-not-configured is not a sensible lint-amnesty value (#26862)
django-not-configured is an error raised by pylint (with
the pylint-django plugin) when it's not correctly configured.

We should not be applying lint amnesty for such a violation.
2021-03-05 08:11:58 -05:00

35 lines
1.1 KiB
Python

"""
i18n utility functions
"""
from django.utils.translation import ugettext as _, override # lint-amnesty, pylint: disable=unused-import
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