diff --git a/bugsink/settings/default.py b/bugsink/settings/default.py index ffd3b52..91b3905 100644 --- a/bugsink/settings/default.py +++ b/bugsink/settings/default.py @@ -250,7 +250,7 @@ USE_L10N = True LOCALE_PATHS = [BASE_DIR / "locale"] LANGUAGES = ( ("en", "English"), - ("zh-Hans", "简体中文"), + ("zh-hans", "简体中文"), ) USE_TZ = True diff --git a/users/forms.py b/users/forms.py index f105477..740ea80 100644 --- a/users/forms.py +++ b/users/forms.py @@ -9,7 +9,9 @@ from django.core.exceptions import ValidationError from django.contrib.auth import password_validation from django.forms import ModelForm from django.utils.html import escape, mark_safe -from django.utils.translation import gettext_lazy as _ +from django.utils.translation import gettext_lazy as _, get_language_info +from django.conf import settings + TRUE_FALSE_CHOICES = ( (True, _("Yes")), @@ -131,6 +133,18 @@ class SetPasswordForm(BaseSetPasswordForm): self.fields['new_password2'].help_text = None # "Confirm password" is descriptive enough +def language_choices(): + items = [("auto", _("Auto (browser preference)"))] + + for code, _label in settings.LANGUAGES: + info = get_language_info(code) + label = info["name_local"] \ + if info["name_local"] == info["name_translated"] \ + else f"{info['name_local']} ({info['name_translated']})" + items.append((code, label)) + return items + + class PreferencesForm(ModelForm): # I haven't gotten a decent display for checkboxes in forms yet; the quickest hack around this is a ChoiceField send_email_alerts = forms.ChoiceField( @@ -143,7 +157,7 @@ class PreferencesForm(ModelForm): ) language = forms.ChoiceField( label=_("Language"), - choices=User.LANGUAGE_CHOICES, + choices=language_choices, required=True, widget=forms.Select(), ) diff --git a/users/migrations/0003_user_language.py b/users/migrations/0003_user_language.py index d1694b1..9952650 100644 --- a/users/migrations/0003_user_language.py +++ b/users/migrations/0003_user_language.py @@ -1,18 +1,16 @@ -# Generated by Django 4.2.23 on 2025-07-28 09:27 - from django.db import migrations, models class Migration(migrations.Migration): dependencies = [ - ('users', '0002_user_theme_preference'), + ("users", "0002_user_theme_preference"), ] operations = [ migrations.AddField( - model_name='user', - name='language', - field=models.CharField(choices=[('auto', 'Auto'), ('en', 'English'), ('zh-Hans', 'Simplified Chinese')], default='auto', max_length=10), + model_name="user", + name="language", + field=models.CharField(default="auto", max_length=10), ), ] diff --git a/users/models.py b/users/models.py index e156d90..88afc8b 100644 --- a/users/models.py +++ b/users/models.py @@ -28,15 +28,11 @@ class User(AbstractUser): default="system", blank=False, ) - - LANGUAGE_CHOICES = [ - ("auto", _("Auto")), - ("en", _("English")), - ("zh-Hans", _("Simplified Chinese")), - ] language = models.CharField( max_length=10, - choices=LANGUAGE_CHOICES, + # choices intentionally not set, we don't want changes to trigger migrations; the actual choices are set in + # forms.py; in Django 5.0 and up we can instead used a callable here + # choices=... default="auto", blank=False, )