Culture-Specific Formatting

  • 2 minutes to read

In .NET applications, formatting options for currency, numbers, and dates are not related to the application's UI language specified as the Thread.CurrentUICulture property value. The formatting options that are set in the current user's operating system or passed by the Internet browser are used. You can also specify a specific formatting culture or customize the default settings.

Change the Formatting Culture

You can set another formatting culture using the XafApplication.SetFormattingCulture method, which changes the Thread.CurrentCulture value. The following code demonstrates how to do this in a Windows Forms application project's Program.cs (Program.vb) file:

static void Main() {
    // ...
    MySolutionWindowsFormsApplication winApplication = 
        new MySolutionWindowsFormsApplication();
    winApplication.SetFormattingCulture("de");
    // ...
}

The following image, illustrates the result.

CustomizeCultureWin2

Analogously, you can change the formatting culture an ASP.NET application project's Global.asax.cs (Global.asax.vb) file:

protected void Session_Start(Object sender, EventArgs e) {
    WebApplication.SetInstance(Session, new MySolutionAspNetApplication());
    WebApplication.Instance.SetFormattingCulture("de");
    // ...
}
TIP

For Blazor UI applications

Refer to the ASP.NET Core Blazor globalization and localization for information on how to change language at runtime.

Override the Default Formatting Options

You can override the default formatting options in the XafApplication.CustomizeFormattingCulture event handler. The following code demonstrates how to do this in a Windows Forms application project's Program.cs (Program.vb) file:

public static void Main() {
    //...
    MySolutionWindowsFormsApplication winApplication = new MySolutionWindowsFormsApplication(); 
    winApplication.CustomizeFormattingCulture += 
        new EventHandler<CustomizeFormattingCultureEventArgs>(
            winApplication_CustomizeFormattingCulture);
      // ...
}
static void winApplication_CustomizeFormattingCulture(
    object sender, CustomizeFormattingCultureEventArgs e) {
    e.FormattingCulture.NumberFormat.CurrencySymbol = "USD";
}

The following image illustrates the currency symbol before and after implementing the code above:

CustomizeCultureWin

The following code demonstrates how override the default formatting options in an ASP.NET application project's Global.asax.cs (Global.asax.vb) file:

protected void Session_Start(Object sender, EventArgs e) {
    WebApplication.SetInstance(Session, new MySolutionAspNetApplication());
    WebApplication.Instance.CustomizeFormattingCulture += Instance_CustomizeFormattingCulture;
    // ...
    WebApplication.Instance.Setup();
    WebApplication.Instance.Start();
}

The following image illustrates the currency symbol after implementing the code above:

CustomizeCultureWeb