Skip to content

Blazor globalization inconsistency between WASM and Server #54486

@guardrex

Description

@guardrex

Describe the bug

Working on updates to the Blazor Glob/Loc topic on dotnet/AspNetCore.Docs#22525. cc: @hishamco

Either I've hit a bug or a personal knowledge gap: Globalization of a number isn't consistent across Blazor Server and Blazor WebAssembly.

  • If a knowledge gap that I have, then I'll address this behavior in the topic's updates.
  • If a bug, then here it is.

To Reproduce

Blazor WebAssembly (✔️ Working-as-expected Case)

  1. Create a new Blazor WebAssembly app from the project template.

  2. Add CultureExample1 component to the app's Pages folder:

    @page "/culture-example-1"
    @using System.Globalization
    @using System.Threading
    
    <h1>Culture Example 1</h1>
    
    <!-- 
        Updated code here, but screenshot is this comment will be left as-is.
        I'll add a screenshot of this output at the end of this issue.
    -->
    <ul>
        <li><b>CultureInfo.CurrentCulture</b>: @CultureInfo.CurrentCulture</li>
        <li><b>CultureInfo.CurrentUICulture</b>: @CultureInfo.CurrentUICulture</li>
        <li><b>Thread.CurrentThread.CurrentCulture</b>: @Thread.CurrentThread.CurrentCulture</li>
        <li><b>Thread.CurrentThread.CurrentUICulture</b>: @Thread.CurrentThread.CurrentUICulture</li>
    </ul>
    
    <h2>Rendered values</h2>
    
    <ul>
        <li><b>Date</b>: @dt</li>
        <li><b>number</b>: @number.ToString("N2")</li>
    </ul>
    
    @code {
        private DateTime dt = DateTime.Now;
        private double number = 1999.69;
    }
  3. Run the app. Use a browser with Peruvian Spanish (es-PE) as the first language choice:

    Capture1

    The date AND number are globalized (i.e., in Spanish: date is day-month format + 24-hour clock & number has a comma decimal and period separator). ✔️ All good. ✔️

Blazor Server (❌ Inconsistent/Not-working-as-expected Case)

  1. Create a new Blazor Server app from the project template.

  2. Add loc services:

    services.AddLocalization();
  3. Add loc middleware to the pipeline immediately after Routing Middleware:

    app.UseRequestLocalization(new RequestLocalizationOptions()
        .AddSupportedCultures(new[] { "en-US", "es-PE" })
        .AddSupportedUICultures(new[] { "en-US", "es-PE" }));
  4. Add the same CultureExample1 component ☝️ to the app's Pages folder.

  5. Run the app. Use a browser with Peruvian Spanish (es-PE) as the first language choice:

Capture

The date is globalized ✔️, BUT the number isn't ❌ (i.e., the number doesn't have a comma decimal and period separator).

Is this the expected outcome, or is this a 🐞? If expected behavior, why? ... because I'll need to address this delta in the doc.

Exceptions (if any)

None

Further technical details

  • ASP.NET Core version

    5.0.6

  • Include the output of dotnet --info

    .NET SDK (reflecting any global.json):
     Version:   5.0.300
     Commit:    2e0c8c940e
    
    Runtime Environment:
     OS Name:     Windows
     OS Version:  10.0.19043
     OS Platform: Windows
     RID:         win10-x64
     Base Path:   C:\Program Files\dotnet\sdk\5.0.300\
    
    Host (useful for support):
      Version: 5.0.6
      Commit:  478b2f8c0e
    
    .NET SDKs installed:
      3.1.302 [C:\Program Files\dotnet\sdk]
      5.0.202 [C:\Program Files\dotnet\sdk]
      5.0.300 [C:\Program Files\dotnet\sdk]
    
    .NET runtimes installed:
      Microsoft.AspNetCore.All 2.1.28 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
      Microsoft.AspNetCore.All 2.2.8 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
      Microsoft.AspNetCore.App 2.1.28 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
      Microsoft.AspNetCore.App 2.2.8 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
      Microsoft.AspNetCore.App 3.1.15 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
      Microsoft.AspNetCore.App 5.0.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
      Microsoft.AspNetCore.App 5.0.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
      Microsoft.NETCore.App 2.1.28 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
      Microsoft.NETCore.App 2.2.8 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
      Microsoft.NETCore.App 3.1.15 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
      Microsoft.NETCore.App 5.0.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
      Microsoft.NETCore.App 5.0.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
      Microsoft.WindowsDesktop.App 3.1.15 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
      Microsoft.WindowsDesktop.App 5.0.5 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
      Microsoft.WindowsDesktop.App 5.0.6 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
    
  • The IDE (VS / VS Code/ VS4Mac) you're running on, and its version

    Visual Studio 16.10.0

Metadata

Metadata

Assignees

Labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions