Store and retrieve language in user Setting.

This commit is contained in:
Christopher C. Wells 2019-04-16 21:41:55 -07:00
parent d590bf8655
commit 83b4edf95d
5 changed files with 38 additions and 18 deletions

View File

@ -41,4 +41,4 @@ class UserPasswordForm(PasswordChangeForm):
class UserSettingsForm(forms.ModelForm):
class Meta:
model = Settings
fields = ['dashboard_refresh_rate']
fields = ['dashboard_refresh_rate', 'language']

View File

@ -0,0 +1,18 @@
# Generated by Django 2.2 on 2019-04-17 04:02
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('babybuddy', '0003_add_refresh_help_text'),
]
operations = [
migrations.AddField(
model_name='settings',
name='language',
field=models.CharField(choices=[], default='en', max_length=255, verbose_name='Language'),
),
]

View File

@ -1,11 +1,13 @@
# -*- coding: utf-8 -*-
from django.conf import settings
from django.contrib.auth.models import User
from django.contrib.auth.signals import user_logged_in
from django.db import models
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.utils.timezone import timedelta
from django.utils.text import format_lazy
from django.utils.translation import gettext_lazy as _
from django.utils.translation import activate, gettext_lazy as _, LANGUAGE_SESSION_KEY
from rest_framework.authtoken.models import Token
@ -30,6 +32,12 @@ class Settings(models.Model):
(timedelta(minutes=15), _('15 min.')),
(timedelta(minutes=30), _('30 min.')),
])
language = models.CharField(
choices=settings.LANGUAGES,
default=settings.LANGUAGE_CODE,
max_length=255,
verbose_name=_('Language')
)
def __str__(self):
return str(format_lazy(_('{user}\'s Settings'), user=self.user))
@ -65,3 +73,9 @@ def create_user_settings(sender, instance, created, **kwargs):
@receiver(post_save, sender=User)
def save_user_settings(sender, instance, **kwargs):
instance.settings.save()
@receiver(user_logged_in)
def user_logged_in_callback(sender, request, user, **kwargs):
activate(user.settings.language)
request.session[LANGUAGE_SESSION_KEY] = user.settings.language

View File

@ -110,7 +110,7 @@ LOGOUT_REDIRECT_URL = '/login/'
# Internationalization
# https://docs.djangoproject.com/en/1.11/topics/i18n/
LANGUAGE_CODE = 'en-us'
LANGUAGE_CODE = 'en'
TIME_ZONE = os.environ.get('TIME_ZONE', 'Etc/UTC')

View File

@ -42,21 +42,9 @@
{% endwith %}
</div>
<div class="form-group row">
<label for="id_language" class="col-sm-2 col-form-label">
{% trans "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>
{% with form_settings.language as field %}
{% include 'babybuddy/form_field.html' %}
{% endwith %}
</div>
</fieldset>
<fieldset>