Table of Contents

Class TlumachCultureOptions

Namespace
Tlumach.Web
Assembly
Tlumach.Web.dll

The cultures that a web application supports, and the path of the endpoint that stores the culture chosen by the user.

Shared by the Blazor, the ASP.NET Core hosting, and the MVC and Razor Pages integrations. Register an instance with AddTlumachCultures(IServiceCollection, Action<TlumachCultureOptions>?) or, in a Blazor application, with AddTlumachBlazor.

public class TlumachCultureOptions
Inheritance
TlumachCultureOptions
Derived
Inherited Members

Fields

DefaultCultureEndpoint

The default value of CultureEndpoint.

public const string DefaultCultureEndpoint = "/tlumach/culture"

Field Value

string

Properties

CultureEndpoint

Gets or sets the path of the endpoint that stores the culture in a cookie, relative to the base path of the application.

public string CultureEndpoint { get; set; }

Property Value

string

DefaultCulture

Gets or sets the culture used when the culture of the request or of the browser is not supported.

When not set, the first supported culture is used.

public CultureInfo? DefaultCulture { get; set; }

Property Value

CultureInfo

SupportedCultures

Gets or sets the cultures that the user may choose. When the list is empty, any culture is accepted.

public IReadOnlyList<CultureInfo> SupportedCultures { get; set; }

Property Value

IReadOnlyList<CultureInfo>

Methods

FindSupportedCulture(CultureInfo)

Finds the supported culture that serves the given culture: the culture itself, then the closest of its parent cultures, then a culture of the same language.

public CultureInfo? FindSupportedCulture(CultureInfo culture)

Parameters

culture CultureInfo

The requested culture.

Returns

CultureInfo

The matching supported culture, the requested culture itself when SupportedCultures is empty, or null if no supported culture matches.

FindSupportedCulture(string)

Finds the supported culture that serves the culture with the given name.

When the name is the name of a predefined culture (one listed by GetCultures(CultureTypes) with AllCultures, compared ignoring case), that culture is obtained with GetCultureInfo(string) and matched by FindSupportedCulture(CultureInfo), so both overloads give the same result. When SupportedCultures is empty, the predefined culture is returned.

Any other name is matched with string operations only, and no culture is created for it: the culture itself, then the closest culture whose name is a prefix of the name (subtags are removed from the end one at a time), then a culture of the same language. When SupportedCultures is empty, such names are rejected. Because the set of predefined names is finite and no other culture is ever created, arbitrary names (for example, from a request) do not grow the process-wide culture cache, in which the runtime keeps the data of every created culture for the lifetime of the process.

On ICU-based runtimes, some legacy region names (for example, zh-TW, zh-CN, sr-RS) are not predefined cultures, so they are matched as strings, that is, by language rather than by script. List the cultures you support explicitly (for example, zh-Hant-TW) if that matters.

An empty name or a name that consists of white space only yields null.

public CultureInfo? FindSupportedCulture(string cultureName)

Parameters

cultureName string

The name of the requested culture, for example "de-AT".

Returns

CultureInfo

The matching supported culture, the predefined culture with the given name when SupportedCultures is empty, or null if no supported culture matches (or, when SupportedCultures is empty, if no predefined culture has the given name).

ResolveInitialCulture(CultureInfo)

Picks the culture to start with: the supported culture that serves current, else DefaultCulture, else the first supported culture.

public CultureInfo ResolveInitialCulture(CultureInfo current)

Parameters

current CultureInfo

The culture of the request or of the browser.

Returns

CultureInfo

The culture to use.