Accept-Language HTTP header and makes it available throughout the request lifecycle.
Configuration
Set up localization viaLocalizationConfiguration when bootstrapping your application. Pass it as the last argument to ApivalkConfiguration.
LocalizationConfiguration is provided, Apivalk defaults to English (en).
The Locale Object
TheLocale class is a BCP 47 compliant value object. It normalizes locale tags automatically (e.g., de_DE becomes de-DE).
Available Factory Methods
| Method | Tag |
|---|---|
Locale::en() | en |
Locale::de() | de |
Locale::fr() | fr |
Locale::enUs() | en-US |
Locale::enGb() | en-GB |
Locale::deDe() | de-DE |
Locale::deAt() | de-AT |
Locale::deCh() | de-CH |
new Locale('ja-JP').
Locale Resolution
TheLocaleResolver automatically resolves the locale from the Accept-Language HTTP header. The resolution follows this order:
- Parse the
Accept-Languageheader and sort entries by quality weight (qvalue), then by order of appearance. - For each entry, attempt to match against supported locales:
- First try an exact match (e.g.,
de-DEmatchesde-DE). - Then try a language-only fallback (e.g.,
de-DEmatchesde).
- First try an exact match (e.g.,
- If no match is found, fall back to the default locale.
Example
Given supported localesen, de, de-DE and default en:
de-DE (exact match on the first entry).
en (no match found, falls back to default).
Accessing the Locale in Controllers
The resolved locale is available on the request object:HTTP Headers
Request: Accept-Language
Clients send their preferred language(s) via the standardAccept-Language header using BCP 47 language tags:
Response: Content-Language
Apivalk returns the resolved locale via theContent-Language response header:
OpenAPI Documentation
The OpenAPI Generator automatically documents locale headers on every operation:- Request: An optional
Accept-Languageheader parameter (BCP 47 language tag). - Response: A
Content-Languageheader on every response, indicating the resolved locale.
false for the $documentLocaleHeaders parameter:
What to Localize
Localize human-readable message strings only:messagefields in success and error responses- Validation error messages
- Human-readable descriptions
- Field names or JSON keys
- Error codes or error keys
- Enum values
- IDs, UUIDs, or technical identifiers