liveBook · Manning

Culture code

As explained in .NET in Action, Second Edition

A culture is a set of language, formatting, sorting, and other regional conventions that influences software behavior and presentation.

A culture code identifies the language and region whose conventions an application should use. In a world-ready application, it can affect visible strings, date and number formats, and sort order. Supporting a new culture should not require changing application logic: culture-specific resources supply the translated content. section 12.5.1 section 12.5.4

For localized sensor descriptions, the unqualified SensorNames.restext file supplies default resources, while SensorNames.es-MX.restext supplies Spanish-language resources for Mexico. A build places the es-MX resources in a culture-specific satellite assembly. ResourceManager selects resources using UI culture; ordinary culture controls formatting, including dates and times. Usually, set both to the same code. section 12.3 section 12.4

Listing 12.12 Enabling ASP.NET Core culture provider
var cultures = new[] { "en-US", "es-MX" };                 #1
var localizationOptions = new RequestLocalizationOptions()
  .SetDefaultCulture(cultures[0])                          #2
  .AddSupportedCultures(cultures)
  .AddSupportedUICultures(cultures);                       #3
app.UseRequestLocalization(localizationOptions);

Configure ASP.NET Core with a default culture and explicit lists of supported cultures and UI cultures, then add request-localization middleware. Its built-in providers check the query string first, then a cookie, then the Accept-Language header. section 12.4

Keep the client and presentation behavior aligned:

  • Set CurrentCulture and CurrentUICulture when producing local output. Also send an Accept-Language header when calling a service: setting the current thread’s culture does not make HttpClient send one automatically. section 12.5.1 section 12.6
  • Do not assemble user-facing labels by concatenating fixed pieces. Resource strings can hold placeholders so a translation can choose where a value appears. section 12.5.1
  • Check assumptions beyond translation. The ar-SA culture can produce a lunar-calendar date, and right-to-left output may not display correctly in every console. section 12.5.3

Verify a culture-specific response

Request the same measurement endpoint with and without ?culture=es-MX, then assert that its descriptions match the expected language. When only culture or ui-culture is supplied as a query parameter, ASP.NET Core copies its value to the other. Adding ar-SA requires both an Arabic resource file and an entry in the supported-cultures list. section 12.4 section 12.5.3

A culture code is not a complete policy for every regional choice. Temperature units, addresses, and paper sizes need separate consideration. Use the invariant culture for stored numbers and backend data that must not vary with a user’s culture. section 12.5.1 section 12.5.4

How .NET in Action, Second Edition covers it

How deep it goes
Works through resource naming, middleware configuration, client requests, and output pitfalls without detailing culture-provider internals.
Examples
Uses Spanish-Mexico and Arabic-Saudi Arabia sensor resources, a localized API response test, and console output.
What it assumes
Assumes familiarity with C# web APIs, resource files, and HTTP clients rather than teaching those components from zero.
What you can do after
Operate/configure it through ASP.NET Core request localization, culture-specific resources, explicit client headers, and response tests.
How it connects
Connects culture codes to UI culture, ResourceManager, satellite assemblies, globalization, localization, and invariant culture.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of .NET in Action, Second Edition. The section links point to the parts of the book it draws on.