0

I'm working on a site that displays dates, and is in several languages via i18n. I'm displaying dates with a filter (sometimes I want only the day, only the hour, etc.)

I've read the last paragraph of this: https://docs.djangoproject.com/en/1.2/topics/i18n/, and from what I understand I'm supposed to be able to specify the formats I want in the django.po locale and use them in the template filter ?

It doesn't seem to work though.

 {{ date_start|date:"{% trans "DATE_HOUR_ONLY" %}" }}

With in the django.po for english language:

 msgid "DATE_HOUR_ONLY"
 msgstr "%P"

This completely blows up though... And using single quotes instead of doubles ({{ date_start|date:"{% trans 'DATE_HOUR_ONLY' %}" }}) doesn't blow up but displays this :

 {% 31Fri, 17 May 2013 00:00:00 -0500a.m.500 'FriAMCDTMay_00-05001368766800R_-0500MayFalse2013' %}

I can't find any concrete example online on how to proceed. Without the filters, the date switches correctly depending on the language, because of how DATE_FORMAT is set in my settings.py

Thanks a lot !

2 Answers 2

1

Well, after a bit of wandering around, this worked:

  {{ date_start|date:_('DATE_WITH_HOUR') }}
1

Maybe you can try Django formats.py

https://docs.djangoproject.com/en/1.4/topics/i18n/formatting/

For example , i have a formats folder in my project, don't forget the "init.py" if you want add a different locale,only need add a folder like 'zh_TW' in the 'en' same level

mysite/
    formats/
        __init__.py
        en/
            __init__.py
            formats.py

the formats.py look like

SHORT_DATE_FORMAT = 'Y/m/d'
SHORT_DATETIME_FORMAT = 'Y/m/d G:i:s'

and in my template.html

{ now | date:"SHORT_DATE_FORMAT" }

that can change by your locale, hope the solution will help you :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.