Globalization
Globalization combines internationalization (i18n)—parsing and formatting dates, times, numbers, and currencies—and localization (l10n)—adding culture-specific customizations and translating UI text.
The Blazor UI components in this toolkit use American English (en-US) by default. Blazor relies on .NET globalization to parse and format numbers and dates based on the active culture. In Blazor WebAssembly, ensure globalization data is available when using non-en cultures.
Blazor uses built-in .NET types from the System.Globalization namespace, such as the CultureInfo class and its culture properties:
- Culture (CultureInfo.CurrentCulture): Determines the formatting of numbers, dates, and times.
- UI culture (CultureInfo.CurrentUICulture): Determines the language of the user interface and which
.resxresources are used.
Browser-Native Input Types
When working with HTML form fields, browser-native input types affect culture behavior. The following input types are well-supported and reliable:
datenumber
Blazor relies on the browser's handling of these input types, which ensures that user input is parsed and rendered according to their specific culture settings.
The following input types are inconsistently supported across browsers and may be less reliable:
datetime-localmonthweek
Example
The following example shows how globalization affects rendered values by formatting dates and numbers according to the current culture.
@page "/"
@using System.Globalization
<ul>
<li><b>CurrentCulture</b>: @CultureInfo.CurrentCulture</li>
<li><b>CurrentUICulture</b>: @CultureInfo.CurrentUICulture</li>
</ul>
<h2>Rendered values</h2>
<ul>
<li><b>Date</b>: @dt.ToLongDateString()</li>
<li><b>Number</b>: @number.ToString("N2")</li>
</ul>
@code {
private DateTime dt = DateTime.Now;
private double number = 1999.69;
}Localization
To change to a specific culture, add the corresponding .resx resource files and configure localization in your application.
Setting The Culture In Code
To set the active culture for a Blazor app, configure CultureInfo early in the application program file.
The following examples show how to do this for Blazor Server and Blazor WebAssembly.
// Blazor Server — Program.cs (top of file)
using System.Globalization;
// Recommended: set defaults for the whole application thread
CultureInfo.DefaultThreadCurrentCulture = CultureInfo.GetCultureInfo("en-GB");
CultureInfo.DefaultThreadCurrentUICulture = CultureInfo.GetCultureInfo("en-GB");
// Blazor WebAssembly — Program.cs
using System.Globalization;
var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.Services.AddCultureLocalization(
options => options.ResourcesPath = "Resources");
await builder.Build().RunAsync();Blazor WebAssembly — additional requirement: By default, the WebAssembly host does not include globalization ICU (International Components for Unicode) data to keep the application payload size small. To use non-en-US cultures, you must include the ICU data file.
Add the following to your Blazor WebAssembly project file (.csproj):
<!-- In your .csproj file -->
<PropertyGroup>
<BlazorWebAssemblyLoadAllGlobalizationData>true</BlazorWebAssemblyLoadAllGlobalizationData>
</PropertyGroup>
Without ICU data, CultureInfo defaults to en-US regardless of the configured culture.