Locale Detection and the Language Switcher UX

Locale Detection and the Language Switcher UX

How a multilingual site should detect and switch locale — why forced IP redirect is an anti-pattern, signal reliability, country vs language, and switcher UX that does not trap.

Locale detection should suggest, never force. Auto-redirecting visitors by IP address traps travelers and expatriates in the wrong locale, and Google warns that such redirects could prevent search engines from viewing all the versions of a site; the durable pattern is to infer a suggestion from signals, let an explicit user choice override and persist it, and resolve all of this at the edge without ever blocking a crawler. Whether the selector is language-only or country-and-language is a product decision — commerce and legal sites need both axes and must not infer one from the other.

The forced-redirect trap

One locale mistake looks like a feature: detect the visitor's country from their IP address and redirect them to that market's site. It feels helpful, but it is a damaging pattern in multilingual UX.

It fails for everyone whose physical location is not their language. An expatriate working abroad is redirected to the host country's site and cannot reach the one in their own language. A consultant traveling through a third country is bounced to that country's locale mid-task and loses the page they had open. A user who deliberately shared a link to the English documentation watches the recipient get silently redirected to a translated version they did not ask for. None of these users can escape, because the redirect fires again on every visit, overriding whatever they choose.

It also damages search. Google's guidance on managing multi-regional sites advises against using IP analysis to adapt content, because Google "may not be able to crawl variations of your site properly": most, but not all, Google crawls originate from the US, and Google does not attempt to vary the location to detect site variations. A hard IP redirect can therefore send most of Google's crawls to the one variant served to US visitors, and the same guidance warns that automatic redirects between language versions "could prevent users (and search engines) from viewing all the versions of your site." Google's locale-adaptive pages guidance adds that when a site returns different content based on the visitor's perceived country or preferred language, Google "might not crawl, index, or rank all your content for different locales," and recommends separate locale URLs annotated with hreflang. The principle that resolves all of this is one sentence: detect to suggest, never to force. A signal can propose a locale; only an explicit user action may select one, and once selected it must persist and outrank every signal on every subsequent visit.

Why a team might ship this anti-pattern anyway is worth naming, because understanding the incentive is how a team avoids rebuilding it. It looks like a feature in a demo: open the site from the target country and it "just works" in the local language, which reads as polish to a stakeholder reviewing from one location. The cost lands on users the demo never includes — the expatriate, the traveler, the analyst comparing markets, the journalist following a shared link — and on crawlers, which no one watches during a UX review. One plausible reason the pattern survives review is that the room reviewing it is the one population it does not harm. Designing for the people not in the room is the whole discipline here, and the cheapest way to keep them in scope is to make "what does a logged-out visitor from the wrong country, on a shared deep link, experience?" a required test case rather than an afterthought.

Signals and their reliability

Three signals are available, and treating them as equally trustworthy is where teams go wrong.

The Accept-Language header reflects the language preferences the user set in their browser or operating system. It is the best first hint because it is an actual stated preference, but it is a hint, not law: it is often left at an operating-system default the user never changed, and it carries language tags, not a location: a tag may include a region subtag, as in fr-CA ("French as used in Canada"), but that names a variety of the language, not where the user is. The W3C's guidance on language priorities describes how to read it as an ordered preference list rather than a single value. IP geolocation gives only an estimate of where the request came from, and Google's guidance calls IP location analysis "difficult and generally not reliable." The signal does not reveal language, residency, or intent; a Gulf-based engineer may want English, a tourist abroad wants their home language, and a corporate VPN makes the whole signal arbitrary. Explicit user selection is the only authoritative signal, because it is the only one where the user told you what they want rather than something a heuristic inferred about them.

Accept-Language rewards being read properly rather than naively. It is a list of language ranges with quality weights, not a single value — a browser may send a primary language, a regional fallback, and a generic fallback with decreasing weights, as in da, en-gb;q=0.8, en;q=0.7. Reading only the first token throws away the fallback the user explicitly configured for exactly the case where the first is unsupported. The correct read ranks the entries by their quality values and selects the highest-weighted entry the site actually supports, falling back to the default only when none match, which is also how the header degrades gracefully for users who never changed an operating-system default in the first place. Rank by the weights, not by position alone: RFC 9110 notes that some recipients treat the order of the tags as descending priority, particularly for tags with equal quality values (a tag with no value counts as q=1), but that "this behavior cannot be relied upon," although many user agents assign each tag a unique quality value while also listing them in decreasing order.

The correct combination follows from that reliability order. Suggest from the available signals — propose a locale based on Accept-Language, optionally informed by IP for the country axis when the site genuinely needs a country — and then decide by explicit choice, remembering that choice permanently. The suggestion is allowed to be wrong because it is only a suggestion; the decision must be the user's and must survive the next visit. A useful intermediate pattern is the non-intrusive suggestion banner: render the locale the user actually requested, and surface a dismissible strip — "View this in [their likely language]?" — that offers the inferred locale as a one-click choice without ever performing it for them. That keeps the helpfulness of detection while removing the trap, because nothing changes until the user acts and the dismissal is itself a remembered preference.

Country-and-language vs language-only

A foundational product decision precedes any UX work: does the site need one axis or two? Some sites are language-only — documentation, a content site, a brand presence — where the only thing that varies is the language of the words. Others are inherently two-axis: a commerce or regulated site, where the country determines applicable law, available payment methods, tax treatment, shipping, and product availability, while the language determines only the words. For those sites a German-speaking buyer in Switzerland and a German-speaking buyer in Germany are different locales with different prices and different legal pages even though the language is identical.

The hard rule is that you must not infer one axis from the other. Language does not imply country — German is spoken in Germany, Austria, and Switzerland — and country does not imply language — Switzerland alone has four. A site that picks the country from the language, or the language from the IP-derived country, will systematically misroute multilingual markets. When both axes matter, the selector is genuinely two-dimensional: choose a country, then a language available in it. When only language matters, a single-axis selector is correct and a two-axis one is needless friction. Deciding which the site is, before designing the control, is what keeps the control honest.

The cost of getting the axis count wrong is asymmetric, which is why the decision deserves its own moment. A two-axis site that ships a language-only selector silently serves every market the wrong legal pages, the wrong prices, and unavailable products to people it implied could buy them — a compliance and conversion failure that hides until someone in-market reports it. A language-only site that ships a two-axis selector merely adds a redundant step, which is recoverable. So when there is genuine doubt about whether country matters, the safer default is to treat it as a two-axis site and collapse the country axis later if it proves cosmetic, rather than discover after launch that a single axis quietly conflated two markets that needed different terms.

Switcher UX patterns

The switcher is the escape hatch that makes "suggest, never force" safe, so its usability is not cosmetic. Several patterns separate a switcher that works from one that traps.

It must be discoverable and persistent. A locale control hidden in the footer behind a grid of flags is effectively absent for the user who was suggested the wrong locale on a deep page. And the W3C advises against using flags to indicate languages in Indicating the language of a link destination: flags represent countries, not languages; numerous countries share a language with another country and numerous countries have more than one official language, so flags do not map well onto these permutations; and flags have nationalistic connotations that may be unwelcome for people of other countries, even though they speak the same language. The same W3C page notes that flags can be very appropriate for sites that are distinguished on the basis of region, but that you should normally avoid them for links to pages that are just translations. List languages in their own name — the endonym, "Deutsch" rather than "German", each label written in the language it names — because that is the label a speaker of that language will recognize at a glance.

The switch must be non-destructive. Changing locale should land the user on the equivalent page in the new locale, not dump them on the homepage, because sending someone from a deep article to the localized front door punishes the act of switching and is functionally another way of trapping them. The choice must be remembered — persisted in a cookie or local storage and honored across sessions — so the user states their preference once, not on every visit. And the control must be keyboard-operable and screen-reader-labeled like any other primary navigation, because a user who cannot operate the switcher is trapped exactly as surely as one who was redirected.

Implementation with edge middleware

Edge middleware is the natural place to resolve a locale because it runs before the page renders and can read request signals without a round trip. The pattern has a strict order of precedence.

code
function resolveLocale(request):
    rendered = localeFromPath(request.path) or DEFAULT_LOCALE   # the URL decides what renders

    stored = request.cookie["locale"]
    if stored and isSupported(stored):
        preferred = stored                 # explicit choice, read first, always
    else:
        accepted = parseAcceptLanguage(request.header["Accept-Language"])
        preferred = bestSupportedByQuality(accepted) or DEFAULT_LOCALE

    # never redirect a crawler or a deep link: render the requested locale
    # and offer the preferred one in a suggestion banner when it differs
    return { render: rendered, suggest: preferred if preferred != rendered else none }

The load-bearing line is the first one: the requested URL decides what renders, so each locale URL serves the same locale to every visitor, including Google's crawler, which Google documents as sending requests without an Accept-Language header. Signals decide only what to suggest, and a stored explicit preference is honored before any other signal is even read. Only when there is no stored choice does the middleware fall back to a suggestion derived from Accept-Language. The suggestion is offered, not redirected — the user lands on the content they requested, with the preferred locale offered in a banner and a visible, non-destructive switcher, rather than being bounced to a different URL.

code
# anti-pattern — do not ship this
if geoIP(request) != request.path.locale:
    return redirect(geoIPLocale + request.path)   # traps users and crawlers

That second block is the forced-redirect trap written as code; it is shown only so it can be recognized and removed. Two cross-cutting rules close the loop with the international-SEO architecture: the hreflang annotations for each locale URL, whether placed in the HTML, the HTTP headers, or a sitemap, must list that URL itself and every other language version — a requirement in Google's localized versions guidance — so Google can understand that these pages are localized variations of the same content, and the middleware must never redirect a crawler — the URL strategy and hreflang design that depend on this are the hreflang article's subject, and this implementation only has to guarantee it does not break them.

A locale-detection checklist

  • Detection only suggests; an explicit choice overrides every signal and persists across sessions
  • Accept-Language treated as a hint ranked by quality values, IP as a location estimate only, explicit choice as the sole authority
  • Axis decision made before UX — language-only or country-and-language, never inferring one from the other
  • Switcher discoverable and persistent, labeled with endonyms, never a footer flag grid
  • Switching is non-destructive — stays on the equivalent page, keyboard- and screen-reader-operable
  • Each locale URL renders its own locale; the stored preference is honored at the edge before any other signal when choosing what to suggest
  • No redirect path that can trap a crawler in a single geo-locked locale
Project Inquiry