django format date on user supplied locale - django

I'm trying to set up the formatting of dates in templates based on a locale supplied by the user. As the rest of the page will remain in the original local ('en'), I only want my users'
supplied data to be formatted.
for example, dates.
users in the uk should be able to use l10n on the dates on their pages, but I don't want to set the whole site to be en_GB.
is there a way to set the locale in a template within a block, eg. something like
{% locale|'en_GB' %}
{{ my_date|localize }}
{% endlocale %}

You don't need to do anything explicit in the template.
Inside your settings.py define the FORMAT_MODULE_PATH setting.
Like:
FORMAT_MODULE_PATH = 'myproject.myapp.formats'
under the formats directory create one python package per supported language(other
than your default) of your project. Inside each of these you should have a formats.py
which should have any localized formatting options.
In my case the default language for my project is en, but I also support el(greek).
So I have this in my settings.py:
FORMAT_MODULE_PATH = 'myproject.websiteapp.formats'
Inside the myproject/websiteapp/formats directory I have a el package with a formats.py file, like:
el/
__init__.py
formats.py
Inside the formats.py I have this:
DATETIME_FORMAT="l j M Y, g:i a"
which is the greek specific representation of a date.
So when I use a datetime field inside my templates:
{{ mymodel.pub_date }}
It prints the default en representation when locale is set to the default:
Published on: Feb. 22, 2013, 1:47 p.m.
and my custom greek one when the locale is set to el.
Δημοσιεύτηκε: Τετάρτη 6 Φεβ 2013, 5:39 μμ.
More info here
Edit
Hmm, I just realized that you asked for specific template blocks or values.
Maybe the localize template filter or the localize template tag
are more relevant to your specific case?

Related

Django datetime localization

I want to output a datetime object (with timezone info set to UTC) in a Django template. I have I18N, L10N, and TZ support all on in the settings. I've also added the LocaleMiddleware and set the language settings. Settings also contains my local timezone as the default. If I simply do {{ mydatetime }} in the template than it properly gives the information in the local timezone. However {{ mydatetime|localize }} seems to always output the datetime in UTC.
Now, I know you're supposed to call django.utils.timezone.activate() to report the current user's timezone (which I'm not doing), but the docs seem to indicate that if you don't use that to report the user's timezone that it uses the default.
Also, does the localize filter get used on datetimes automatically? Is the only purpose of having an explicit call is for when you use {% localize off %} and you want a one-off localization made?

Translate date format in django: technical message id?

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 !
Well, after a bit of wandering around, this worked:
{{ date_start|date:_('DATE_WITH_HOUR') }}
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 :)

Condition that checks placeholder content existence

How can I check placeholder content existence and make next trick? :
{% if placeholder 'Head_text' %} <--check here
<div class="in">
...
...
<h2 class="title">{% placeholder 'Head_text' %}</h2>
...
...
</div>
{% endif %}
I want to know, does placeholder have some content before rendering some special HTML-structure for it.
Thanks.
I was looking for a solution for this and I found a few alternatives instead of checking if the placeholder exists. Basically the idea is to use a different plugin instead that adds the extra html. There are a few packages you can install with pip. Now, after trying them I just did it myself and it was much more easier than using the packages.
cmsplugin-text-wrapper: It integrates to the existent django-cms default text plugin keeping the editor but adds a selector on top so you can select a wrapper. The wrapper contains the extra html you would like to add. It also has a nice CSS system to add classes. On the downside, I didn't want to make the editors life more difficult with the extra template selector.
cmsplugin-text-ng: This basically adds a new plugin. When you add the plugin to your placeholder, it display a selector with the available templates (that contains your extra HTML). What I really like is that you can add extra fields that you can use in your customized HTML. For example, you could add a title so the plugin displays an extra textfield for it. On the downside, the templates are store in the database through the admin!. That is an extra hit to the database and I really don't want to sacrifice it for something too simple.
Do your own plugin using the existent Text Model. Four very simple steps:
3.1 Basically add this to your cms_plugins.py:
from cms.plugin_pool import plugin_pool
from cms.plugins.text.models import Text
from cms.plugins.text.cms_plugins import TextPlugin
class WidgetPlugin(TextPlugin):
model = Text
name = _("Widget")
render_template = "widget.html"
def render(self, context, instance, placeholder):
context['instance'] = instance
return context
plugin_pool.register_plugin(WidgetPlugin)
3.2 Create your widget.html template in your templates folder:
<div class="in">
...
...
{{ instance.body|safe }}
...
...
</div>
3.3 Place your placeholder wherever you want:
{% placeholder 'Head_text' %}
3.4 Make the user use the new plugin adding the Head_text configuration plugin in the settings.py:
CMS_PLACEHOLDER_CONF = {
#...
'Head_text': {
'plugins': {'WidgetPlugin'}
},
}
I've had this problem before, and when I researched it (this might have changed since) there is no built in way to do this, so you have to write your own template tag to load the placeholder into a variable.
Here are some discussions on the django-cms mailing list:
https://groups.google.com/forum/?fromgroups=#!topic/django-cms/QeTlmxQnn3E
https://groups.google.com/forum/#!topic/django-cms/2mWvEpTH0ns/discussion

Format floats in Django template in language aware manner

I am using Django's humanize to make large float numbers in my app more readable. Also my app is available in different languages, and different languages use , and . signs in numbers the other way around.
For integer values the humanize tag intcomma works well:
{{ intvalue|intcomma }}
Would give for different locales:
English: 1,000,000
Dutch: 1.000.000
However, using floats this doesn't work very well. For example in my template you would find this:
{{ floatvalue|floatformat:2|intcomma }}
Would give for different locales:
English: 1,000,000.00
Dutch: 1,000,000,00
Note that instead of 1.000.000,00 for Dutch, it shows 1,000,000,00. Switching around the floatformat:2 and intcomma tags doesn't work either as then the value is nothing.
Any ideas on how to easily fix this?
(If possible, I'd rather not use an external libraries such as Babel)
Try the Django Format Localization feature. It gives you three options. As the Django docs say,
Django’s formatting system is capable to display dates, times and numbers in templates using the format specified for the current locale. It also handles localized input in forms.
When it’s enabled, two users accessing the same content may see dates, times and numbers formatted in different ways, depending on the formats for their current locale.
The formatting system is disabled by default. To enable it, it’s necessary to set USE_L10N = True in your settings file.
Thus this first option turns on locale-dependent number formatting for all of your templates.
A second option is to use the localize template tag to turn on locale-dependent number formatting for only part of a template. For example,
{% load l10n %}
{% localize on %}
{{ value }}
{% endlocalize %}
A third option is to use the localize template filter to force locale-dependent number formatting for a single value. For example,
{% load l10n %}
{{ value|localize }}

Django separate thousands in template

My model is returning a Decimal(1234567.50), I can't seem to display the Decimal with a thousands separator. Does Django have a way to do this? Do I need to create my own template filter?
Thanks.
You can use the intcomma filter.
Add humanize to the installed apps in settings.py and load it in the template.
{% load humanize %}
{{ my_num|intcomma }}
One easy way is to import locale and do the formatting in your view function.
Even easier to read this: http://docs.djangoproject.com/en/dev/topics/i18n/#overview
For format localization, it’s just
necessary to set USE_L10N = True in
your settings file. If USE_L10N is set
to True, Django will display numbers
and dates in the format of the current
locale. That includes field
representation on templates, and
allowed input formats on the admin.
You could add the following to settings.py:
USE_THOUSAND_SEPARATOR = True
That should work and you don't have to act with loading stuff.
https://docs.djangoproject.com/en/4.1/ref/settings/#use-thousand-separator