Django: How to force translation into a given language inside a template?

In the Django template, I need to translate some lines into a specific language (different from the current one).

I need something like this:

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

to force the translation into German.

I know that 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 template tag? Or does he already have a special tag in Django?

+5
source share
4 answers

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>
+9
source

There 1.6is a template tag from the Django version language, so you can just pass the desired language code:

{% language "de" %}my block to translate{% endlanguage %}
+13

Zalew, locale:

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

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

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

def trans_to(parser, token):
    """ 
    force translation into a given language
      usage : {% trans_to "string to translate" locale %}
    """
    try:
        tag_name, value, lc = token.split_contents()
    except ValueError:
            raise TemplateSyntaxError, '%r tag usage: {%% trans_to "string to translate" locale %%}' % token.contents.split()[0]
    return TransNode(value, lc)

register.tag('trans_to', trans_to)
+3

trans :

{% load i18n %}
{% trans "Hello" %}

USE_I18N True settings.py.

LANGUAGE_CODE LocaleMiddleware MIDDLEWARE_CLASSES. Django Discovery Language Preference .

-3

All Articles