I 'm overriding django-admin templates to add a language choice links.
I added this:
{% get_current_language as LANGUAGE_CODE %}
{% get_available_languages as LANGUAGES %}
{% get_language_info_list for LANGUAGES as languages %}
{% for language in languages %}
<a href="/{{ language.code }}{{ request.get_full_path|slice:'3:' }}"
class="{% if language.code == LANGUAGE_CODE %}selected{% endif %}"
lang="{{ language.code }}">
{{ language.name_local }}
</a>
{% endfor %}
in both base_site.html and login.html.
They work fine
The problem is that I always get the default language in the dashboard.
For example:
If I choose Frensh in the login page, the login page gets translated into frensh but after I login, I find the dashboard and other pages in the default language.
How can I fix this, in order to display the dashboard in the language chosen from the login page
I finally fixed it.
I changed the href in the login.html as follows:
<a href="/{{ language.code }}{{ request.get_full_path|slice:'3:23' }}{{ language.code }}/admin/"
Infact, the login url is as follows : /fr/admin/login/?next=/fr/admin/
so I need to keep in mind the next page url.
I just needed to change the language code in the actual page slice:'3:' and in the next page slice:':23'.
Related
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.
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();
}
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
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)
Hi I have an html template for switching languages on my site:
<form action="{{ SITE_URL }}i18n/setlang/" name="postlink" method="post">{% csrf_token %}
<ul class="lang">
<li class="lang">
{% for lang in LANGUAGES %}
{% if lang.0 != LANGUAGE_CODE %}
<input type="hidden" name="language" value="{{ lang.0 }}">
<a class="active" href=# onclick="submitPostLink()">{{ lang.1 }}</a>
{% else %}
{{ lang.1 }}
{% endif %}
{% if forloop.last %}{% else %} | {% endif %}
{% endfor %}
</li>
</ul>
</form>
All this works fine on the dev server and in production and in production at the normal url of www.mysite.com/project/
However, if I try project.mysite.com or mysite.com.project I get my home page as normal but changing the language brings up the 403 Forbidden failure.
Do I need to define the root url variations I need for setlang somewhere?
Any help much appreciated.
Since these urls are running on different subdomains, you should check the domain setting for the CSRF cookie CSRF_COOKIE_DOMAIN: https://docs.djangoproject.com/en/1.3/ref/contrib/csrf/#subdomains