🐛 Fix localization

This commit is contained in:
LittleSheep 2025-05-08 23:04:21 +08:00
parent d70b081752
commit b370c69670
4 changed files with 8 additions and 37 deletions

View File

@ -1,3 +1,4 @@
using System.Globalization;
using DysonNetwork.Sphere.Activity; using DysonNetwork.Sphere.Activity;
using DysonNetwork.Sphere.Connection; using DysonNetwork.Sphere.Connection;
using DysonNetwork.Sphere.Resources; using DysonNetwork.Sphere.Resources;
@ -11,10 +12,10 @@ namespace DysonNetwork.Sphere.Account;
public class AccountEventService( public class AccountEventService(
AppDatabase db, AppDatabase db,
AccountService acc,
ActivityService act, ActivityService act,
WebSocketService ws, WebSocketService ws,
IMemoryCache cache IMemoryCache cache,
IStringLocalizer<Localization.AccountEventResource> localizer
) )
{ {
private static readonly Random Random = new(); private static readonly Random Random = new();
@ -120,8 +121,10 @@ public class AccountEventService(
public async Task<CheckInResult> CheckInDaily(Account user) public async Task<CheckInResult> CheckInDaily(Account user)
{ {
var localizer = AccountService.GetEventLocalizer(user.Language); var cultureInfo = new CultureInfo(user.Language, false);
CultureInfo.CurrentCulture = cultureInfo;
CultureInfo.CurrentUICulture = cultureInfo;
// Generate 2 positive tips // Generate 2 positive tips
var positiveIndices = Enumerable.Range(1, FortuneTipCount) var positiveIndices = Enumerable.Range(1, FortuneTipCount)
.OrderBy(_ => Random.Next()) .OrderBy(_ => Random.Next())

View File

@ -40,32 +40,4 @@ public class AccountService(
return null; return null;
} }
public static IStringLocalizer GetEventLocalizer(string language)
{
var culture = new CultureInfo(language, false);
var originalCulture = CultureInfo.CurrentCulture;
try
{
// Set the desired culture
CultureInfo.CurrentUICulture = culture;
CultureInfo.CurrentCulture = culture;
// Now create the localizer
var localizer = new ResourceManagerStringLocalizerFactory(
new Microsoft.Extensions.Options.OptionsWrapper<LocalizationOptions>(new LocalizationOptions
{ ResourcesPath = "Resources" }),
NullLoggerFactory.Instance
).Create(typeof(AccountEventResource));
return localizer;
}
finally
{
CultureInfo.CurrentCulture = originalCulture;
CultureInfo.CurrentUICulture = originalCulture;
}
}
} }

View File

@ -69,7 +69,7 @@ public class AppDatabase(
optionsBuilder.UseNpgsql( optionsBuilder.UseNpgsql(
dataSource, dataSource,
opt => opt.UseNodaTime() opt => opt.UseNodaTime().UseQuerySplittingBehavior(QuerySplittingBehavior.SplitQuery)
).UseSnakeCaseNamingConvention(); ).UseSnakeCaseNamingConvention();
optionsBuilder.UseAsyncSeeding(async (context, _, cancellationToken) => optionsBuilder.UseAsyncSeeding(async (context, _, cancellationToken) =>

View File

@ -64,12 +64,8 @@ builder.Services.Configure<RequestLocalizationOptions>(options => {
new CultureInfo("zh-hans"), new CultureInfo("zh-hans"),
}; };
options.DefaultRequestCulture = new RequestCulture("en-us");
options.SupportedCultures = supportedCultures; options.SupportedCultures = supportedCultures;
options.SupportedUICultures = supportedCultures; options.SupportedUICultures = supportedCultures;
// TODO parse user token
options.AddInitialRequestCultureProvider(new CustomRequestCultureProvider(async context => await Task.FromResult(new ProviderCultureResult("zh-hans"))));
}); });
// Other pipelines // Other pipelines