-
Notifications
You must be signed in to change notification settings - Fork 10.4k
Added Culture persistence between Client and Server #63144
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
Open
dariatiurina
wants to merge
8
commits into
dotnet:main
Choose a base branch
from
dariatiurina:26941-prerendering-wasm-localization
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
2053096
Implemented CultureStateProvider
dariatiurina d3330e1
Added opt-in and tests
dariatiurina 2f195f8
Clean-up
dariatiurina c0f92ba
Clean-up
dariatiurina 857a699
Test fix
dariatiurina 707e121
Fix test
dariatiurina 1f97466
Fix and feedback
dariatiurina 0acb3d9
Fix test and feedback
dariatiurina File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
using System.Globalization; | ||
|
||
namespace Microsoft.AspNetCore.Components.Forms; | ||
|
||
internal class CultureStateProvider | ||
{ | ||
protected string? _currentCultureName; | ||
protected string? _currentUICultureName; | ||
|
||
[PersistentState] | ||
public string? CurrentCultureName | ||
{ | ||
get => _currentCultureName; | ||
set => _currentCultureName = value; | ||
} | ||
|
||
[PersistentState] | ||
public string? CurrentUICultureName | ||
{ | ||
get => _currentUICultureName; | ||
set => _currentUICultureName = value; | ||
} | ||
|
||
/// <summary> | ||
/// Captures the current thread culture for persistence. | ||
/// Called from server-side during prerendering. | ||
/// </summary> | ||
public void CaptureCurrentCulture() | ||
{ | ||
CurrentCultureName = CultureInfo.CurrentCulture.Name; | ||
CurrentUICultureName = CultureInfo.CurrentUICulture.Name; | ||
} | ||
|
||
/// <summary> | ||
/// Applies the stored culture to the current thread. | ||
/// Called on WebAssembly side after hydration. | ||
/// </summary> | ||
public void ApplyStoredCulture() | ||
{ | ||
if (!string.IsNullOrEmpty(CurrentCultureName)) | ||
{ | ||
CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo(CurrentCultureName); | ||
} | ||
|
||
if (!string.IsNullOrEmpty(CurrentUICultureName)) | ||
{ | ||
CultureInfo.CurrentUICulture = CultureInfo.GetCultureInfo(CurrentUICultureName); | ||
ilonatommy marked this conversation as resolved.
Show resolved
Hide resolved
ilonatommy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
#nullable enable | ||
static Microsoft.Extensions.DependencyInjection.WebAssemblyRazorComponentsBuilderExtensions.EnforceServerCultureOnClient(this Microsoft.Extensions.DependencyInjection.IRazorComponentsBuilder! builder) -> Microsoft.Extensions.DependencyInjection.IRazorComponentsBuilder! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
src/Components/test/testassets/Components.WasmMinimal/Pages/CulturePersistence.razor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
@page "/persist-culture-state" | ||
@using System.Globalization | ||
@using Microsoft.AspNetCore.Components.Web | ||
@rendermode RenderMode.InteractiveWebAssembly | ||
|
||
<script> | ||
function start() { | ||
Blazor.start({ | ||
logLevel: 1 // LogLevel.Debug | ||
}); | ||
} | ||
</script> | ||
|
||
<script src="_framework/blazor.webassembly.js" autostart="false"></script> | ||
|
||
<h3>CulturePersistence</h3> | ||
|
||
<p id="culture-set">@CultureInfo.CurrentCulture</p> | ||
<p id="culture-ui-set">@CultureInfo.CurrentUICulture</p> | ||
|
||
@if(RendererInfo.IsInteractive) | ||
{ | ||
<p id="interactive">Interactive</p> | ||
} | ||
else | ||
{ | ||
<p id="prerender">Prerender</p> | ||
} | ||
|
||
<button id="start-blazor" onclick="start()">Start Blazor</button> | ||
|
||
@code { | ||
protected override void OnAfterRender(bool firstRender) | ||
{ | ||
if (firstRender) | ||
{ | ||
StateHasChanged(); | ||
} | ||
base.OnAfterRender(firstRender); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.