meta info

Culture Fallback Behavior

By Ziya Mollamahmut

Asp.Net Core uses localization culture providers to detect the request culture and respond accordingly. The culture checking process goes one by one through all registered providers, whenever a request culture is detected the check process stops and the localization process starts accordingly.

By default XLocalizer will use below RequestCultureProviders in order to detect the request culture (It may change depending on the startup configuration):

  1. RouteSegmentRequestCultureProvider
  2. QueryStringRequestCultureProvider
  3. CookieRequestCultureProvider
  4. AcceptedLanguageHeaderRequestCultureProvider

Depending on the supported cultures list provided in startup, it will try to match cultures in the list by the request culture in the providers till it find the first match and respond accordingly.

So when a request is made, the localization middleware will start looking for the culture value in the defined providers.

  • First try with RouteSegmentRequestCultureProvider to look for {culture} value in the route https://localhost:1234/xx.
  • If there is no {culture} provided in the route, the next provider will be checked QueryStringRequestCultureProvider to find culture value in the query string https://localhost:1234/?culture=xx,
  • Again if there is no culture value in the query string, it will go ahead to the next provider CookieRequestCultreProvider (below is a sample culture cookie for TR culture.)

Asp.Net Core Culture Cookie

  • If there is no culture param in the cookie it will check the last culture provider AcceptedLanguageHeaderRequestCultureProvider which provides a list of accepted cultures. (Below you can see a screenshot of chrome supported cultures list, you may change the order of your browsers cultures and see how it affect the request culture.)_

Chrome Browser Accepted Languages

Finally, if it can't detect the request culture in any of the above providers then it will use the DefaultRequestCulture that has been defined in RequestLocalizationOptions in startup.

services.Configure<RequestLocalizationOption>(ops =>
{
    // ...
    ops.DefaultRequestCulture = new RequestCulture("en");
});