Lesson 3 of 9 · 23m
Locale Negotiation, URLs, and User Preference
Choose a language deliberately using explicit preference, URL state, cookies, and Accept-Language without creating contradictory locale signals.
Locale negotiation is where good intentions become inconsistent product behavior. The marketing site uses /fr/, the app uses a cookie, the emails use account settings, and the checkout page guesses from IP. The user experiences four opinions of their language in one session. You need a precedence model and one canonical place to resolve it.
Establish precedence before writing code
A common order is:
1. explicit user preference in the account
2. explicit locale in the URL or current navigation state
3. remembered preference in a cookie or local storage
4. Accept-Language from the browser
5. product defaultThe exact order depends on the product. Public content often prioritizes the URL because links must be shareable. Authenticated apps often prioritize account preference. The mistake is allowing every subsystem to choose differently.
Accept-Language is a weighted hint
Browsers send a header such as:
Accept-Language: fr-CA,fr;q=0.9,en-US;q=0.7,en;q=0.6Parse quality values and match against supported locales with a library. fr-CA may match a fr catalog. en-US should not beat a higher-quality fr-CA merely because English is first in your internal list. This is negotiation, not substring search.
URLs make locale shareable and crawlable
For public pages, locale in the URL is usually the honest choice:
/en/docs/getting-started
/fr/docs/getting-started
/pt-BR/docs/getting-startedWhen equivalents exist, link them with hreflang metadata such as hreflang="fr" and an x-default URL. Do not redirect users away from a shared language URL merely because their browser header disagrees; offer a switcher instead.
Make switching intentional and lossless
A language switcher should preserve the current route and meaningful state. If I am on /fr/docs/incident-response and choose English, I expect the English version of that page, not the English home page. Query parameters such as ?next= and fragments need care, especially when locale is part of the path.
Avoid mixed negotiation inside one render
Once the request resolves pt-BR, formatting, message lookup, direction, and localized routing should use that decision consistently. A hidden second lookup based on browser headers can produce Portuguese labels with English dates or vice versa. In server rendering, pass the resolved locale down explicitly instead of rereading ambient state.
Field notes
- Never use a cookie as the only signal for public localized content; shared links then render differently for each recipient.
- Language negotiation can change cache keys. If responses depend on
Accept-Language, setVary: Accept-Languageor use locale-specific URLs. - Unsupported regional tags need graceful matching, but do not treat every fallback as equivalent quality.
- Log the final resolved locale in server traces. Debugging becomes miserable when every layer claims a different answer.
Was this lesson helpful?