fix culture

This commit is contained in:
2025-12-07 12:41:04 +08:00
parent 8aca372fc1
commit d91954e331
10 changed files with 89 additions and 24 deletions

View File

@@ -27,7 +27,7 @@ builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.
// ע<><EFBFBD>ػ<EFBFBD><D8BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD><CAB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>վ<EFBFBD><D5BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ŀ¼<C4BF><C2BC> "Localization" <20><>ע<EFBFBD><D7A2><EFBFBD><EFBFBD><EFBFBD>ڼ<EFBFBD><DABC>ؾ<EFBFBD>̬<EFBFBD>ļ<EFBFBD><C4BC><EFBFBD> HttpClient<6E><74>
builder.Services.AddScoped<IStringLocalizerFactory>(sp =>
new JsonStringLocalizerFactory("Localization", sp.GetRequiredService<HttpClient>()));
new JsonStringLocalizerFactory("localization", sp.GetRequiredService<HttpClient>()));
// ע<><D7A2>IStringLocalizer
builder.Services.AddTransient(typeof(IStringLocalizer<>), typeof(StringLocalizer<>));

View File

@@ -16,7 +16,7 @@ namespace Atomx.Admin.Client.Services
// 在 Blazor WebAssembly 场景下,会使用注入的 HttpClient 从 wwwroot/Localization/{culture}.json 获取资源
public JsonStringLocalizer(string resourcesPath, HttpClient? httpClient = null)
{
_resourcesPath = (resourcesPath ?? "Localization").Trim('/'); // 规范化
_resourcesPath = (resourcesPath ?? "localization").Trim('/'); // 规范化
_httpClient = httpClient;
}

View File

@@ -1,5 +1,4 @@
using Microsoft.Extensions.Localization;
using System.Net.Http;
namespace Atomx.Admin.Client.Services
{
@@ -10,7 +9,7 @@ namespace Atomx.Admin.Client.Services
public JsonStringLocalizerFactory(string resourcesPath, HttpClient httpClient)
{
_resourcesPath = (resourcesPath ?? "Localization").Trim('/');
_resourcesPath = (resourcesPath ?? "localization").Trim('/');
_httpClient = httpClient;
}

View File

@@ -1,6 +1,5 @@
using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
using System.Globalization;
namespace Atomx.Admin.Client.Services
{
@@ -29,19 +28,6 @@ namespace Atomx.Admin.Client.Services
if (_currentLanguage != value)
{
_currentLanguage = value;
// 设置全局线程文化,确保 IStringLocalizer 等在随后的渲染中读取到新文化
try
{
var ci = new CultureInfo(value);
CultureInfo.DefaultThreadCurrentCulture = ci;
CultureInfo.DefaultThreadCurrentUICulture = ci;
}
catch
{
// 忽略无效 culture 字符串
}
OnLanguageChanged?.Invoke();
}
}
@@ -59,8 +45,10 @@ namespace Atomx.Admin.Client.Services
public async Task InitializeAsync()
{
// 尝试从本地存储获取保存的语言
Console.WriteLine("尝试从本地存储获取保存的语言 Initializing LanguageProvider...");
try
{
var savedLanguage = await _jsRuntime.InvokeAsync<string>("localStorage.getItem", "preferred-language");
if (!string.IsNullOrEmpty(savedLanguage) && SupportedLanguages.Contains(savedLanguage))
{
@@ -85,6 +73,7 @@ namespace Atomx.Admin.Client.Services
/// </summary>
public async Task ChangeLanguageAsync(string languageCode)
{
Console.WriteLine("切换语言 ChangeLanguageAsync to " + languageCode);
if (SupportedLanguages.Contains(languageCode) && CurrentLanguage != languageCode)
{
CurrentLanguage = languageCode;
@@ -99,7 +88,8 @@ namespace Atomx.Admin.Client.Services
// 忽略错误
}
// setter 已触发 OnLanguageChanged
// 通知语言已更改
OnLanguageChanged?.Invoke();
}
}