mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-01-21 07:55:00 +00:00
locale: Anonymous users can set preferred language
Logged-in user flow is pending. Check [How django discovers language preference]: https://docs.djangoproject.com/en/1.10/topics/i18n/translation/#how-django-discovers-language-preference Signed-off-by: Sai Kiran Naragam <saikiran.rguiiit@gmail.com> Reviewed-by: Sunil Mohan Adapa <sunil@medhas.org>
This commit is contained in:
parent
cf31c47d9d
commit
5cff5629d8
@ -20,10 +20,15 @@ Common forms for use by modules.
|
||||
"""
|
||||
|
||||
from django import forms
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
from django.conf import settings
|
||||
from django.utils import translation
|
||||
from django.utils.translation import ugettext_lazy as _, get_language_info
|
||||
|
||||
import plinth
|
||||
from plinth import utils
|
||||
|
||||
import os
|
||||
|
||||
|
||||
class ServiceForm(forms.Form):
|
||||
"""Generic configuration form for a service."""
|
||||
@ -48,3 +53,21 @@ class DomainSelectionForm(forms.Form):
|
||||
),
|
||||
choices=[]
|
||||
)
|
||||
|
||||
|
||||
class LanguageSelectionForm(forms.Form):
|
||||
"""Form for selecting the user's preferred language """
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
supported_languages = []
|
||||
for language_code, language_name in settings.LANGUAGES:
|
||||
locale_code = translation.to_locale(language_code)
|
||||
plinth_dir = os.path.dirname(plinth.__file__)
|
||||
if language_code == 'en' or os.path.exists(
|
||||
os.path.join(plinth_dir, 'locale', locale_code)):
|
||||
supported_languages.append((language_code, get_language_info(language_code)['name_local']))
|
||||
|
||||
self.fields['language'].choices = supported_languages
|
||||
|
||||
language = forms.ChoiceField(label='Language', choices=[])
|
||||
|
||||
@ -172,6 +172,11 @@
|
||||
</ul>
|
||||
</li>
|
||||
{% else %}
|
||||
<li>
|
||||
<a href="{% url 'language-selection' %}" title="{% trans "Select Language" %}">
|
||||
<i class="glyphicon glyphicon-globe nav-icon"></i>
|
||||
</a>
|
||||
</li>
|
||||
<li>
|
||||
<a href="{% url 'users:login' %}" title="{% trans "Log in" %}">
|
||||
<i class="glyphicon glyphicon-user nav-icon"></i>
|
||||
|
||||
38
plinth/templates/language-selection.html
Normal file
38
plinth/templates/language-selection.html
Normal file
@ -0,0 +1,38 @@
|
||||
{% extends 'base.html' %}
|
||||
{% comment %}
|
||||
#
|
||||
# This file is part of Plinth.
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Affero General Public License as
|
||||
# published by the Free Software Foundation, either version 3 of the
|
||||
# License, or (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Affero General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Affero General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
#
|
||||
{% endcomment %}
|
||||
|
||||
{% load i18n %}
|
||||
|
||||
{% load bootstrap %}
|
||||
|
||||
{% block content%}
|
||||
|
||||
<h3> Choose your language </h3>
|
||||
|
||||
<form method="post">
|
||||
{% csrf_token %}
|
||||
|
||||
{{ form|bootstrap }}
|
||||
|
||||
<input type="submit" class="btn btn-primary"
|
||||
value="{% trans "Save Changes" %}"/>
|
||||
</form>
|
||||
|
||||
{% endblock %}
|
||||
@ -28,6 +28,7 @@ from . import views
|
||||
|
||||
urlpatterns = [
|
||||
url(r'^$', views.index, name='index'),
|
||||
url(r'^language-selection', public(views.LanguageSelectionView.as_view()), name='language-selection'),
|
||||
url(r'^apps/$',
|
||||
TemplateView.as_view(template_name='apps.html'), name='apps'),
|
||||
url(r'^sys/$', views.system_index, name='system'),
|
||||
|
||||
@ -23,6 +23,11 @@ from django.core.exceptions import ImproperlyConfigured
|
||||
from django.template.response import TemplateResponse
|
||||
from django.views.generic import TemplateView
|
||||
from django.views.generic.edit import FormView
|
||||
from django.http import HttpResponseRedirect
|
||||
from django.conf import settings
|
||||
from django.shortcuts import render
|
||||
from django.urls import reverse
|
||||
from django.utils import translation
|
||||
from django.utils.translation import ugettext as _
|
||||
from stronghold.decorators import public
|
||||
import time
|
||||
@ -63,6 +68,30 @@ def system_index(request):
|
||||
return TemplateResponse(request, 'system.html')
|
||||
|
||||
|
||||
class LanguageSelectionView(FormView):
|
||||
"""View for language selection"""
|
||||
form_class = forms.LanguageSelectionForm
|
||||
template_name = 'language-selection.html'
|
||||
|
||||
def get(self, request, *args, **kwargs):
|
||||
current_values = {'language': translation.get_language()}
|
||||
form = self.form_class(initial=current_values)
|
||||
return render(request, self.template_name, {'form': form})
|
||||
|
||||
def post(self, request, *args, **kwargs):
|
||||
form = self.form_class(request.POST)
|
||||
if form.is_valid():
|
||||
selected_language = form.cleaned_data['language']
|
||||
translation.activate(selected_language)
|
||||
# set selected language in session
|
||||
request.session[translation.LANGUAGE_SESSION_KEY] = selected_language
|
||||
response = HttpResponseRedirect(reverse('language-selection'))
|
||||
# send a cookie for selected language
|
||||
response.set_cookie(settings.LANGUAGE_COOKIE_NAME, selected_language)
|
||||
return response
|
||||
return render(request, self.template_name, {'form': form})
|
||||
|
||||
|
||||
class ServiceView(FormView):
|
||||
"""A generic view for configuring simple services."""
|
||||
clients = []
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user