Skip to content

Commit 326b54b

Browse files
committed
Closes #14579: Add user language preference
1 parent 3905ddf commit 326b54b

File tree

4 files changed

+35
-7
lines changed

4 files changed

+35
-7
lines changed

netbox/account/views.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from django.urls import reverse
1414
from django.utils.decorators import method_decorator
1515
from django.utils.http import url_has_allowed_host_and_scheme, urlencode
16+
from django.utils.translation import gettext_lazy as _
1617
from django.views.decorators.debug import sensitive_post_parameters
1718
from django.views.generic import View
1819
from social_core.backends.utils import load_backends
@@ -193,8 +194,16 @@ def post(self, request):
193194
if form.is_valid():
194195
form.save()
195196

196-
messages.success(request, "Your preferences have been updated.")
197-
return redirect('account:preferences')
197+
messages.success(request, _("Your preferences have been updated."))
198+
response = redirect('account:preferences')
199+
200+
# Set/clear language cookie
201+
if language := form.cleaned_data['locale.language']:
202+
response.set_cookie(settings.LANGUAGE_COOKIE_NAME, language)
203+
else:
204+
response.delete_cookie(settings.LANGUAGE_COOKIE_NAME)
205+
206+
return response
198207

199208
return render(request, self.template_name, {
200209
'form': form,

netbox/netbox/preferences.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
from django.conf import settings
12
from django.utils.translation import gettext as _
3+
24
from netbox.registry import registry
35
from users.preferences import UserPreference
46
from utilities.paginator import EnhancedPaginator
@@ -16,11 +18,18 @@ def get_page_lengths():
1618
'ui.colormode': UserPreference(
1719
label=_('Color mode'),
1820
choices=(
19-
('light', 'Light'),
20-
('dark', 'Dark'),
21+
('light', _('Light')),
22+
('dark', _('Dark')),
2123
),
2224
default='light',
2325
),
26+
'locale.language': UserPreference(
27+
label=_('Language'),
28+
choices=(
29+
('', _('Auto')),
30+
*settings.LANGUAGES,
31+
)
32+
),
2433
'pagination.per_page': UserPreference(
2534
label=_('Page length'),
2635
choices=get_page_lengths(),
@@ -30,9 +39,9 @@ def get_page_lengths():
3039
'pagination.placement': UserPreference(
3140
label=_('Paginator placement'),
3241
choices=(
33-
('bottom', 'Bottom'),
34-
('top', 'Top'),
35-
('both', 'Both'),
42+
('bottom', _('Bottom')),
43+
('top', _('Top')),
44+
('both', _('Both')),
3645
),
3746
description=_('Where the paginator controls will be displayed relative to a table'),
3847
default='bottom'

netbox/netbox/settings.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from django.core.exceptions import ImproperlyConfigured, ValidationError
1414
from django.core.validators import URLValidator
1515
from django.utils.encoding import force_str
16+
from django.utils.translation import gettext_lazy as _
1617
try:
1718
import sentry_sdk
1819
except ModuleNotFoundError:
@@ -721,6 +722,14 @@ def _setting(name, default=None):
721722
# Localization
722723
#
723724

725+
LANGUAGES = (
726+
('en', _('English')),
727+
('es', _('Spanish')),
728+
('fr', _('French')),
729+
('pt', _('Portuguese')),
730+
('ru', _('Russian')),
731+
)
732+
724733
LOCALE_PATHS = (
725734
BASE_DIR + '/translations',
726735
)

netbox/users/forms/model_forms.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ def __new__(mcs, name, bases, attrs):
5656
class UserConfigForm(BootstrapMixin, forms.ModelForm, metaclass=UserConfigFormMetaclass):
5757
fieldsets = (
5858
(_('User Interface'), (
59+
'locale.language',
5960
'pagination.per_page',
6061
'pagination.placement',
6162
'ui.colormode',

0 commit comments

Comments
 (0)