Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closes #14579: Add user language preference #14580

Merged
merged 1 commit into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions netbox/account/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from django.urls import reverse
from django.utils.decorators import method_decorator
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.generic import View
from social_core.backends.utils import load_backends
Expand Down Expand Up @@ -193,8 +194,16 @@ def post(self, request):
if form.is_valid():
form.save()

messages.success(request, "Your preferences have been updated.")
return redirect('account:preferences')
messages.success(request, _("Your preferences have been updated."))
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, {
'form': form,
Expand Down
19 changes: 14 additions & 5 deletions netbox/netbox/preferences.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from django.conf import settings
from django.utils.translation import gettext as _

from netbox.registry import registry
from users.preferences import UserPreference
from utilities.paginator import EnhancedPaginator
Expand All @@ -16,11 +18,18 @@ def get_page_lengths():
'ui.colormode': UserPreference(
label=_('Color mode'),
choices=(
('light', 'Light'),
('dark', 'Dark'),
('light', _('Light')),
('dark', _('Dark')),
),
default='light',
),
'locale.language': UserPreference(
label=_('Language'),
choices=(
('', _('Auto')),
*settings.LANGUAGES,
)
),
'pagination.per_page': UserPreference(
label=_('Page length'),
choices=get_page_lengths(),
Expand All @@ -30,9 +39,9 @@ def get_page_lengths():
'pagination.placement': UserPreference(
label=_('Paginator placement'),
choices=(
('bottom', 'Bottom'),
('top', 'Top'),
('both', 'Both'),
('bottom', _('Bottom')),
('top', _('Top')),
('both', _('Both')),
),
description=_('Where the paginator controls will be displayed relative to a table'),
default='bottom'
Expand Down
9 changes: 9 additions & 0 deletions netbox/netbox/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from django.core.exceptions import ImproperlyConfigured, ValidationError
from django.core.validators import URLValidator
from django.utils.encoding import force_str
from django.utils.translation import gettext_lazy as _
try:
import sentry_sdk
except ModuleNotFoundError:
Expand Down Expand Up @@ -721,6 +722,14 @@ def _setting(name, default=None):
# Localization
#

LANGUAGES = (
('en', _('English')),
('es', _('Spanish')),
('fr', _('French')),
('pt', _('Portuguese')),
('ru', _('Russian')),
)

LOCALE_PATHS = (
BASE_DIR + '/translations',
)
Expand Down
1 change: 1 addition & 0 deletions netbox/users/forms/model_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ def __new__(mcs, name, bases, attrs):
class UserConfigForm(BootstrapMixin, forms.ModelForm, metaclass=UserConfigFormMetaclass):
fieldsets = (
(_('User Interface'), (
'locale.language',
'pagination.per_page',
'pagination.placement',
'ui.colormode',
Expand Down