15

ASP.NET Core Blazor globalization and localization states:

Blazor's @bind functionality performs formats and parses values for display based on the user's current culture.
The current culture can be accessed from the System.Globalization.CultureInfo.CurrentCulture property.

The statement is true, but the problem is that, the culture has to be set just before it is used (or maybe each time the DOM is refreshed).

For demonstration I will use standard blazor counter application. Let's modify Counter.razor

@page "/counter"
@using System.Globalization;

<h1>Counter</h1>
<input type="text" @bind="currentDate" />

<p>Current count: @currentCount</p>

<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>

@code {
    private DateTime currentDate = DateTime.Now;
    private int currentCount = 0;

    private void IncrementCount() {
        if (currentCount < 2) Utils.SetCCDateFormat();
        currentCount++;
    }

    public class Utils {
        public static void SetCCDateFormat() {
            var cc = CultureInfo.CurrentCulture.Clone() as CultureInfo;
            cc.DateTimeFormat.ShortDatePattern = "dd-yyyy-m";
            CultureInfo.CurrentCulture = cc;
            CultureInfo.CurrentUICulture = cc;
        }
    }

} 

The result is:

  • when the page is first rendered the text box contain date formatted by server default culture.
  • when the button is pressed first and second time the date format is dd-yyyy-m

I attempted to modify the date in OnAfterRender, OnInitialized without success. Only usable solution, I have found, is setting the format on the begging of razor markup.

@{Utils.SetCCDateFormat();}

Is there a way to modify CurrentCulture to become persistent in the blazor circuit?

Is the observed behavior correct or is it a bug?

Edit

What I have found so far

It is possible to set culture properties (CultureInfo.CurrentCulture) in a middleware before the blazor endpoint is created and the changes are persistent for the circuit lifetime. When we modify CurrentCulture in component lifecycle methods the change is only temporary (till the end of the method).

My understanding of the problem is

  • When a circuit is created it stores the current culture somewhere
  • A server has a limited number of threads
  • A thread is assigned to a circuit when required and the current culture is set by what was stored at the beginning
  • It is possible to modify the CurrentCulture, but this doesn't influence the setting storage and so when another event method is called (other thread) the original culture is used.

So it seems that the question is: How to modify the circuit culture settings when it is already created?

Maybe it is not possible and it is necessary do full refresh (start a request again with navigation) and use a middleware to set a modified culture. A culture storage existence is only my conjecture and I don't have any reference to support it.

Many thanks to Tyeth and Ashiquzzaman for help but I am not taking their attempts as the answer.

5
  • Did you check this github issue? Commented Mar 18, 2020 at 9:44
  • 1
    @PavelAnikhouski: I am aware a problem with current culture in blazor webassembly (mono). I think (hope), that the situation in blazor server is different.
    – IvanH
    Commented Mar 18, 2020 at 9:48
  • IvanH you need to clarify your understanding of thread lifecycle and how each thread culture is initialised or just accept that middleware is the standard design and fullfils your requirements unless you wish to randomly adjust a threads culture on a whim. Alternatively realise the question is not "How to modify the circuit culture settings when it is already created?" But actually how to modify the culture that is created initially per user/request/thread depending on your needs
    – Tyeth
    Commented Apr 13, 2020 at 22:47
  • @Tyeth: I am now aware abilities of a middleware in a culture initial setting. Now it seems that my real world problem will can be solved by a middleware. But my question is about the date format modification. I personally feel a difference between an initial setting (middleware - once) and a modification (lifecycle methods - repeatedly).
    – IvanH
    Commented Apr 14, 2020 at 8:03
  • Thanks for clarification. I guess the question in my mind is do you need to modify the display/parsing for a single use case (page/control) in which case just format in situ using string.format, otherwise are you asking to create a custom culture to essentially change the date format for ever more. Either way myself I would still set the custom culture up upon login/first visit or pick normal culture upon login and then string format or date.parse for the one place it was different (last resort with a custom model binder?)
    – Tyeth
    Commented Apr 14, 2020 at 11:23

2 Answers 2

5
+50

1) Use middleware

Example:

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
      //your Code
        app.Use(async (context, next) =>
        {
            var culture = CultureInfo.CurrentCulture.Clone() as CultureInfo;// Set user culture here
            culture.DateTimeFormat.ShortDatePattern = "dd-yyyy-m";
            CultureInfo.CurrentCulture = culture;
            CultureInfo.CurrentUICulture = culture;

            // Call the next delegate/middleware in the pipeline
            await next();
        });
      //your Code
    }

2) Custom middleware with service:

Service :

public interface ICultureService
{
    void SetCCDateFormat();
}
public class CultureService : ICultureService
{
    public void SetCCDateFormat()
    {
        CultureInfo culture = (CultureInfo)CultureInfo.CurrentCulture.Clone();
        culture.DateTimeFormat.ShortDatePattern = "dd-yyyy-m";
        CultureInfo.CurrentCulture = culture;
        CultureInfo.CurrentUICulture = culture;
    }
}

Middleware:

public class CultureMiddleware
{
    private readonly RequestDelegate _next;

    public CultureMiddleware(RequestDelegate next)
    {
        _next = next;

    }

    public Task Invoke(HttpContext context, ICultureService culture)
    {           
        culture.SetCCDateFormat();
        return this._next(context);
    }
}

Startup:

    public void ConfigureServices(IServiceCollection services)
    {
        //Your Code
        services.AddScoped<ICultureService, CultureService>();
        //Your Code
    }
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        //Your Code
        app.UseMiddleware<CultureMiddleware>();
        //Your Code
    }

Culture.razor:

@page "/culture"
@inject ICultureService CultureService
<h1>Counter</h1>
<input type="text" @bind="currentDate" />

<p>Current count: @currentCount</p>

<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>

@code {
    private DateTime currentDate = DateTime.Now;
    private int currentCount = 0;

    private void IncrementCount()
    {
         if (currentCount < 2) CultureService.SetCCDateFormat();
        currentCount++;
    }
}

3) If you can to change default Culture of the application the use localization Middleware. Blazor Server apps are using Localization Middleware for Localization & Globalization. The current culture on a request is set in the localization Middleware. The localization middleware is enabled in the Startup.Configure method. The localization middleware must be configured before any middleware which might check the request culture (for example, app.UseMvcWithDefaultRoute()).

Example:

var culture = new CultureInfo("en-US");
            culture.DateTimeFormat.ShortDatePattern = "dd-yyyy-MM";
            var supportedCultures = new List<CultureInfo> { culture };
        app.UseRequestLocalization(new RequestLocalizationOptions
        {
            DefaultRequestCulture = new RequestCulture(culture, culture),
            // Formatting numbers, dates, etc.
            SupportedCultures = supportedCultures,
            // UI strings that we have localized.
            SupportedUICultures = supportedCultures
        });
4
  • It is possible to use a middleware to set the culture before the circuit is created. The question looks answer how to modify a current culture (when the circuit is already created). DefaultThreadCurrentCulture modifies the culture for all threads on the server (be aware of multiuser envirnonment with thousands of users). The timezone issue is connected to webassembly (and is mentioned in an original post comment). Would you, please delete no longer relevant comments (to keep the question clean)?
    – IvanH
    Commented Apr 13, 2020 at 7:06
  • A middleware sets the initial culture. Doesn't modify the current one.
    – IvanH
    Commented Apr 13, 2020 at 8:11
  • I have awarded the bounty to this answer as most helpful, but I am still looking for an answer which can be accepted.
    – IvanH
    Commented Apr 13, 2020 at 11:40
  • @IvanH Thanks. But now I think option 2 is fulfill your requirement. You have full control. Populate data for anywhere in your service. Commented Apr 13, 2020 at 11:58
0

The second part of Ashiquzzaman's answer (after OR) hints at the correct path to take.

The built in ASP.Net Core localisation middleware will be your new best friend: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/localization?view=aspnetcore-3.1#localization-middleware

You can rely on the default included CookieRequestCultureProvider, and by setting a cookie for your logged in users on a per user basis (I suggest upon login as you will have access to the Users stored preferences then) you can override the default browser requested language (or system default as a last resort).

The listed order of RequestLocalizationProviders is on that link posted and suggests the querystring can override the cookie which can override the browser preference:

https://learn.microsoft.com/en-us/aspnet/core/fundamentals/localization?view=aspnetcore-3.1#cookierequestcultureprovider

  1. QueryStringRequestCultureProvider
  2. CookieRequestCultureProvider **
  3. AcceptLanguageHeaderRequestCultureProvider

The CookieRequestCultureProvider DefaultCookieName returns the default cookie name used to track the user's preferred culture information. The default cookie name is .AspNetCore.Culture.

The cookie format is c=%LANGCODE%|uic=%LANGCODE%, where c is Culture and uic is UICulture, for example:

c=en-UK|uic=en-US

You should definitely read the blazor section on globalisation and localisation as suggested by Ashiquzzaman: https://learn.microsoft.com/en-us/aspnet/core/blazor/globalization-localization?view=aspnetcore-3.1

3
  • The question is about modifiing the culture (date format) - not setting the culture. This is the reason why a crazy date format is used in the example.
    – IvanH
    Commented Apr 12, 2020 at 20:55
  • I read that it respects server culture until you update using user culture. Please read the last link, note the date field uses Invariant and checkout the sample project on github, you should find (if you set your region + browser to french) that the sample site should respect it. Also notice the link to localisation and globalisation for your ASP.Net server side too if using Blazor server apps. You may wish to make some middleware that changes the culture as someone logs in, just setting the cookie should be enough, but you should always allow the user to easily change language/culture.
    – Tyeth
    Commented Apr 12, 2020 at 22:51
  • The idea is to have something before the Endpoint in this diagram learn.microsoft.com/en-us/aspnet/core/fundamentals/middleware/…
    – Tyeth
    Commented Apr 12, 2020 at 22:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.