实现多语言切换
This commit is contained in:
@@ -1,9 +1,41 @@
|
||||
@inherits LayoutComponentBase
|
||||
@inject Atomx.Admin.Client.Services.ILocalizationProvider LocalizationProvider
|
||||
|
||||
<div style="min-height:100vh">
|
||||
@Body
|
||||
</div>
|
||||
|
||||
@code {
|
||||
protected override void OnInitialized()
|
||||
{
|
||||
if (LocalizationProvider != null)
|
||||
{
|
||||
LocalizationProvider.LanguageChanged += OnLanguageChanged;
|
||||
}
|
||||
}
|
||||
|
||||
private void OnLanguageChanged(object? sender, string culture)
|
||||
{
|
||||
_ = InvokeAsync(StateHasChanged);
|
||||
}
|
||||
|
||||
protected override async Task OnAfterRenderAsync(bool firstRender)
|
||||
{
|
||||
if (firstRender && LocalizationProvider != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
await LocalizationProvider.InitializeAsync();
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (LocalizationProvider != null)
|
||||
{
|
||||
LocalizationProvider.LanguageChanged -= OnLanguageChanged;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
@inherits LayoutComponentBase
|
||||
@inject ILogger<MainLayout> _logger
|
||||
@inject Atomx.Admin.Client.Services.ILocalizationProvider LocalizationProvider
|
||||
@inject NavigationManager Navigation
|
||||
|
||||
<ErrorBoundary>
|
||||
<ChildContent>
|
||||
@@ -86,10 +88,10 @@
|
||||
|
||||
private AvatarMenuItem[] AvatarMenuItems =>
|
||||
[
|
||||
new() { Key = "center", IconType = "user", Option = "通知消息"},
|
||||
new() { Key = "setting", IconType = "setting", Option ="修改资料" },
|
||||
new() { IsDivider = true },
|
||||
new() { Key = "logout", IconType = "logout", Option = "退出登录"}
|
||||
new() { Key = "center", IconType = "user", Option = "通知消息" },
|
||||
new() { Key = "setting", IconType = "setting", Option = "修改资料" },
|
||||
new() { IsDivider = true },
|
||||
new() { Key = "logout", IconType = "logout", Option = "退出登录" }
|
||||
];
|
||||
|
||||
public void HandleSelectUser(AntDesign.MenuItem item)
|
||||
@@ -131,5 +133,46 @@
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnInitialized()
|
||||
{
|
||||
if (LocalizationProvider != null)
|
||||
{
|
||||
LocalizationProvider.LanguageChanged += OnLanguageChanged;
|
||||
}
|
||||
}
|
||||
|
||||
private void OnLanguageChanged(object? sender, string culture)
|
||||
{
|
||||
// ensure UI updates on the SyncContext; small delay to let cache update
|
||||
_ = InvokeAsync(async () =>
|
||||
{
|
||||
await Task.Yield();
|
||||
try
|
||||
{
|
||||
// Force route and page components to remount and re-render translations
|
||||
Navigation.NavigateTo(Navigation.Uri, forceLoad: false);
|
||||
}
|
||||
catch { }
|
||||
});
|
||||
}
|
||||
|
||||
protected override async Task OnAfterRenderAsync(bool firstRender)
|
||||
{
|
||||
if (firstRender && LocalizationProvider != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
await LocalizationProvider.InitializeAsync();
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (LocalizationProvider != null)
|
||||
{
|
||||
LocalizationProvider.LanguageChanged -= OnLanguageChanged;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user