Django:如何强制翻译成模板中的给定语言? [英] Django: How to force translation into a given language inside a template?

查看:214
本文介绍了Django:如何强制翻译成模板中的给定语言?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在Django模板中,我需要将一些字符串转换成特定语言(不同于当前的语言)。 >

  {%tans_tode我要翻译的字符串%} 

{%blocktrans_tode %}我的团体翻译{%endblocktrans_to%}

强制翻译成德语。



我知道我可以在视图中调用以下代码:

  gettext.translation ('django','locale',['de'],fallback = True).ugettext(我的字符串要翻译)

我需要创建一个特定的模板标签吗?还是Django中已经存在一个专用标签?

解决方案

templatetags / trans_to.py:


从django.utils导入翻译
$ d
从django.utils.translation导入ugettext
从django.template导入库,节点,变量,TemplateSyntaxError
register = Library()

class TransNode(Node):
def __init __(self,value,lc):
self.value = Variable(value)
self.lc = lc

def render(self,context):
translation.activate(self.lc)
val = ugettext(self.value.resolve上下文))
translation.deactivate()
return val

def trans_to(解析器,令牌):
try:
tag_name,value,lc = token.split_contents()
除了ValueError:
raise TemplateSyntaxError,%r tag requires arguments%token.contents.split()[0]
如果没有(lc [0] == lc [-1]和lc [0] in(''''''))
raise TemplateSyntaxError,%r locale应该是引号%tag_name
return TransNode(value,lc [1:-1])$ ​​b
$ b register.tag('trans_to',trans_to)

html:

  {%load trans_to%} 
{#pass string#}
< p> {%trans_totest de%}< / p>
< p> {%transtest%}< / p>
{#pass variable#}
{%withtestas a_variable%}
< p> {%trans_to a_variablede%}< / p>
< p> {%trans a_variable%}< / p>
{%endwith%}

结果:

 < p> test in deutsch< / p> 
< p> test< / p>
< p> test in deutsch< / p>
< p> test< / p>


In a Django template, I need to transanlate some strings to a specific language (different from current one).

I would need something like this:

{% tans_to "de" "my string to translate" %}
or
{% blocktrans_to "de" %}my bloc to translate {% endblocktrans_to %}

to force translation to German.

I know I can call the following code in a view:

gettext.translation('django', 'locale', ['de'], fallback=True).ugettext("my string to translate")

Do I need to create a specific template tag ? Or does it already exist a dedicated tag in Django ?

解决方案

templatetags/trans_to.py:

from django.utils import translation
from django.utils.translation import ugettext
from django.template import Library, Node,  Variable, TemplateSyntaxError
register = Library()

class TransNode(Node):
    def __init__(self, value, lc):
        self.value = Variable(value)
        self.lc = lc

    def render(self, context):        
        translation.activate(self.lc)
        val = ugettext(self.value.resolve(context))        
        translation.deactivate()        
        return val

def trans_to(parser, token):
    try:
        tag_name, value, lc = token.split_contents()
    except ValueError:
        raise TemplateSyntaxError, "%r tag requires arguments" % token.contents.split()[0]
    if not (lc[0] == lc[-1] and lc[0] in ('"', "'")):
        raise TemplateSyntaxError, "%r locale should be in quotes" % tag_name 
    return TransNode(value, lc[1:-1])

register.tag('trans_to', trans_to)

html:

{% load trans_to %}
{# pass string #}   
<p>{% trans_to "test" "de" %}</p>
<p>{% trans "test" %}</p>
{# pass variable #}
{% with "test" as a_variable %}
<p>{% trans_to a_variable "de" %}</p>
<p>{% trans a_variable %}</p>       
{% endwith %}

result:

<p>test in deutsch</p>
<p>test</p>
<p>test in deutsch</p>
<p>test</p>

这篇关于Django:如何强制翻译成模板中的给定语言?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆