可翻译模板
django-templates-i18n的Python项目详细描述
应用程序提供了在数据库中存储模板并使用django modeltransflation应用程序本地化模板的能力。 如果您需要经常更改模板内容,并且不想重新编译所有ugettext消息,则此功能非常有用。 一直如此。它也适用于电子邮件模板。
在Django 1.4.5测试。
要求
- Django
- Django模型转换
安装
使用pip安装python库:pip install django-templates-i18n
将templates_i18n添加到django设置文件中的INSTALLED_APPS。
同步并迁移数据库
在django设置文件中指定所需的语言:
from django.utils.translation import gettext LANGUAGE_CODE = 'en' LANGUAGES = ( ('en', gettext('English')), ('de', gettext('German')), )
运行sync_translation_fields和update_translation_fields命令(来自modeltranslationapp)
用法
每当父字段更改时,都会进行ajax调用。必须设置ajax url以返回json列表:
from django.http import HttpResponse from django.template import Template, Context from django.views.generic import View from templates_i18n.models import Template_i18n class MyView(View): def dispatch(self, request, *args, **kwargs): template_i18n = Template_i18n.objects.get(machine_name='my-template') template = Template(template_i18n.content) context = Context({'user': request.user}) return HttpResponse(template.render(context))
或:
from django.core.mail import send_mail from django.template import Template, Context from templates_i18n.models import Template_i18n def dispatch(self, request, *args, **kwargs): template_i18n = Template_i18n.objects.get(machine_name='my-template') template = Template(template_i18n.content) context = Context({'user': request.user}) message = template.render(context) send_mail('Subject here', message, 'from@example.com', ['to@example.com'], fail_silently=False)