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

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

Related

Display language icons in Django template

I want to display language icons instead of names in Django template.
My code looks like this:
{% load static i18n %}
{% get_current_language as CURRENT_LANGUAGE %}
{% get_available_languages as AVAILABLE_LANGUAGES %}
{% get_language_info_list for AVAILABLE_LANGUAGES as languages %}
<div id="language">
{% for language in languages %}
<ul>
<li>
<a href="/{{ language.code }}/"
{% if language.code == LANGUAGE_CODE %} class="active"{% endif %}>
{{ language.name|slice:":3" }}
</a>
</li>
</ul>
{% endfor %}
</div>
Is there any possible ways to achieve that goal or I have to try different ways?
solution for that was to change that rows to this:
<a href="/{{ language.code }}/"
{% if language.code == LANGUAGE_CODE %} class="active"{% endif %}>
<img src="/static/images/{{ language.name }}.png" alt="Geo">
</a>
And you have to place icons in static folder with the language names for english -> English and so on.
The harder part is assembling your set of country flag images together somewhere.
The easy part is using your language context to get an <img> reference, for example
<img ... src="somewhere/{{language.name|slice:":3"}}.gif" >
You might want to take a look at the django-countries package. Use it, or use it for ideas.

Django Language Change Anchors vs. Options

I have a dropdown list of languages that looks something like the following:
<ul class="languages hoverSelectorBlock">
{% get_current_language as current_lang %}
{% for lang in LANGUAGES %}
<li><a href="{% url 'set_language' %}" lang-code="{{ lang.0 }}"
class="change_language{% if current_lang == lang.0 %} current_language {% endif %}"
{% if current_lang == lang.0 %}selected="selected"
{% else %}selected=""{% endif %}>{{ lang.1 }}</a></li>
{% endfor %}
</ul>
The documentation says that it needs to be submitted as a POST request. From my understanding that means I need to change the anchor tags into options. But I want the same behavior such that when you click a language it will submit the form and reload the current page in that language.
Will the set_language view work with anchor tags? If I need to change them to a select with options, how can I submit the form when one of the options is clicked without the need for a submit button?
If you use select options and wrap them in a form with an id. You can submit the form on the select element's change event.
<form id="my_form">
<select onchange="submit_my_form()">
...
</select>
</form>
function submit_my_form(){
document.getElementById("my_form").submit();
}

How to use the teaser_text templatetag of cmsplugin-articles 0.2.2

I'm making a site for which I use cmsplugin-articles 0.2.2. That means that there's a page on the site (News) which has nested child pages which are the news. In the parent page you view a list of news teasers. (This is the way the plugin works.)
Looking into the template of the article teaser that the plugin provides:
{% load article_tags i18n %}
<div class="article">
{% block teaser_head %}
<time datetime="{{ article|published_at|date:"Y-m-d" }}">{{ article|published_at|date:"d.m.Y" }}</time>
<h2>{{ article|teaser_title }}</h2>
{% endblock %}
{% block teaser_body %}
{% with article|teaser_image as image %}
{% if image %}
<img src="{{ image.url }}" />
{% endif %}
{% endwith %}
<p>
{% teaser_text article %}
{% trans "More" %}
</p>
{% endblock %}
</div>
you can see the templatetag {% teaser_text article %} that should show some kind of text in the news teaser when rendering the News page.
I've published some news with some text for each one and when I go to the News page I can see the teasers list ok, but not any text, only the title, date, paginator and the "more" link, that proceed from other templatetags different than {% teaser_text %}.
Now the question is: does anybody knows how I can show each news text into each teaser (better an extract) when rendering the News page?

Django URL Translation - Stay on the same page when changing language

I developped a Django app with i18n for urls as well.
That look really nice but when changing the language I would like to stay on the same/previous page.
What is the best way of doing that ?
Basically to get the new url I need to do a reverse on the name of the previous page after having changed the language and do a redirect but how can I know the url name of the previous page?
Edit:
A solution that came from a collegue:
Calculate a next parameter for each language using request.resolver_match.
For each language : activate(language) + reverse('{app_name}:{url_name}', args, kwargs) using request.resolver_match elements
Do you see a better idea?
After change language with django-modeltranslation redirecting to home ?
If you want to redirect to the same page, you can replace this part of code:
{% get_current_language as LANGUAGE_CODE %}
{% get_available_languages as LANGUAGES %}
{% get_language_info_list for LANGUAGES as languages %}
<div class="languages">
<p>{% trans "Language" %}: </p>
<ul class="languages">
{% for language in languages %}
<li>
<a href="/{{ language.code }}/
{% if language.code == LANGUAGE_CODE %} class="selected"{% endif %}>
{{ language.name_local }}
</a>
</li>
{% endfor %}
</ul>
</div>
to:
{% get_current_language as LANGUAGE_CODE %}
{% get_available_languages as LANGUAGES %}
{% get_language_info_list for LANGUAGES as languages %}
<div class="languages">
<p>{% trans "Language" %}: </p>
<ul class="languages">
{% for language in languages %}
<li>
<a href="/{{ language.code }}/{{request.get_full_path|slice:"4:"}}"
{% if language.code == LANGUAGE_CODE %} class="selected"{% endif %}>
{{ language.name_local }}
</a>
</li>
{% endfor %}
</ul>
</div>
Attention please:
<a href="/{{ language.code }}/
replaced to <a href="/{{ language.code }}/{{request.get_full_path|slice:"4:"}}"
Two options for you:
Option 1
If you use the form from the documentation then will take you which brings you back to the page you were on.
Option 2
When changing the language you could use the referrer header, HTTP_REFERER , and redirect back to where you came from
# Change the language
# ... code ...
# Redirect back to where we came from
redirect_to = request.META.get('HTTP_REFERER', reverse('default-redirect-page'))
return HttpResponseRedirect(redirect_to)

How I can use another Translation for a template rendering

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.