i18n Server Surface
What it is. The HTTP routes, the single locale resolver and the localized error shape, all in botcore/src/i18n/ and mounted once from the server route merge.
Open it. Call the routes on the API service, or read the i18n API reference.
Do this. Fetch /api/i18n/locales to populate a picker, /api/i18n/:locale to load a dictionary, and /api/i18n/preference only for a signed-in person.
Limits. There is no ?locale= query parameter; resolution reads headers and the stored preference only.
There is one Locale type — botlib::i18n::Locale, re-exported by botcore::i18n. The available set comes from the loaded catalogue; BUNDLED_LOCALES is only a pre-initialisation fallback.
Endpoints
GET /api/i18n/locales
No authentication. Returns the shipped codes, the default and presentation metadata.
| Field | Meaning |
|---|---|
code |
BCP-47 code, identical to the catalogue directory name |
native_name |
The language's own name, so the picker is readable in every locale |
dir |
ltr or rtl |
date_format, number_format |
CLDR patterns for Intl formatters |
A locale directory added without metadata still appears, using its code as the native name and English formats.
GET /api/i18n/:locale
Returns { "locale": "...", "translations": { ... } }. An unparsable code falls back to the default rather than erroring. The dictionary is the union of the explicit Rust key list and botlib::i18n::message_ids() — every key authored in the English catalogue.
Values are rendered without arguments, so plural blocks are stripped, not selected. Apply arguments in the browser (data-i18n-params) or with translate on the server.
GET /api/i18n/preference
Requires a bearer token; the user id comes from token claims. Without one the response is 401 with the localized body for error-auth-token-missing.
The value lives in user_preferences under preference_key = 'locale', not in a users.locale column. Background work uses locale_for_user(conn, user_id) instead of the process locale.
POST /api/i18n/preference
Body: { "locale": "pt-BR" }.
| Case | Response |
|---|---|
| Saved | 200 with the locale |
| No or invalid token | 401 error-auth-token-missing |
| Unparsable locale | 400 error-locale-invalid |
| Not shipped | 400 error-locale-unsupported plus available |
| Database failure | 500 error-locale-persist-failed |
The write is an upsert on (user_id, preference_key), so a person has one row per key. Only shipped locales are accepted.
The locale resolver
botcore::i18n::locale_from_headers decides a request's language:
X-Locale— explicit pin, honoured only when the catalogue ships a match; unsupported values are ignored.Accept-Language— parsed into(language, q)pairs, sorted byq, withq=0dropped; exact language+region, then language-only.en— fallback.
RequestLocale is the axum extractor wrapping the resolver; its rejection is Infallible, so locale resolution never fails a request. A ?locale= query parameter is not implemented.
Resolution order for a person (not a request)
| Context | Signal |
|---|---|
| Browser request | X-Locale, then Accept-Language, then en |
| Authenticated handler | locale_for_user, then the header resolver |
| Email or scheduled job | Recipient's user_preferences, then en |
| WhatsApp, Telegram, Teams | Bot's locale config via locale_from_config, then en |
| CLI text | --lang, then LC_ALL, LC_MESSAGES, LANG, then en |
The LocalizedError contract
Every user-facing error has a stable code, localized message, the same text in error for older clients, and optional structured details. Constructors include new, with_args, not_found, validation, internal, unauthorized, forbidden and rate_limited.
Never return a raw e.to_string() to a client. Keep code stable across releases and translate only the human text; do not reshape machine-readable payloads.
Translating from Rust
use botcore::i18n::{t_args, Locale, translate};
let locale = Locale::new("pt-BR").unwrap_or_default();
let count = t_args(&locale, "admin-group-member-count", &[("count", "3")]);
translate is re-exported from botlib, so crates that cannot depend on botcore use the same catalogue and fallback chain. botserver calls i18n::init_i18n(default_locales_path()) once at boot; earlier call sites use ensure_initialized().
See also
- Localization (i18n) — catalogue and tooling
- i18n Client Runtime — what consumes these endpoints
- i18n API — the HTTP reference