Table of Contents

Class TlumachCultureState

Namespace
Tlumach.Blazor
Assembly
Tlumach.Blazor.dll

Holds the culture of one user and switches it.

The service is scoped: Blazor Server creates one per circuit (and one per prerendering request), so the users of one server do not share the culture; Blazor WebAssembly and Blazor Hybrid effectively have one per application.

Components re-render after a switch through the cascading TlumachCulture value; other code can subscribe to CultureChanged.

public sealed class TlumachCultureState
Inheritance
TlumachCultureState
Inherited Members

Constructors

TlumachCultureState(TlumachBlazorOptions, ITlumachCultureStore, NavigationManager, ILogger<TlumachCultureState>)

Initializes a new instance of the TlumachCultureState class with the culture that the request localization middleware (Blazor Server, static SSR) or LoadTlumachCultureAsync(IServiceProvider) (Blazor WebAssembly) has set.

public TlumachCultureState(TlumachBlazorOptions options, ITlumachCultureStore store, NavigationManager navigationManager, ILogger<TlumachCultureState> logger)

Parameters

options TlumachBlazorOptions

The options.

store ITlumachCultureStore

The store for the chosen culture.

navigationManager NavigationManager

The navigation manager, used to reload the page.

logger ILogger<TlumachCultureState>

The logger.

Properties

Culture

Gets the culture of the user.

public CultureInfo Culture { get; }

Property Value

CultureInfo

Current

Gets the snapshot of the culture that is cascaded to the components.

public TlumachCulture Current { get; }

Property Value

TlumachCulture

LocalizerCulture

Gets the culture that the localizers of this user should use, for example those of integrations with component suites; they may also be used outside the user's scope.

After a live switch in this scope, it is Culture. Before that, it is the culture of the current execution context (the request or circuit), matched against the supported cultures. Inside the user's scope both are the same until the first switch, because the state is created from that culture; a state resolved from the root provider, which is never switched, thus follows each request instead of keeping the culture of the application's start.

public CultureInfo LocalizerCulture { get; }

Property Value

CultureInfo

SupportedCultures

Gets the cultures that the user may choose.

public IReadOnlyList<CultureInfo> SupportedCultures { get; }

Property Value

IReadOnlyList<CultureInfo>

Methods

SetCultureAsync(CultureInfo, bool)

Switches the culture of the user.

public Task SetCultureAsync(CultureInfo culture, bool forceReload = false)

Parameters

culture CultureInfo

The new culture. It must match one of SupportedCultures (see FindSupportedCulture(CultureInfo)).

forceReload bool

true to store the culture and reload the page, so that everything, including the formatting that does not go through Tlumach, uses the new culture; false to switch the language live.

Returns

Task

A task that completes when the components have been notified and the culture has been stored.

Exceptions

ArgumentException

The culture is not supported.

Events

CultureChanged

Occurs after the culture has been switched, before the components re-render.

If a handler throws, the components are still notified and the culture is still stored; then the exception propagates from SetCultureAsync(CultureInfo, bool).

public event EventHandler<CultureChangedEventArgs>? CultureChanged

Event Type

EventHandler<CultureChangedEventArgs>