Add cookie-based language selection to user settings.

This will likely be refactored to use a database column instead of dropping a cookie.
This commit is contained in:
Christopher C. Wells 2019-04-13 20:07:46 -07:00
parent 8f0c2f3159
commit 0e4d80dab0
3 changed files with 21 additions and 1 deletions

View File

@ -1,5 +1,5 @@
{% extends 'babybuddy/page.html' %} {% extends 'babybuddy/page.html' %}
{% load widget_tweaks %} {% load i18n widget_tweaks %}
{% block title %}User Settings{% endblock %} {% block title %}User Settings{% endblock %}
@ -41,6 +41,23 @@
{% include 'babybuddy/form_field.html' %} {% include 'babybuddy/form_field.html' %}
{% endwith %} {% endwith %}
</div> </div>
<div class="form-group row">
<label for="id_language" class="col-sm-2 col-form-label">
Language
</label>
<div class="col-sm-10">
<select name="language">
{% get_current_language as LANGUAGE_CODE %}
{% get_available_languages as LANGUAGES %}
{% get_language_info_list for LANGUAGES as languages %}
{% for language in languages %}
<option value="{{ language.code }}"{% if language.code == LANGUAGE_CODE %} selected{% endif %}>
{{ language.name_local }} ({{ language.code }})
</option>
{% endfor %}
</select>
</div>
</div>
</fieldset> </fieldset>
<fieldset> <fieldset>
<legend>Dashboard</legend> <legend>Dashboard</legend>

View File

@ -53,6 +53,7 @@ urlpatterns = [
path('admin/', admin.site.urls), path('admin/', admin.site.urls),
path('', include('api.urls', namespace='api')), path('', include('api.urls', namespace='api')),
path('', include((app_patterns, 'babybuddy'), namespace='babybuddy')), path('', include((app_patterns, 'babybuddy'), namespace='babybuddy')),
path('user/lang', include('django.conf.urls.i18n')),
path('', include('core.urls', namespace='core')), path('', include('core.urls', namespace='core')),
path('', include('dashboard.urls', namespace='dashboard')), path('', include('dashboard.urls', namespace='dashboard')),
path('', include('reports.urls', namespace='reports')), path('', include('reports.urls', namespace='reports')),

View File

@ -10,6 +10,7 @@ from django.urls import reverse, reverse_lazy
from django.views.generic import View from django.views.generic import View
from django.views.generic.base import TemplateView, RedirectView from django.views.generic.base import TemplateView, RedirectView
from django.views.generic.edit import CreateView, UpdateView, DeleteView from django.views.generic.edit import CreateView, UpdateView, DeleteView
from django.views.i18n import set_language
from django_filters.views import FilterView from django_filters.views import FilterView
@ -135,6 +136,7 @@ class UserSettings(LoginRequiredMixin, View):
user_settings = form_settings.save(commit=False) user_settings = form_settings.save(commit=False)
user.settings = user_settings user.settings = user_settings
user.save() user.save()
set_language(request)
messages.success(request, 'Settings saved!') messages.success(request, 'Settings saved!')
return redirect('babybuddy:user-settings') return redirect('babybuddy:user-settings')
return render(request, self.template_name, { return render(request, self.template_name, {