简体   繁体   中英

Django template translation in block not possible

Using Django 1.7.1 under Python 3.4, I have a problem where I did not find any answer to, despite seeming banal, andhaving seen many similar problems all over the internet. No solution that I understood thus far.

I have a simple django template, boiled down to the essential part. It is a simle "trans"-tag translated string within a content block that overrides the admin/base content block.

{% extends "admin/base.html" %}
{% block content %}
    {% trans "Entries" %}
{% endblock %}

With this I get a TemplateSyntaxError :

Invalid block tag: 'trans', expected 'endblock'

What I learned until now it that you need to use a blocktrans for something like that. But I don't get it - WHY the heck does this not work? Funny thing is, if I replace the {% trans "Entries" %} with a {{ _("Entries") }} everything works as expected. But I need the additional benefits you get with the trans tag.

There are plenty of examples, like here , which use exactly that pattern above - and it whould work. Any help welcome.

Possibly you are missing this instruction at the top of your template?

{% load i18n %}

https://docs.djangoproject.com/en/1.7/topics/i18n/translation/#internationalization-in-template-code

i tried using this command:

python manage.py makemessages zh_SG

instead of

python manage.py makemessages zh-sg

(note the underscore and capital letters ending) and also made sure that each app has a locale folder (makemessages may create locale folder outside the apps which is a wrong location)

This worked for me, hope it will help you ( https://stackoverflow.com/a/1833340/5798298 )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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