从当前函数创建模板标签

Create template tags from the current function

我已经在我的 models.py 文件中正式构造函数:

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')

我知道 Django 的模板语言不是 Python,所以我们不能写 {{ customer.date_customerprofile('French') }}

此处的解决方案是创建自定义模板标签。另请注意,translation.activate 将更改 request/response 周期剩余时间的活动语言 - 使用 translation.override 上下文管理器当然是一个更好的主意。由于我不是 templatetags 专家,谁能告诉我如何在考虑上述功能的情况下创建它?

我想我们可以用下面的问题来做这样的事情How to format date in different languages?

我可以使用上下文管理器吗:Django switching, for a block of code, switch the language so translations are done in one language?

谢谢!

事实上,我想 return 在模板中输出当前日期的法语 'Jeudi, 30 mars 2017' 或当前日期的英语 Thursday, March 30, 2017

P.S.有什么不明白的地方请告诉我。

这是 Django 模板的示例:

{% 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 }}

现在我们已经看到了全部废话答案,下面是严肃的答案:

在您的 yourapp/templatetags/yourapptags.py 模块中:

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)

然后在您的模板中:

{% load "yourapptags" %}

<p>EN : {% localdate 'en' %}</p>
<p>FR : {% localdate 'fr' %}</p>

请注意,这是为了在给定的语言环境中强制进行日期格式化/本地化,无论当前语言环境是什么,在这种情况下对我来说似乎很奇怪。

如果您真正想要的只是显示根据当前访问者的区域设置格式化的当前日期 (cf https://docs.djangoproject.com/en/1.10/topics/i18n/translation/#how-django-discovers-language-preference),您可以去掉 lang 参数并使用 translation.get_language() 而不是:

@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)

另请注意,Django 提供的 l10n 格式文件已经有 some date formats defined and that you can override them.