dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.43k stars 10.01k forks source link

QueryStringRequestCultureProvider Does Not Work With Blazor Server #20346

Open taoyouh opened 4 years ago

taoyouh commented 4 years ago

Describe the bug

When using QueryStringRequestCultureProvider on Blazor Server, the culture immediately changes from the one given in query string to the one set in HTTP headers.

To Reproduce

// Startup.cs
app.UseRequestLocalization("zh-CN", "en");
// Index.razor
@CultureInfo.CurrentCulture.TextInfo

Launch the app with URL http://localhost:xxx/?culture=zh-CN. The page displays "zh-CN" for less than 1 second and changes to "en"

Further technical details

Version of .NET Core SDK:

 Version:   3.1.200
 Commit:    c5123d973b

运行时环境:
 OS Name:     Windows
 OS Version:  10.0.18363
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\3.1.200\

Host (useful for support):
  Version: 3.1.2
  Commit:  916b5cba26

.NET Core SDKs installed:
  2.1.508 [C:\Program Files\dotnet\sdk]
  2.1.509 [C:\Program Files\dotnet\sdk]
  2.1.801 [C:\Program Files\dotnet\sdk]
  3.1.200 [C:\Program Files\dotnet\sdk]

.NET Core runtimes installed:
  Microsoft.AspNetCore.All 2.1.12 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.13 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.16 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.App 2.1.12 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.13 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.16 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 2.1.12 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.13 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.16 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.2 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 3.1.2 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

To install additional .NET Core runtimes or SDKs:
  https://aka.ms/dotnet-download

IDE: Visual Studio 16.5.0

javiercn commented 4 years ago

@taoyouh thanks for contacting us.

This is likely due to the url used to establish the connection with server-side Blazor.

@rynowak do you have any thoughts around this?

@SteveSandersonMS We could consider tackling query string parameters when we setup the connection to the hub so that things like this would possibly work. Do you have any opinions?

mkArtakMSFT commented 4 years ago

@anurse how does query string localization work in SignalR?

analogrelay commented 4 years ago

SignalR doesn’t do anything with it. Any culture information on the HttpContext when it reaches SignalR should be preserved.

javiercn commented 4 years ago

@anurse do you have any docs or thoughts on how to do this E2E for SignalR applications?

analogrelay commented 4 years ago

Nope. It’s not something we’ve really thought about.

AlexTeixeira commented 4 years ago

Hi,

Same here. For info, If I debug the GetString Method, the correct culture is displayed.

mkArtakMSFT commented 4 years ago

We've moved this issue to the Backlog milestone. This means that it is not going to happen for the coming release. We will reassess the backlog following the current release and consider this item at that time. However, keep in mind that there are many other high priority features with which it will be competing for resources.

mhudasch commented 3 years ago

Hi, could recreate the problem in 5.0.2

I debugged through the RequestLocalizationMiddleware and yes the _blazor/ calls reset the current thread culture that is then used during the second call inside a razor component. I had a breakpoint inside the OnInitialized lifecycle hook and on first hit my injected IStringLocalizer instance had the correct string resolved. When the breakpoints hits the second time (which is expected when using blazor server/prerendered) the IStringLocalizer has a different result. Creating logs like:

dbug: Microsoft.Extensions.Localization.ResourceManagerStringLocalizer[1]
      ResourceManagerStringLocalizer searched for 'Welcome' in 'BlazorApp1.Resources.Pages.Index' with culture 'en'.
dbug: Microsoft.Extensions.Localization.ResourceManagerStringLocalizer[1]
      ResourceManagerStringLocalizer searched for 'Welcome' in 'BlazorApp1.Resources.Pages.Index' with culture 'de'.

on each request with culture/ui-culture query parameter used in the url.

TanayParikh commented 3 years ago

Potentially related to: https://github.com/dotnet/aspnetcore/issues/28521

ghost commented 3 years ago

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

rafael-f commented 2 years ago

I don't know why, but after I modified my Pages/_Hosts.cshtml file to contain:

this.HttpContext.Response.Cookies.Append(

        CookieRequestCultureProvider.DefaultCookieName,

        CookieRequestCultureProvider.MakeCookieValue(

            new RequestCulture(

                CultureInfo.CurrentCulture,

                CultureInfo.CurrentUICulture)));

The querystring started to work...

(Sorry but I don't know how to format code on github comments..., mine never works)

hishamco commented 5 months ago

AFAIK the QueryStringRequestCultureProvider works well with the Blazor Server