Files
ServerKit/backend/migrations/versions/089_user_language.py
T
Juan DenisandClaude Opus 5 b851f48ef0 feat(i18n): locale runtime, precedence, and a per-user language column
Plan 79 A1/A2/A4 + B1/B2.

Delivery deliberately avoids the usual http-backend fetch. `en` is bundled;
other locales are dynamic imports Vite emits as chunks from the app's own
static origin. The sign-in and setup screens render before any API call
succeeds, so a fetched locale either flashes English or -- when the backend
is the thing that is broken -- never arrives, on the one screen whose job is
to say so. It also keeps air-gapped and subpath installs working with no
second path to 404.

Precedence (user row -> localStorage -> panel default -> navigator -> en)
mirrors ThemeContext's, including the split provider: LocaleProvider sits
outermost so a toast raised during theme init is already translatable, and
LocaleSync bridges auth state down, exactly as ThemeSync does.

users.language is nullable on purpose -- "follow the panel default" is a
different state from an explicit choice of English, so existing rows are not
backfilled. The backend restates the supported list to validate writes; a
parity test fails the build if it drifts from the frontend manifest, which
stays canonical.

Two bugs the tests caught, both real rather than test artifacts:

- normalize_language('ES') returned None. The shape gate was case-sensitive,
  so two of the three forms browsers actually send were rejected.
- PUT /me is throttled 3/minute for credential edits, and language rides the
  same route. Switching language three times returned 429 -- and because the
  client applies the change locally first, the preference silently failed to
  persist and reverted on the next sign-in elsewhere, defeating the column.
  Cosmetic-only payloads are now exempt from that limit; credential edits
  stay throttled (proved both ways) and the app-wide 100/minute still holds.

eslint moves to ecmaVersion 'latest' because import attributes are ES2025 and
Node's test runner requires them for the manifest imports.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-19 23:16:09 -04:00

35 lines
966 B
Python

"""Per-user UI language preference: `users.language`.
Plan 79 B2. Nullable on purpose — NULL means "follow the panel default"
(`default_language` in system settings), which is a different state from an
explicit choice of English, so existing rows are left alone rather than
backfilled to 'en'.
Revision ID: 089_user_language
Revises: 088_soft_delete_managed_databases
"""
import sqlalchemy as sa
from alembic import op
revision = '089_user_language'
down_revision = '088_soft_delete_managed_databases'
branch_labels = None
depends_on = None
def _has_column(table, column):
bind = op.get_bind()
return column in {col['name'] for col in sa.inspect(bind).get_columns(table)}
def upgrade():
if _has_column('users', 'language'):
return
op.add_column('users', sa.Column('language', sa.String(length=10), nullable=True))
def downgrade():
if not _has_column('users', 'language'):
return
op.drop_column('users', 'language')