'Simpliest way to open the page in the specific culture in Blazor?

My Blazor Server App works on server with En-US culture. All pages except one should open in En-Us culture regardless browser settings.

But one specific page should open in It-It culture.

What is easiest way to achive this?

I can set the culture in OnInitialized method:

protected override void OnInitialized() {
    CultureInfo cultureInfo = CultureInfo.CreateSpecificCulture("it-IT");
    CultureInfo.DefaultThreadCurrentUICulture = cultureInfo;
    CultureInfo.DefaultThreadCurrentCulture = cultureInfo;
}

And set it back in MainLayout:

protected override void OnParametersSet() {
    CultureInfo cultureInfo = CultureInfo.CreateSpecificCulture("en-Us");
    CultureInfo.DefaultThreadCurrentUICulture = cultureInfo;
    CultureInfo.DefaultThreadCurrentCulture = cultureInfo;
}

But I think it is not the best solution.



Solution 1:[1]

To Understand what it's the best solution for this, we need to understand the Lifecycle Events

OnInitialized and OnInitializedAsyc are invoked when the component is initialized after having received its initials parameters that you set on the layout. Based on that you will read first your MainLayout Parameters configuration and then you will "override" those with OnInitialized. And then, when the component reRender again. It will set it back the other culture So it's an easy solution with bad performance on it.

If you want a better and scalable code i think the best way to do it it's with a middleware (In this example they apply it on a date parameter, but you can apply it on a fullpage). With the middleware you don't need to ReRender the page everytime you switch cultures, with only the first render, the component will know which culture choose and apply it on the first time that the component it's rendering

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Leandro Toloza