Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django country from request

Tags:

python

django

I'm building a Django (1.6) site (with twitter bootstrap) which has some forms where the user has to fill in some dates. I enabled l10n and i18n. The datetime fields are controlled by a JQuery widget. The widget accept a parameter to define the input format of the date and time. How can I get the current django datetime format in a template tag, so that I can map this to it's Javascript equivalent? What I want is to get the complete locale (like nl_BE, en_US,...) because I live in Belgium and we spreak French, Dutch and German, but we all use the same date format. If I use the language only (with get_language from django.utils.translation), I see the date formats from France and Germany.

>>> from django.utils import formats
>>> formats.get_format("SHORT_DATE_FORMAT", lang="nl")
Out[27]: u'j-n-Y'
>>> formats.get_format("SHORT_DATE_FORMAT", lang="fr")
Out[28]: u'j N Y'
>>> formats.get_format("SHORT_DATE_FORMAT", lang="de")
Out[29]: u'd.m.Y'

I checked already Django-datetime-widget on their demo page, but if I switch my browser (chrome) to dutch or french, it does not change the date format...

Anyone has an idea for to solve this?

like image 267
WimDH Avatar asked Nov 09 '22 22:11

WimDH


1 Answers

It sounds like you need to create some custom format files, because django does not provide locale formats for fr_BE, de_BE, and nl_BE. See https://docs.djangoproject.com/en/1.6/topics/i18n/formatting/#creating-custom-format-files on how to create custom locale formats.

It basically involves creating a new app that will contain your new formats, and specifying that app using the FORMAT_MODULE_PATH setting.

Your formats app should be something like:

formats/
    __init__.py
    fr_BE/
        __init__.py
        formats.py
    nl_BE/
        __init__.py
        formats.py
    de_BE/
        __init__.py
        formats.py

You should also add fr-be, de-be, and nl-be to your LANGUAGES in settings.py

LANGUAGES = (
    ('nl-be', ugettext_lazy('Dutch (Belgium)')),
    ('nl-fr', ugettext_lazy('French (Belgium)')),
    ('nl-de', ugettext_lazy('German (Belgium)')),
)
like image 194
Tareq Avatar answered Nov 14 '22 22:11

Tareq