Skip to content

Commit 1bc79e9

Browse files
MartinCastroAlvarezmartin-castro-laminr-aiclaude
authored
docs(readme): LocaleMiddleware section for translated payload fields (#630) (#648)
Documents the requirement for ``django.middleware.locale.LocaleMiddleware`` in the consumer's MIDDLEWARE stack to make the API's ``gettext_lazy``-wrapped ``verbose_name`` / ``help_text`` / ``@admin.action(description=…)`` proxies resolve to the active request locale. The API package itself has no ``activate()`` call — by design, it piggybacks on Django's standard ``LocaleMiddleware.process_request`` (which calls ``translation.activate(language)`` after ``get_language_from_request``). With the middleware enabled, the JSON payload returns ``verbose_name`` etc. in the user's language; without it, the proxies stay un-resolved and read as English regardless of ``Accept-Language``. Django's ``startproject`` template does NOT include ``LocaleMiddleware`` by default, so a stock consumer who adds this package gets the English-only behaviour and a confusing gap between Django's own chrome (which IS translated when ``LocaleMiddleware`` is present in other paths) and the SPA's payloads. Calling this out in the README + showing the exact ``MIDDLEWARE`` line is the smallest doc change that closes the "why aren't my translated verbose_names showing up?" question. Half of #630 — the wire-side gap — is now documented. The SPA's own chrome strings (``"Add"`` / ``"Search"`` / ``"Loading…"``) are still hard-coded English; the message-catalog work is staged for a separate session. Co-authored-by: Martin Castro Laminrs <mcastro@laminr.ai> Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent f8f6fda commit 1bc79e9

1 file changed

Lines changed: 46 additions & 0 deletions

File tree

README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,52 @@ The SPA's HTTP client already sends `credentials: "include"`, so no
750750
frontend change is needed — only the Django-side cookie + CORS
751751
config above. Tracked: [#635](https://github.com/MartinCastroAlvarez/django-admin-react/issues/635).
752752

753+
### Translated `verbose_name` / `help_text` / action descriptions (`LocaleMiddleware`)
754+
755+
The API package surfaces whatever your `ModelAdmin` declares —
756+
including `gettext_lazy`-wrapped strings on `verbose_name`,
757+
`help_text`, `@admin.action(description=…)`, etc. For those proxies
758+
to resolve to the active request's language, you need Django's
759+
**`LocaleMiddleware`** in your stack. It's not enabled by default
760+
in `django-admin startproject`, and the package has no
761+
ModelAdmin-level workaround:
762+
763+
```python
764+
# settings.py
765+
USE_I18N = True
766+
LANGUAGE_CODE = "en-us" # or your default
767+
LANGUAGES = [ # the locales your translations cover
768+
("en", "English"),
769+
("es", "Español"),
770+
#
771+
]
772+
773+
MIDDLEWARE = [
774+
"django.middleware.security.SecurityMiddleware",
775+
"django.contrib.sessions.middleware.SessionMiddleware",
776+
"django.middleware.locale.LocaleMiddleware", # ← REQUIRED for i18n
777+
"django.middleware.common.CommonMiddleware",
778+
"django.middleware.csrf.CsrfViewMiddleware",
779+
"django.contrib.auth.middleware.AuthenticationMiddleware",
780+
"django.contrib.messages.middleware.MessageMiddleware",
781+
"django.middleware.clickjacking.XFrameOptionsMiddleware",
782+
]
783+
```
784+
785+
With `LocaleMiddleware` in place, the API payload's
786+
`verbose_name` / `help_text` / `description` strings come back
787+
translated per the request's `Accept-Language` header (or the
788+
user's stored preference if you wire one), the same as Django's
789+
HTML admin. The wire shape is identical regardless of locale —
790+
only the human-readable strings change.
791+
792+
**The SPA's own chrome strings** ("Add", "Search", "Save and
793+
continue editing", "Loading…") are still hard-coded English. A
794+
message-catalog refresh + `config.language` wire field is tracked
795+
in [#630](https://github.com/MartinCastroAlvarez/django-admin-react/issues/630). Until that lands, non-English-primary shops
796+
get translated `verbose_name` / `help_text` (via `LocaleMiddleware`)
797+
but English chrome around them.
798+
753799
---
754800

755801
## The API surface

0 commit comments

Comments
 (0)