Skip to content

locale_selector doesn't see lang prefix after LangPrefixMiddleware strips it #1606

@davidpoblador

Description

@davidpoblador

Bug

locale_selector() in vibetuner/frontend/middleware.py re-parses the request path to detect a language prefix:

def locale_selector(conn: HTTPConnection) -> str | None:
    parts = conn.scope.get("path", "").strip("/").split("/")
    if parts and len(parts[0]) == 2 and parts[0].islower() and parts[0].isalpha():
        return parts[0]
    return None

But LangPrefixMiddleware has already stripped the prefix from scope["path"] and stored it in scope["state"]["lang_prefix"]. So by the time locale_selector runs inside LocaleMiddleware, the path is / and it returns None.

Expected

Visiting /ca/ should detect Catalan as the locale, persist it via AdjustLangCookieMiddleware, and make it sticky.

Actual

The language prefix is ignored for locale detection. The cookie never gets updated, so the language doesn't persist.

Suggested Fix

locale_selector should read from scope["state"]["lang_prefix"]:

def locale_selector(conn: HTTPConnection) -> str | None:
    return conn.scope.get("state", {}).get("lang_prefix")

Filed by Claude Code.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions