Django country on request

I am creating a Django (1.6) website (with twitter bootstrap) that has some forms where the user has to fill out several dates. I have included l10n and i18n. Datetime fields are controlled by jQuery widgets. The widget accepts a parameter to determine the input format for the date and time. How can I get the current django datetime format in a template tag so that I can match it with the Javascript equivalent? I want to get the full language (e.g. nl_BE, en_US, ...) because I live in Belgium and we play in French, Dutch and German, but we all use the same date format. If I use only the language (with get_language from django.utils.translation), I see date formats from France and Germany.

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

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

Anyone have an idea to solve this?

+5
source share
1 answer

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

This is mainly due to creating a new application that will contain your new formats, and specifying this application using the FORMAT_MODULE_PATH parameter.

Your formats application should look something like this:

 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)')), ) 
+1
source

Source: https://habr.com/ru/post/1207340/


All Articles