Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Blazor Localization does not work with InteractiveServer #53707

Closed
1 task done
aco-mreble opened this issue Jan 30, 2024 · 7 comments
Closed
1 task done

Blazor Localization does not work with InteractiveServer #53707

aco-mreble opened this issue Jan 30, 2024 · 7 comments
Assignees
Labels
area-blazor Includes: Blazor, Razor Components feature-localization investigate Needs: Repro Indicates that the team needs a repro project to continue the investigation on this issue

Comments

@aco-mreble
Copy link

aco-mreble commented Jan 30, 2024

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

When using <Routes @rendermode="InteractiveServer"/> in pre-rendering the correct locale from the browser is shown but after pre-rendering succceded the Fallback Locale is set again. If you disable pre-rendering the fallback locale will be set all the times

Expected Behavior

Blazor should set the correct locale for users

Steps To Reproduce

Create new Blazor Web App:

dotnet new blazor -o Test

in Program.cs add

builder.Services.AddLocalization();

and

app.UseRequestLocalization(["en-US", "de-DE"]);

In Components/App.razor change <Routes /> to

<Routes @rendermode="InteractiveServer"/>

for testing with prerender or

<Routes @rendermode="new InteractiveServerRenderMode(prerender:false)"/>

for testing without prerender.

In Components/Pages/Home.razor add this line below the page title

@(CultureInfo.CurrentCulture)

Then go to the website e.g. with Edge and go to Developer Tools -> Sensors -> Location and set it to "Berlin". In prerender: true some milliseconds you see the correct one, on prerender: false its allways the wrong one (en-US)

Exceptions (if any)

No response

.NET Version

8.0.101

Anything else?

Using Edge Version 121.0.2277.83 (Official build) (64-bit)

I believe I've pinpointed the issue: During the debugging of the RequestLocalizationMiddleware.Invoke method, I observed that each HttpRequest is sent with the appropriate Accept-Language header. However, the final call to this method targets http://localhost:XXXX/_blazor, where the header is unexpectedly set to en-US,en;q=0.9,de;q=0.8. I'm unsure of the cause, but since this is the last invocation of the method during a user's page request, the CultureInfo.CurrentCulture ends up being reset to en-US.

@dotnet-issue-labeler dotnet-issue-labeler bot added the area-blazor Includes: Blazor, Razor Components label Jan 30, 2024
@aco-mreble aco-mreble changed the title Blazor Localization does not work with SSR Blazor Localization does not work with InteractiveServer Jan 30, 2024
@mkArtakMSFT mkArtakMSFT added investigate Needs: Repro Indicates that the team needs a repro project to continue the investigation on this issue labels Jan 31, 2024
@ghost
Copy link

ghost commented Jan 31, 2024

Thank you for filing this issue. In order for us to investigate this issue, please provide a minimal repro project that illustrates the problem without unnecessary code. Please share with us in a public GitHub repo because we cannot open ZIP attachments, and don't include any confidential content.

@ghost ghost added the Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue. label Jan 31, 2024
@aco-mreble
Copy link
Author

@ghost ghost added Needs: Attention 👋 This issue needs the attention of a contributor, typically because the OP has provided an update. and removed Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue. labels Feb 1, 2024
@guardrex
Copy link
Contributor

guardrex commented Feb 6, 2024

I'm going to keep an 👁️ on this for possible docs work, even if only a temporary call out is needed for a framework 😈 during the 8.0 era.

@mkArtakMSFT mkArtakMSFT removed the Needs: Attention 👋 This issue needs the attention of a contributor, typically because the OP has provided an update. label Feb 13, 2024
@mkArtakMSFT mkArtakMSFT modified the milestone: .NET 9 Planning Feb 13, 2024
@mkArtakMSFT
Copy link
Member

@halter73 could you please try to validate this? Thanks!

@halter73
Copy link
Member

halter73 commented Apr 5, 2024

This appears to be a bug, or at least a shortcoming, of location override using "Sensors" pane in the Chrome/Edge dev tools as described in https://www.aleksandrhovhannisyan.com/blog/changing-locale-in-chrome-with-dev-tools/. I think it was primarily intended to override the location provided by JS Sensor APIs as the name implies, but it happens to change some (but not all) of the Accept-Language headers.

If you look at request headers the network tab in the developer tab, you'll see that it aligns with the Accept-Language reported by the middleware in the repro project.

This is not a bug in Blazor though. That's just what happens if you override the language using the "Sensors" pane. If instead, you go to edge://settings/languages, add "German (Germany) - Deutsch (Deutschland)" as a language and move it to the top, the Accept-Language headers should be correct, and everything should work as expected.

Add German (Germany)
German (Germany) at the top
de-DE Output

@guardrex I don't know if you want to include anything about this in our docs. Do they say anything about how to test localization? Maybe we could add something to https://learn.microsoft.com/aspnet/core/fundamentals/troubleshoot-aspnet-core-localization

@halter73 halter73 closed this as completed Apr 5, 2024
@guardrex
Copy link
Contributor

guardrex commented Apr 5, 2024

No, we don't in the Blazor glob/loc doc. We do cross-link to the main doc set article (IIRC). I'll take a closer look at this on Monday.

@guardrex
Copy link
Contributor

guardrex commented Apr 6, 2024

BTW tho @aco-mreble ... Hisham and I have worked on the article, and we have two candidates for BWA glob/loc. I favor continuing to use local storage on the client, while Hisham likes hacking the loc cookie client-side. The article currently covers the approach that I favor.

https://learn.microsoft.com/en-us/aspnet/core/blazor/globalization-localization?view=aspnetcore-8.0

The bit that pertains to BWAs is here ...

https://learn.microsoft.com/en-us/aspnet/core/blazor/globalization-localization?view=aspnetcore-8.0#dynamically-set-the-culture-in-a-blazor-web-app-by-user-preference

If you're only trying to use interactive SSR, see this bit of the article ...

https://learn.microsoft.com/en-us/aspnet/core/blazor/globalization-localization?view=aspnetcore-8.0#dynamically-set-the-server-side-culture-by-user-preference

... and WRT to the discussion here about prerendering behavior, I don't have any knowledge on it. I will take a closer look on Monday. I'm off the weekend now 🎉.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-blazor Includes: Blazor, Razor Components feature-localization investigate Needs: Repro Indicates that the team needs a repro project to continue the investigation on this issue
Projects
None yet
Development

No branches or pull requests

5 participants
@halter73 @guardrex @mkArtakMSFT @aco-mreble and others