How I can use another Translation for a template rendering - django

How I can say to the render function which language my e-mail should use if I use i18n?
FYI:
It now looks like this
{% load i18n %}
{% language language %}
{% trans "Hello" %} {{ name }}
{% endlanguage %}

You can wrap the template in the language tag:
{% load i18n %}
{% language email_language %}
...
{% endlanguage %}
In your Python code you should pass the value for email_language to the template. This is the language in which the e-mail will be rendered as the language tag activates that language for that part of the template.

Related

{% block %} in {% blocktrans %}

I am planning to implement Arabic translation that translates it making a whole sentence to be written right-to-left. How should I do this?:
<!-- parent.html -->
{% blocktrans %}
{% block category %}{% endblock category %} - Site Name
{% endblocktrans %}
<!-- child.html -->
{% extends 'parent.html' %}
{% block category %}Books{% endblock category %}
for Arabic translation I need to use blocktrans, and I need to use different children names as well.
The docs of blocktrans state the following:
Other block tags (for example {% for %} or {% if %}) are not allowed inside a blocktrans tag.
block is another block tag, so it is not supported inside blocktrans either. The only thing allowed inside blocktrans is a direct variable substitution (without any filters, or attribute / method resolution using .).
In other words, what you're asking for is simply not possible with blocktrans. You'll have to reorganize your templates accordingly.

How to hide an untranslated page from language chooser in django cms?

I want to hide an untranslated page from the language chooser in django cms so that the language chooser shows only the languages that have translations for that page. How can I do that?
One idea is to extend the language chooser template and check there if the language has a translated page but I couldn't find out how to do that.
Actually there is a beautiful solution, first posted here: https://groups.google.com/forum/#!topic/django-cms/z1rdf4C-ltQ
current_page.get_languages is the solution. Works with djangocms 3 in my Aldryn project.
includes/menu/langnav.html:
{% load i18n menu_tags %}
{% if languages|length > 1 %}
{% comment %}
This is awesome: https://groups.google.com/forum/#!topic/django-cms/z1rdf4C-ltQ
{% endcomment %}
<li class="lang">
{% for language in current_page.get_languages %}
<a class="{{ language }}{% ifequal current_language language %} selected{% endifequal %}"
href="{% page_language_url language %}">{{ language }}</a>
{% endfor %}
</li>
{% endif %}
in django cms 3.0:
{% if page and language in page.languages %}
for 2.4:
you probably will a custom filter or templatetag that runs::
if page.title_set.filter(language=lang).count():
return True
else:
return False

How to specify context of translation in Django {% trans %} {% blocktrans %}?

Documentation of Django says Contextual markers are also supported by the trans and blocktrans template tags. but it not explained how to do it?
Can you help marking translation context since I have some words with several meanings.
In Python I can do in such way:
pgettext("month name", "May")
pgettext("verb", "May")
How to specify translation context in Django template?
{% blocktrans %}May{% endblocktrans %}
It is explained at the very end of their specific paragraphs:
https://docs.djangoproject.com/en/dev/topics/i18n/translation/#trans-template-tag
{% trans %} also supports contextual markers using the context keyword:
{% trans "May" context "month name" %}
https://docs.djangoproject.com/en/dev/topics/i18n/translation/#blocktrans-template-tag
{% blocktrans %} also supports contextual markers using the context keyword:
{% blocktrans with name=user.username context "greeting" %}Hi {{ name }}{% endblocktrans %}
{% blocktrans context "month name" %}May{% endblocktrans %}

How to use {% with %} along with {% include %} -- Django

For example, I have a template file called:
filter.html
{{ title }}
code...
What I'd like to do is, on a separate template:
{% with "Filter by Types" as title %}
{% include "filter.html" %}
{% endwith %}
Currently it can't be done. Could someone explain why that is and an alternative way to achieve this?
Background context:
The app base is used for multiple sites. The site admin would only be able to edit the template files to give them a degree of customization, but not the views.py or other core files. So the {{ title }} variable can't really be sent by the views.py.
I might be missing something but why not just use extends and block tags?
base.html
{% block title %}Default title{% endblock %}
filter.html
{% extends "base.html" %}
{% block title %}Filter by Types{% endblock %}
Check out the documentation on extends, blocks and template inheritance for more info.

django blocktrans and i18n in templates

I have an i18n problem in django:
This works fine :
{% trans cat.name %}
cat.name will be translated
But this doesn't work:
{% blocktrans with cat.name|slugify as cat_slug %}{{ cat_slug }}{% endblocktrans %}
cat.name is not translated
If I change the filter :
{% blocktrans with cat.name|capfirst as cat_slug %}{{ cat_slug }}{% endblocktrans %}
I can see that the filter is working, but there is no translation...
I'm only just getting started with Django internationalization, but I think you're misunderstanding how the {% blocktrans %} tag handles placeholders.
The point of blocktrans is to allow the text around the placeholders to be translated. It won't translate anything inside {{...}}.
If you look at the generated .po file, you'll see that the following template code:
{% blocktrans %}This is my variable: {{variable}}{% endblocktrans %}
Will get converted into something like the following:
msgid:"This is my variable: %s"
I don't think you can translate a variable within a blocktrans tag. You can probably do constant strings with {% blocktrans with _("string") as x %}{{x}}{% endblocktrans %} but I can't think why you'd want to.
You'll have to do what you want in your view or model code I think.
This works:
{% filter slugify %}{% trans cat.name %}{% endfilter %}
As Tom pointed out blocktrans will preserve what you put inside the with statement instead of translating it. What you need to do is use the with before the translation. In your example, it would look like this:
{% with cat_slug=cat.name|slugify %}
{% trans cat_slug %}
{% endwith %}
P.S. I know I'm answering a 6yr old question, but I've run across this exact situation a couple times now and haven't seen a SO question/answer that handles it.
{% blocktrans with cat.name as cat_slug %}{{ cat_slug|capfirst }}{% endblocktrans %}
?
EDIT: you were right the doc says the filter as to be placed in the blocktrans
{% blocktrans with cat_slug=cat.name|capfirst %}{{ cat_slug }}{% endblocktrans %}