Closes #14579: Add user language preference

This commit is contained in:
Jeremy Stretch 2023-12-21 12:11:30 -05:00
parent 3905ddf163
commit 326b54b7e0
4 changed files with 35 additions and 7 deletions

View File

@ -13,6 +13,7 @@ from django.shortcuts import render, resolve_url
from django.urls import reverse from django.urls import reverse
from django.utils.decorators import method_decorator from django.utils.decorators import method_decorator
from django.utils.http import url_has_allowed_host_and_scheme, urlencode from django.utils.http import url_has_allowed_host_and_scheme, urlencode
from django.utils.translation import gettext_lazy as _
from django.views.decorators.debug import sensitive_post_parameters from django.views.decorators.debug import sensitive_post_parameters
from django.views.generic import View from django.views.generic import View
from social_core.backends.utils import load_backends from social_core.backends.utils import load_backends
@ -193,8 +194,16 @@ class UserConfigView(LoginRequiredMixin, View):
if form.is_valid(): if form.is_valid():
form.save() form.save()
messages.success(request, "Your preferences have been updated.") messages.success(request, _("Your preferences have been updated."))
return redirect('account:preferences') response = redirect('account:preferences')
# Set/clear language cookie
if language := form.cleaned_data['locale.language']:
response.set_cookie(settings.LANGUAGE_COOKIE_NAME, language)
else:
response.delete_cookie(settings.LANGUAGE_COOKIE_NAME)
return response
return render(request, self.template_name, { return render(request, self.template_name, {
'form': form, 'form': form,

View File

@ -1,4 +1,6 @@
from django.conf import settings
from django.utils.translation import gettext as _ from django.utils.translation import gettext as _
from netbox.registry import registry from netbox.registry import registry
from users.preferences import UserPreference from users.preferences import UserPreference
from utilities.paginator import EnhancedPaginator from utilities.paginator import EnhancedPaginator
@ -16,11 +18,18 @@ PREFERENCES = {
'ui.colormode': UserPreference( 'ui.colormode': UserPreference(
label=_('Color mode'), label=_('Color mode'),
choices=( choices=(
('light', 'Light'), ('light', _('Light')),
('dark', 'Dark'), ('dark', _('Dark')),
), ),
default='light', default='light',
), ),
'locale.language': UserPreference(
label=_('Language'),
choices=(
('', _('Auto')),
*settings.LANGUAGES,
)
),
'pagination.per_page': UserPreference( 'pagination.per_page': UserPreference(
label=_('Page length'), label=_('Page length'),
choices=get_page_lengths(), choices=get_page_lengths(),
@ -30,9 +39,9 @@ PREFERENCES = {
'pagination.placement': UserPreference( 'pagination.placement': UserPreference(
label=_('Paginator placement'), label=_('Paginator placement'),
choices=( choices=(
('bottom', 'Bottom'), ('bottom', _('Bottom')),
('top', 'Top'), ('top', _('Top')),
('both', 'Both'), ('both', _('Both')),
), ),
description=_('Where the paginator controls will be displayed relative to a table'), description=_('Where the paginator controls will be displayed relative to a table'),
default='bottom' default='bottom'

View File

@ -13,6 +13,7 @@ from django.contrib.messages import constants as messages
from django.core.exceptions import ImproperlyConfigured, ValidationError from django.core.exceptions import ImproperlyConfigured, ValidationError
from django.core.validators import URLValidator from django.core.validators import URLValidator
from django.utils.encoding import force_str from django.utils.encoding import force_str
from django.utils.translation import gettext_lazy as _
try: try:
import sentry_sdk import sentry_sdk
except ModuleNotFoundError: except ModuleNotFoundError:
@ -721,6 +722,14 @@ RQ_QUEUES.update({
# Localization # Localization
# #
LANGUAGES = (
('en', _('English')),
('es', _('Spanish')),
('fr', _('French')),
('pt', _('Portuguese')),
('ru', _('Russian')),
)
LOCALE_PATHS = ( LOCALE_PATHS = (
BASE_DIR + '/translations', BASE_DIR + '/translations',
) )

View File

@ -56,6 +56,7 @@ class UserConfigFormMetaclass(forms.models.ModelFormMetaclass):
class UserConfigForm(BootstrapMixin, forms.ModelForm, metaclass=UserConfigFormMetaclass): class UserConfigForm(BootstrapMixin, forms.ModelForm, metaclass=UserConfigFormMetaclass):
fieldsets = ( fieldsets = (
(_('User Interface'), ( (_('User Interface'), (
'locale.language',
'pagination.per_page', 'pagination.per_page',
'pagination.placement', 'pagination.placement',
'ui.colormode', 'ui.colormode',