Skip to main content

Module locale

Module locale 

Source
Expand description

Locale negotiation middleware - the session-reading half of larust_lang’s own current-locale story (see that crate’s doc comment for the full design: translation files, t/t_with, the task-local scope this wraps). Opt-in, like require_auth/csrf::verify - unlike push_registry_scope, larust_lang::current_locale() already degrades correctly with no scope established at all (falls back to Config::app_locale), so there’s no “always on, unconditional” case to make for this the way there was for @push/@stack.

Constants§

SESSION_KEY
The session key LanguageController-style app code should session.insert("locale", ...) into - matches whatever a real app’s own language-switcher route already sets, so wiring this middleware in doesn’t require renaming an existing session key.

Functions§

negotiate
Establishes a fresh larust_lang locale scope for this request, then - if the session already has a stored "locale" value - overrides it for the rest of the request. Add after .with_sessions(...), the same ordering constraint csrf::verify/require_auth already have:
negotiate_from_header
negotiate’s session-free sibling, for routers with no cookie session at all - a bearer-token API (routes/api.rs’s own shape) has nowhere to persist a per-user locale choice, but a caller can still say what they want on each request via the standard Accept-Language header, so this reads that instead of a session key. Same task-local scope, same “degrade to Config::app_locale if unrecognized” fallback as negotiate - just a different source for the raw locale string.