Locale Fallback: Negotiation and Missing Translations

Locale Fallback: Negotiation and Missing Translations

Every multilingual codebase has a fallback chain, designed or accidental. Negotiating locales, filling missing strings, and keeping lang attributes truthful.

Every multilingual codebase has a locale fallback chain, designed or accidental. This article separates the two fallback problems, which are choosing a supported locale for a request and filling a single missing string, and covers BCP 47 tags read precisely, script-aware negotiation, per-environment visibility for missing translations, coverage thresholds, and keeping the document's language metadata truthful when content falls back.

Every codebase has a fallback chain

The moment a site supports two locales, a fallback chain exists. The only question is whether someone designed it or it is whatever the i18n library did by default. The accidental version is recognizable from the outside: a Brazilian visitor lands on European Portuguese without anyone deciding that, a German page shows three English buttons nobody noticed, and when a support ticket asks why, nobody can say which behavior was intended.

There are two distinct fallback problems, and conflating them is the root of most confusion:

  • Locale-level negotiation: which supported locale serves this request? The user asked for pt-BR; the site has pt-PT and es. What happens?
  • String-level fallback: the request is served in German, but one key in the German catalog has no translation yet. What renders in its place?

They run at different times, they are configured in different places, and they fail in different ways. This article keeps them separate throughout.

The scope line: which signals suggest a locale to a first-time visitor, such as browser preferences or geography, belongs to the insight locale-detection literature. This article starts from a requested locale, typically parsed from the URL by the routing layer, and designs what happens when it cannot be fully honored.

Reading BCP 47 precisely

Fallback design starts with reading the tags correctly. A BCP 47 language tag, defined in RFC 5646, has three subtags that matter at working level:

SubtagExampleWhat it identifiesWhat it means for fallback
Language`pt`, `zh`, `sr`The languageThe anchor; fallback never crosses it silently
Script`Hant`, `Hans`, `Cyrl`, `Latn`The writing systemScript variants must never substitute for each other silently
Region`BR`, `PT`, `TW`, `CH`The regional varietyRegion variants share a language and can usually stand in for each other

The distinction between region and script is where fallback goes wrong most expensively. Unicode CLDR's language-matching data treats regional variants of one language as close matches: a Swiss German reader can be served by standard German, and a Mexican reader by a neutral Spanish catalog. Script variants are not interchangeable in the same way. Traditional and Simplified Chinese are different scripts, written zh-Hant and zh-Hans, not regional flavors of one text. A request for zh-TW that falls back to a catalog labeled zh which actually contains Simplified text serves a Taiwanese reader the wrong writing system. That is a correctness failure, not a style preference. Serbian in Cyrillic and Latin script is a softer case: the same CLDR data (CLDR 49) scores sr-Latn against sr-Cyrl as a close match, while Traditional and Simplified Chinese get the default script distance, so whether Serbian may cross scripts is a product decision to make explicitly. The sketch below keeps Serbian script-sensitive.

The fix is script-aware fallback. Before stripping subtags, expand the requested tag to its likely script using CLDR's likely-subtags data, described in UTS #35, Likely Subtags: zh-TW expands to zh-Hant-TW, zh-CN to zh-Hans-CN. Then fall back only within the same script, and label catalogs by script (zh-Hant, zh-Hans) rather than by a bare language code that hides which script is inside.

Two housekeeping rules prevent a recurring class of bugs. Normalize tags once, at the boundary, to canonical case and form: en-us, EN_US, and en-US should all become en-US before anything compares them. And keep the supported locales as an explicit, ordered list in code, the artifact negotiation runs against. A supported set inferred from whichever catalog files happen to exist in a folder means a half-finished catalog becomes "supported" the moment someone commits it.

Locale negotiation: request meets supported list

Negotiation matches an ordered request, the user's preference chain, against the supported list. The lookup-style matching described in RFC 4647 is the working model: for each requested tag in order, try an exact match, then progressively strip subtags from the right (a single-letter or digit subtag, such as the private-use x or an extension singleton, is removed together with the subtag that follows it) until something matches, and only when the whole request is exhausted fall to the designed default.

typescript
const SUPPORTED = ["en", "de", "fr", "pt-BR", "pt-PT", "zh-Hans", "zh-Hant"];
const DEFAULT = "en";
const SCRIPT_SENSITIVE = new Set(["zh", "sr"]); // bare tag hides which script is meant

// requested: already normalized and expanded with likely scripts,
// e.g. ["zh-Hant-TW", "en-GB"]
export function negotiate(requested: string[]): string {
  for (const tag of requested) {
    const parts = tag.split("-");
    while (parts.length > 0) {
      const candidate = parts.join("-");
      if (SUPPORTED.includes(candidate)) return candidate;
      parts.pop(); // zh-Hant-TW -> zh-Hant -> zh
      if (parts.length === 1 && SCRIPT_SENSITIVE.has(parts[0])) break; // never fall to bare zh or sr
    }
  }
  return DEFAULT;
}

// string-level chain for one key, once the locale is resolved
export function lookup(key: string, locale: string): string {
  // baseLanguageOf keeps the script: pt-BR -> pt, zh-Hant-TW -> zh-Hant
  const chain = [locale, baseLanguageOf(locale), SOURCE_LOCALE];
  for (const l of chain) {
    const value = catalogs[l]?.[key];
    if (value !== undefined) {
      if (l !== locale) recordFallback(key, locale, l); // telemetry, see below
      return value;
    }
  }
  return reportMissing(key); // loud in dev, see the policy below
}

Because negotiation is a pure function from a request list to one supported tag, it is also the easiest part of the i18n stack to test exhaustively. A table-driven test with a few dozen cases pins down the intended behavior: pt-PT requested with only pt-BR supported, zh-TW against both Chinese scripts, sr-Latn against Cyrillic-only Serbian, an unsupported language followed by a supported second preference, and an empty request. When someone later changes the supported list or the matching logic, the table shows exactly which users would land somewhere different.

Negotiation runs once, at the routing boundary, and produces the single resolved locale that the whole request uses. When individual components re-negotiate, reading a header here and a cookie there, two parts of one page can resolve to different locales. That is how mixed-language pages happen even when every catalog is complete.

The default locale is a designed choice, not an alphabetical accident or the first entry in a config file. It should be the language most of the site's audience can at least partially read. And landing on it through a failed negotiation should stay visible: the page should make it obvious that another language is available and how to switch, rather than presenting the default as though it were what the user asked for.

Publish regional catalogs only where content genuinely diverges. If the only Portuguese a team maintains is Brazilian Portuguese, publishing it as pt-BR alone means that, under lookup-style matching like the sketch above, a pt-PT request falls straight to the default language. Publishing it as pt, with pt-BR as an alias, lets every Portuguese request land on Portuguese. Regional catalogs belong where spelling, vocabulary, or legal content actually differ, and each one should earn its maintenance cost. That keeps the locale matrix from multiplying faster than the translation budget.

String-level fallback: the missing translation

Once a locale is resolved, individual keys can still be missing: a feature shipped before its German translation arrived, or a string was edited and its translation invalidated. The chain per key is short: the requested locale, then its base language, then the source language the product is authored in.

The order is rarely the hard part. The design question is visibility: when a string falls back, who finds out? A per-environment policy is the working pattern:

EnvironmentBehavior on a missing keyWhy
DevelopmentRender loudly: the raw key or a visible markerDevelopers see gaps immediately while building the feature
CIFail the build for release localesA gap cannot reach production unnoticed; the catalog-pipeline article owns the gate itself
ProductionFall back silently along the chainA source-language string is better for the user than a broken UI

A missing key is not the only gap. A stale translation, one whose source string changed after it was translated, is worse in a way: it renders confidently in the right language and says the wrong thing. Whether a source edit invalidates the translation, sending that key down the fallback chain, or merely flags it for review while the old text keeps rendering, is a decision to make explicitly per kind of change. A typo fix in the source may not need retranslation; a changed price or feature description does.

Silent fallback in production is correct for the user's session and dangerous for the product over time, which is why it must be paired with measurement, covered below.

A page that mixes locales because half its keys fell back is a shipped defect even though nothing crashed. The structural answer is a coverage threshold per locale: a locale ships, or stays live, only when its catalog passes a completeness bar for the pages that matter. A locale at 60 percent coverage is not a German site with some gaps; it is a bilingual site nobody designed. The catalog tooling measures the number, and this layer decides what the number means.

Some content never falls back. Legal terms, prices, and contractual copy that is missing in the requested language is a blocker, not a fallback case: showing a user an English contract under a German interface can be a compliance question. In the EU, for example, Article 6(7) of the Consumer Rights Directive (2011/83/EU) lets Member States maintain or introduce national language requirements for the contractual information in distance and off-premises contracts, so that the consumer can easily understand it. The depth of it belongs to the insight localization literature.

Keeping the document truthful

When content falls back, the page's language metadata can start telling lies. A page served under /de/ declares <html lang="de">. If a paragraph inside it fell back to English, that paragraph is now labeled German. The W3C guidance on language declarations is clear that the attribute should describe the content actually present, and the precise tool is per-element lang on the regions that fell back:

html
<html lang="de">
  <main>
    <h1>Preise</h1>
    <p lang="en">This plan includes priority support.</p>
  </main>
</html>

This matters mechanically, not just semantically. Several rendering systems key on the lang attribute: screen readers can use it to pick pronunciation rules, so English text marked German may be read aloud with German phonetics; CSS hyphenation uses the language's dictionary; browsers select fonts and glyph variants by language, which is visible in CJK text; and quotation marks rendered by the q element follow the language. One wrong attribute can degrade all of them at once. When the lookup function knows a string fell back, the component rendering it can mark the element accordingly.

Make fallback observable. Count fallback hits per key and per locale in telemetry, the recordFallback call in the sketch above. Without it, a production chain that is silent by design becomes invisible decay: catalogs drift out of date and nobody sees it. With it, the list of most-requested untranslated keys is the localization backlog, already sorted by user impact.

To restate the boundaries: detection and suggestion policy belongs to insight locale-detection, the URL scheme and missing-page routing to the routing article, and catalog completeness gates in CI to the message-catalogs article. This layer's job is to make both fallback chains deliberate, visible, and honest about which language is on the screen.

Project Inquiry