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

Cannot use lazy gettext for fieldset names #488

Closed
alexkiro opened this issue Jul 12, 2022 · 0 comments
Closed

Cannot use lazy gettext for fieldset names #488

alexkiro opened this issue Jul 12, 2022 · 0 comments

Comments

@alexkiro
Copy link
Contributor

Describe the problem

Currently in order to configure fieldsets the CONSTANCE_CONFIG_FIELDSETS setting must be set to a dictionary that maps the header name as the key to the fieldset options as the value. Example from the docs:

CONSTANCE_CONFIG_FIELDSETS = {
    'General Options': ('SITE_NAME', 'SITE_DESCRIPTION'),
    'Theme Options': ('THEME',),
}

This causes an issue when attempting to use gettext_lazy for the header names. This is because the header name is the key for the dictionary, so Python will attempt to hash the value for the key. The Django proxy objects will in turn attempt to cast itself to a string in order to compute the hash. (as seen here)

This causes an issue, because the translation is attempted immediately, however the apps are not yet loaded as Django is still processing the settings file, so it will raise this error:

django.core.exceptions.AppRegistryNotReady: The translation infrastructure cannot be initialized before the apps registry is ready. Check that you don't make non-lazy gettext calls at import time.

Steps to reproduce

  • install django-constance into a django app
  • add to the settings file, something like:
from django.utils.translation import gettext_lazy

CONSTANCE_CONFIG = {
    "SITE_NAME": ("My Title", gettext_lazy("Website title")),
    "SITE_DESCRIPTION": ("", gettext_lazy("Website description")),
    "THEME": ("light-blue", gettext_lazy("Website theme")),
}

CONSTANCE_CONFIG_FIELDSETS = {
    gettext_lazy("General Options"): ("SITE_NAME", "SITE_DESCRIPTION"),
    gettext_lazy("Theme Options"): ("THEME",),
}

System configuration

  • Django version: 3.2.14
  • Python version: 3.8.10
  • Django-Constance version: 2.9.0

Notes

As far as I can tell, there is no workaround possible, so header names cannot be translated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants