简体   繁体   English

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

[英]Django templates: include template passing translated variable

I need to pass the following into an included template via the Django include tag: 我需要通过Django include标签将以下内容传递给包含的模板:

{% include 'btn.html' with
     btn_text='Hi '|add:first_name|add:' - Verify Email Now'
     btn_url=verify_url
%}

Therefore I can dissect the entire question in two parts: 因此,我可以将整个问题分为两部分:

A. Is it possible to have first_name added into the string in another, more elegant, manner at template level ? A.是否可以在模板级别以另一种更优雅的方式将first_name添加到字符串中?

B. I need to have the string translated at template level - is it possible? B.我需要在模板级别翻译字符串 - 这可能吗?

Ie what I intend to do (but is not syntactically correct) is the below: 即我打算做什么(但在语法上正确)如下:

{% include 'btn.html' with
     btn_text=
         {% blocktrans first_name as first_name %}
             Hi {{first_name}} - Verify Email Now
         {% endblocktrans %}
     btn_url=verify_url
%}

I found the solution in this post : 我在这篇文章中找到了解决方案:

Here is the given example: 这是给出的例子:

{% trans "Load more promotions" as promotions %}
{% include "a_dir/stuff.html" with text=promotions %}

To format the string, you could do that in the view and pass it in the context: 要格式化字符串,您可以在视图中执行此操作并在上下文中传递它:

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

For translation of text, have a look at the following link: 有关文本的翻译,请查看以下链接:
https://docs.djangoproject.com/en/1.2/topics/i18n/internationalization/#trans-template-tag https://docs.djangoproject.com/en/1.2/topics/i18n/internationalization/#trans-template-tag

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM