Is Blazor server culture set via a url?

David Thielen 2,306 Reputation points
2023-10-10T04:32:12.4233333+00:00

Hi all;

Reading this section, it looks like the way to set the locale in Blazor for a user is:

 Navigation.NavigateTo(
                    $"Culture/Set?culture={cultureEscaped}&redirectUri={uriEscaped}",
                    forceLoad: true);

Is that correct? That strikes me as a really convoluted solution.

And does setting this then set it for the session/circuit? For both the strings returned from IStringLocalizer and the date/number formatting?

Also, is it correct to assume that the culture for a session/circuit, if I don't navigate to this url, is the one set in the user's browser?

thanks - dave

Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,413 questions
0 comments No comments
{count} votes

Accepted answer
  1. Zhi Lv - MSFT 32,036 Reputation points Microsoft Vendor
    2023-10-20T03:18:00.52+00:00

    Hi @David Thielen

    As we all known, in the Blazor Server application, the communication or navigation is based on SignalR circuit (WebSockets). So, if localization schemes are based on the URL path or query string, the scheme might not be able to work with WebSockets, thus fail to persist the culture. Therefore, the recommended approach is to use a localization culture cookie, it will ensure that the WebSocket connection can correctly propagate the culture. See the Dynamically set the server-side culture by user preference section of this article to see an example Razor expression that persists the user's culture selection.

    So, it will use the follow code to achieve a redirect-based approach with a localization cookie. The app persists the user's selected culture via a redirect to a controller. The controller sets the user's selected culture into a cookie and redirects the user back to the original URI.

     Navigation.NavigateTo(
                        $"Culture/Set?culture={cultureEscaped}&redirectUri={uriEscaped}",
                        forceLoad: true);
    

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,

    Dillion

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Josué Levi Aguilar 0 Reputation points
    2023-10-10T05:43:51.13+00:00

    That code snippet you posted seems to be navigating to a "Culture/Set" endpoint with culture and redirectUri parameters. It looks like a custom solution for setting the culture in a Blazor application. Typically, setting the culture for a user in a web application involves updating the CultureInfo for the current thread.

    If you're dealing with localization and want to set the culture for the session, you might want to look into using System.Globalization.CultureInfo directly. For example:

    var cultureInfo = new CultureInfo("es-MX"); // Set your desired culture
    CultureInfo.DefaultThreadCurrentCulture = cultureInfo;
    CultureInfo.DefaultThreadCurrentUICulture = cultureInfo;
    
    

    This would affect how date, time, and number formatting is handled in your application.

    As for the default behavior without navigating to a custom endpoint, Blazor typically relies on the user's browser settings to determine the initial culture. However, if you want to override that behavior, setting it in the ConfigureServices method in Startup.cs might be more straightforward:

    services.Configure<RequestLocalizationOptions>(options =>
    {
        options.DefaultRequestCulture = new RequestCulture("es-MX");
    });