Django模板:包含传递已翻译变量的模板

Jos*_*mit 9 django django-templates

我需要通过Django include标记将以下内容传递给包含的模板:

{% include 'btn.html' with
     btn_text='Hi '|add:first_name|add:' - Verify Email Now'
     btn_url=verify_url
%}
Run Code Online (Sandbox Code Playgroud)

因此,我可以将整个问题分为两部分:

A.是否可以在模板级别first_name以另一种更优雅的方式添加到字符串中?

B.我需要在模板级别翻译字符串 - 这可能吗?

即我打算做什么(但在语法上正确)如下:

{% include 'btn.html' with
     btn_text=
         {% blocktrans first_name as first_name %}
             Hi {{first_name}} - Verify Email Now
         {% endblocktrans %}
     btn_url=verify_url
%}
Run Code Online (Sandbox Code Playgroud)

Emi*_*nte 9

我在这篇文章中找到了解决方案:

这是给出的例子:

{% trans "Load more promotions" as promotions %}
{% include "a_dir/stuff.html" with text=promotions %}
Run Code Online (Sandbox Code Playgroud)


小智 -1

要格式化字符串,您可以在视图中执行此操作并将其传递到上下文中:

context = {'btn_text': 'Hi {0} - Verify Email Now'.format(first_name)}
return HttpResponse(context=context)
Run Code Online (Sandbox Code Playgroud)

要翻译文本,请查看以下链接:
https://docs.djangoproject.com/en/1.2/topics/i18n/internationalization/#trans-template-tag