In ASP.NET Core request localization, how do SupportedCultures and SupportedUICultures differ, and when should the two lists hold different cultures?
answer
- formatting versus translations
- c= and uic= in the culture cookie
- specific cultures for formats, neutral for text
- each half matched separately
- an unmatched half takes the default
basics
~10 sSupportedCultures limits CurrentCulture, which formats dates, numbers and currency and drives culture-aware comparison; SupportedUICultures limits CurrentUICulture, which chooses translated resources. Regional sites list many specific cultures for formatting and a few languages for text.
solid answer
~40 sEach request gets two cultures. `CurrentCulture`, chosen from `SupportedCultures`, formats dates, numbers and currency symbols and drives culture-aware sorting and comparison. `CurrentUICulture`, chosen from `SupportedUICultures`, decides which `.resx` translation is read. The middleware matches the two halves separately, each with its own parent-fallback flag, so a travel guide can list `de-DE`, `de-CH` and `de-AT` for formatting but only `de` for translations: a Swiss visitor gets Swiss number formats and the shared German text. The trap is a half-match: if only the UI culture matches, the culture comes from `DefaultRequestCulture`, so `fr-BE` against `SupportedCultures` of `en-GB` and `fr-FR` yields French text with British dates. Adding the neutral `fr` to `SupportedCultures` fixes it. Query strings (`culture`, `ui-culture`) and the cookie (`c=…|uic=…`) can carry the two separately.
code
csharp · 15 linesbuilder.Services.AddRequestLocalization(options =>
{
options.SetDefaultCulture("en-GB");
// formatting: the regions the guide sells in, plus neutral catch-alls
options.AddSupportedCultures(
"en-GB", "en-US", "fr-FR", "fr-CH", "de-DE", "de-CH", "de-AT",
"en", "fr", "de");
// translations: one resource set per language
options.AddSupportedUICultures("en", "fr", "de");
});
// Accept-Language: de-CH -> CurrentCulture de-CH, CurrentUICulture de
// Accept-Language: fr-BE -> CurrentCulture fr, CurrentUICulture frgo deeper
Recall that the culture formats dates and numbers while the UI culture picks the translation, and that each has its own supported list.
Explain separate matching, parent fallback per half, how query string and cookie carry the two values, and what happens when only one half matches.
Design the lists for a regional product: specific cultures for formats, neutral ones for text, catch-alls to avoid half-matches, and stored currencies kept independent of culture.
Decide how far regional formatting goes versus translation cost, which markets get their own resources, and who owns the supported-culture lists as markets are added.
## Two cultures per request .NET gives every execution context two culture properties, and request localization sets both: | | `CurrentCulture` | `CurrentUICulture` | |---|---|---| | Chosen from | `SupportedCultures` | `SupportedUICultures` | | Parent fallback flag | `FallBackToParentCultures` | `FallBackToParentUICultures` | | Drives | date, number and currency formatting; culture-aware sorting and comparison | which translated resource is read | | Query string key | `culture` | `ui-culture` | | Cookie part | `c=` | `uic=` | The two are often the same, and every built-in provider copies one into the other when only one is given: `?culture=fr` sets both, and a cookie `c=fr|uic=` sets both to `fr`. ## Each half is matched separately When a provider answers, the middleware looks for the culture in `SupportedCultures` and, independently, for the UI culture in `SupportedUICultures`. The outcomes: - **both match**: the provider wins with both; - **neither matches**: the provider is skipped and the next one is asked; - **one matches**: the provider still wins, and the half that did not match is taken from `DefaultRequestCulture`. That last rule is the source of most odd behaviour. ## Where each culture shows up - **`CurrentCulture`**: `DateTime` and number `ToString()` calls, `{0:C}` and `{0:N2}` format strings, values written by Razor such as `@tour.StartDate`, and culture-aware `string.Compare` or `StringComparer.CurrentCulture`. - **`CurrentUICulture`**: `IStringLocalizer<T>` and `IViewLocalizer` lookups, translated DataAnnotations messages, the culture-suffixed views that `LanguageViewLocationExpander` picks, and the `Content-Language` header when `ApplyCurrentCultureToResponseHeaders` is on. A bug report that says "the text is French but the dates are American" is therefore a report about two different settings, and the fix is in `SupportedCultures`, not in the `.resx` files. ## When the lists should differ A travel guide sells tours in many regions but translates its pages into three languages. Formatting must follow the region; text only needs the language: - list **specific cultures** for formatting, such as `de-DE`, `de-CH`, `de-AT`, `fr-FR`, `fr-CH`; - list **neutral cultures** for text, `en`, `fr`, `de`, and keep one `.resx` per language; - rely on `FallBackToParentUICultures`, on by default, so `de-CH` finds the `de` translations; - also list the neutral `en`, `fr` and `de` in `SupportedCultures` as catch-alls, so a region you did not list still formats in its language. ## The half-match trap, step by step Suppose `SupportedCultures` is `en-GB, fr-FR`, `SupportedUICultures` is `en, fr`, the default is `en-GB`, and a Belgian visitor sends `Accept-Language: fr-BE`: 1. Culture: `fr-BE` is not listed; its parent `fr` is not listed either; no match. 2. UI culture: `fr-BE` is not listed; its parent `fr` is; match. 3. One half matched, so the provider wins; the culture half comes from the default, `en-GB`. 4. The page shows French text with British dates and number formats. Adding `fr` to `SupportedCultures`, or `fr-BE` itself, gives a French culture as well. ## A note on currency The culture formats a currency **symbol** from its own region: `{0:C}` under `de-CH` prints a Swiss franc symbol. A tour price stored in euros must be formatted with the currency it is stored in, not with whatever culture the request chose. Region-specific `SupportedCultures` make this mistake more visible, not less likely. ## Mistakes interviewers listen for - treating the two lists as duplicates that must always be equal; - expecting an unmatched half to stay at the visitor's requested culture rather than the default; - listing only specific cultures for text and then needing one `.resx` per region; - formatting stored prices with the request culture's currency.
- How do you test the two halves separately without changing browser settings?Use the query string provider: `?culture=de-CH&ui-culture=en` gives Swiss formatting with English text, as long as both values are supported. The cookie can carry the same pair as `c=de-CH|uic=en`. Both are handy for checking that formats and translations are wired independently.
- Why can SetDefaultCulture("en-GB") still find English translations when SupportedUICultures lists only en?`SetDefaultCulture` sets both halves of `DefaultRequestCulture` to `en-GB`, and the default is applied without checking the supported lists. Resource lookup then falls back from `en-GB` to `en`, so the English `.resx` is read. Listing `en-GB` in `SupportedUICultures` is unnecessary for the default itself.
A hotel concierge who speaks to you in your language but hands you a map printed for the country you are in: the spoken language and the units on the map are chosen separately, and if no local map exists you get the hotel's standard one, even while being addressed in French.
- SupportedUICultures controls how dates and numbers are formatted.
- Both lists must always contain exactly the same cultures.
- If only the UI culture matches, the culture keeps the visitor's requested value.
- A Swiss visitor needs a separate de-CH .resx to see German text.
- The request culture should decide which currency a stored price is in.