Add language switcher widget

This commit is contained in:
Carl van Tonder 2018-05-19 13:52:19 -04:00
parent 8a68b826d3
commit 0160e58491
4 changed files with 41 additions and 0 deletions

View File

@ -0,0 +1,8 @@
$(function() {
$('#id-language-dropdown').change(function() {
window.location = ('/set_language/'
+ $('#id-language-dropdown').find(':selected').val()
+ "?from=" + encodeURI(window.location.href)
);
})
});

View File

@ -78,6 +78,22 @@
{% endfor %}
</ul>
<ul class="nav navbar-nav navbar-right">
<li class="form-group">
<form class="navbar-form navbar-left">
{# Global language selection dropdown #}
{% get_available_languages as LANGUAGES %}
{% get_current_language as LANGUAGE_CODE %}
<select id="id-language-dropdown" name="language-dropdown">
{% for language in LANGUAGES %}
{% if language.0 == LANGUAGE_CODE %}
<option value="{{ language.0 }}" selected="selected">{{ language.0|language_name_local|title }}</option>
{% else %}
<option value="{{ language.0 }}">{{ language.0|language_name_local|title }}</option>
{% endif %}
{% endfor %}
</select>
</form>
</li>
{% if user.is_authenticated %}
<a class="btn btn-primary navbar-btn" href="{% url 'create' %}"><i class="fa fa-plus" aria-hidden="true"></i> New Case Study</a>
<li class="dropdown">
@ -134,6 +150,7 @@
{# CDN Javascript #}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.min.js" integrity="sha256-JmvOoLtYsmqlsWxa7mDSLMwa6dZ9rrIdtrrVYRnDRH0=" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<script src="{% static "map/language.js" %}"></script>
{% block scripts %}{% endblock %}
</html>

View File

@ -22,6 +22,8 @@ from apps.map.models import CaseStudy
from rest_framework import routers, serializers, viewsets
from rest_framework_gis import serializers as gis_serializers
from .views import LanguageDropdownView
class UserSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = User
@ -69,6 +71,7 @@ urlpatterns = [
url(r'^avatar/', include('avatar.urls')),
url(r'^cas/', include('cas_server.urls', namespace='cas_server')),
url(r'^files/', include('apps.files.urls')),
url(r'^set_language/(?P<language>[^/]+)$', LanguageDropdownView.as_view()),
# url(r'^contact/', include('apps.contact.urls'), name="contact"),
]

13
ojusomap/views.py Normal file
View File

@ -0,0 +1,13 @@
from django.utils import translation
from django.views.i18n import set_language
from django.views.generic import View
class LanguageDropdownView(View):
def get(self, request, *args, **kwargs):
lang_code = kwargs.get('language', None)
if lang_code is not None and translation.check_for_language(lang_code):
request.POST = request.POST.copy()
request.POST['language'] = lang_code
request.method = 'POST'
return set_language(request)