问题
I have formally constructed the function in my models.py
file :
from datetime import datetime
from django.template.defaultfilters import date as datefilter
from django.utils import translation
def date_customerprofile(language):
now_ = datetime.today()
if language == 'English':
translation.activate('en')
return datefilter(now_, 'l, F j, Y')
else:
translation.activate('fr')
return datefilter(now_, 'l, j F Y')
I know Django's template language is NOT Python, so we can't write {{ customer.date_customerprofile('French') }}
.
The solution here is to create a custom templatetag. Also note that translation.activate
will change the active language for the remaining of the request/response cycle - using the translation.override
context manager is certainly a better idea. As I am not an expert with templatetags, could anyone be able to show me how could I create it in taking into consideration the above function?
I think we could use the following question to do such thing How to format date in different languages?
Could I use context manager : Django switching, for a block of code, switch the language so translations are done in one language?
Thanks!
In fact, I would like to return in the template as output the current date in French 'Jeudi, 30 mars 2017' or the current date in English Thursday, March 30, 2017
.
P.S. Please tell me if the question is unclear.
This is an example of Django's template :
{% load i18n %}
{% load date %}
{{ customer.civil_title }}{{ user.get_full_name }}
{{ customer.civic_number }}, {{ customer.street }}
{{ customer.rough_location }}
{# customer. date_customerprofile('English') #}
7 DAYS NOTICE
Subject: Overdue account with ...
Client id : {{ customer.id }}
Hello {{ customer.civil_title }}{{ user.get_full_name }},
This notice serves to inform you that your payments with Gestion Multi Finance Inc. are pending. Your file has been transferred to our collection service. The balance of your loan is now: {{ customer.current_balance }}.
We are asking you to communicate with us without delay, by phone at: 1-855-... ext. 324 or by email at: ... to find a payment solution in order to regulate your situation. If this does not happen, we will be forced to send you a notice of acceleration of the process and transfer your file to our judiciary collection service, with authority to take the necessary steps in the collection process. These steps can include the seizure of salary, but we prefer to avoid these measures.
Also, it is important to remember that at the time of the loan, you signed a contract in which you agreed to respect the payments. Unfortunately, you have no respected this agreement.
Ignore this notice if an agreement has already been reached.
Cordially,
{{ email.footer.txt }}
回答1:
Now we've seen the total nonsense answer, here's the no-nonsense one:
In your yourapp/templatetags/yourapptags.py
module:
from django import template
register = template.Library()
DATE_FORMATS = {
"en": "l, F j, Y",
"fr": "l, j F Y"
}
DEFAULT_LANG = 'fr'
@register.simple_tag
def localdate(lang=DEFAULT_LANG):
fmt = DATE_FORMATS.get(lang, DATE_FORMATS[DEFAULT_LANG])
now = datetime.now()
with translation.override(lang):
return datefilter(now, fmt)
Then in your template:
{% load "yourapptags" %}
<p>EN : {% localdate 'en' %}</p>
<p>FR : {% localdate 'fr' %}</p>
Note that this is for forcing date formatting / localisation in a given locale whatever the current locale is which in this case seems quite weird to me.
If what you really want is just to display the current date formatted according to the current visitor's locale (cf https://docs.djangoproject.com/en/1.10/topics/i18n/translation/#how-django-discovers-language-preference), you can just get rid of the lang
argument and use translation.get_language()
instead:
@register.simple_tag
def localdate():
lang = translation.get_language()
fmt = DATE_FORMATS.get(lang, DATE_FORMATS[DEFAULT_LANG])
now = datetime.now()
return datefilter(now, fmt)
Also note that l10n formats files provided by Django already have some date formats defined and that you can override them.
来源:https://stackoverflow.com/questions/43120100/create-template-tags-from-the-current-function