♻️ Replaced the SetCultureInfo with the new localization engine

This commit is contained in:
2026-02-05 16:28:31 +08:00
parent 3c6ccba74f
commit 0051740cb0
10 changed files with 60 additions and 106 deletions

View File

@@ -302,9 +302,9 @@ public class AccountEventService(
private const string CheckInLockKey = "checkin:lock:";
public async Task<SnCheckInResult> CheckInDaily(SnAccount user, Instant? backdated = null)
public async Task<SnCheckInResult> CheckInDaily(SnAccount account, Instant? backdated = null)
{
var lockKey = $"{CheckInLockKey}{user.Id}";
var lockKey = $"{CheckInLockKey}{account.Id}";
try
{
@@ -322,12 +322,9 @@ public class AccountEventService(
await using var lockObj =
await cache.AcquireLockAsync(lockKey, TimeSpan.FromMinutes(1), TimeSpan.FromSeconds(5)) ??
throw new InvalidOperationException("Check-in was in progress.");
var cultureInfo = new CultureInfo(user.Language, false);
CultureInfo.CurrentCulture = cultureInfo;
CultureInfo.CurrentUICulture = cultureInfo;
var accountProfile = await db.AccountProfiles
.Where(x => x.AccountId == user.Id)
.Where(x => x.AccountId == account.Id)
.Select(x => new { x.Birthday, x.TimeZone })
.FirstOrDefaultAsync();
@@ -360,7 +357,7 @@ public class AccountEventService(
{
IsPositive = true,
Title = localizer.Get("fortuneTipSpecialTitleBirthday"),
Content = localizer.Get("fortuneTipSpecialContentBirthday", args: new { user.Nick }),
Content = localizer.Get("fortuneTipSpecialContentBirthday", args: new { account.Nick }),
}
];
}
@@ -374,8 +371,8 @@ public class AccountEventService(
tips = positiveIndices.Select(index => new CheckInFortuneTip
{
IsPositive = true,
Title = localizer.Get($"fortuneTipPositiveTitle{index}"),
Content = localizer.Get($"fortuneTipPositiveContent{index}")
Title = localizer.Get($"fortuneTipPositiveTitle{index}", account.Language),
Content = localizer.Get($"fortuneTipPositiveContent{index}", account.Language)
}).ToList();
// Generate 2 negative tips
@@ -387,8 +384,8 @@ public class AccountEventService(
tips.AddRange(negativeIndices.Select(index => new CheckInFortuneTip
{
IsPositive = false,
Title = localizer.Get($"fortuneTipNegativeTitle{index}"),
Content = localizer.Get($"fortuneTipNegativeContent{index}")
Title = localizer.Get($"fortuneTipNegativeTitle{index}", account.Language),
Content = localizer.Get($"fortuneTipNegativeContent{index}", account.Language)
}));
// The 5 is specialized, keep it alone.
@@ -409,7 +406,7 @@ public class AccountEventService(
{
Tips = tips,
Level = checkInLevel,
AccountId = user.Id,
AccountId = account.Id,
RewardExperience = 100,
RewardPoints = backdated.HasValue ? null : 10,
BackdatedFrom = backdated.HasValue ? SystemClock.Instance.GetCurrentInstant() : null,
@@ -421,7 +418,7 @@ public class AccountEventService(
if (result.RewardPoints.HasValue)
await payment.CreateTransactionWithAccount(
null,
user.Id.ToString(),
account.Id.ToString(),
WalletCurrency.SourcePoint,
result.RewardPoints.Value.ToString(CultureInfo.InvariantCulture),
$"Check-in reward on {now:yyyy/MM/dd}"
@@ -439,7 +436,7 @@ public class AccountEventService(
"check-in",
$"Check-in reward on {now:yyyy/MM/dd}",
result.RewardExperience.Value,
user.Id
account.Id
);
// The lock will be automatically released by the await using statement

View File

@@ -34,18 +34,6 @@ public class AccountService(
INatsConnection nats
)
{
public static void SetCultureInfo(SnAccount account)
{
SetCultureInfo(account.Language);
}
public static void SetCultureInfo(string? languageCode)
{
var info = new CultureInfo(languageCode ?? "en-us", false);
CultureInfo.CurrentCulture = info;
CultureInfo.CurrentUICulture = info;
}
public const string AccountCachePrefix = "account:";
public async Task PurgeAccountCache(SnAccount account)
@@ -123,7 +111,7 @@ public class AccountService(
).CountAsync();
if (dupeEmailCount > 0)
throw new InvalidOperationException("Account email has already been used.");
var account = new SnAccount
{
Name = name,
@@ -432,8 +420,8 @@ public class AccountService(
Notification = new PushNotification
{
Topic = "auth.verification",
Title = localizer.Get("authCodeTitle", account.Language),
Body = localizer.Get("authCodeBody", locale: account.Language, args: new { code }),
Title = localizer.Get("authCodeTitle", account.Language),
Body = localizer.Get("authCodeBody", locale: account.Language, args: new { code }),
IsSavable = false
}
}
@@ -554,7 +542,7 @@ public class AccountService(
{
if (!await IsDeviceActive(session.ClientId.Value))
await pusher.UnsubscribePushNotificationsAsync(new UnsubscribePushNotificationsRequest()
{ DeviceId = session.Client!.DeviceId }
{ DeviceId = session.Client!.DeviceId }
);
}
@@ -790,11 +778,11 @@ public class AccountService(
{
var accountIds = accounts.Select(a => a.Id).ToList();
var subscriptions = await remoteSubscription.GetPerkSubscriptions(accountIds);
var subscriptionDict = subscriptions
.Where(s => s != null)
.ToDictionary(
s => Guid.Parse(s.AccountId),
s => Guid.Parse(s.AccountId),
s => SnWalletSubscription.FromProtoValue(s).ToReference()
);

View File

@@ -87,7 +87,6 @@ public class MagicSpellService(
.Where(a => a.Id == spell.AccountId)
.Select(a => a.Language)
.FirstOrDefaultAsync();
AccountService.SetCultureInfo(accountLanguage);
try
{

View File

@@ -2,7 +2,6 @@ using System.ComponentModel.DataAnnotations;
using Microsoft.AspNetCore.Mvc;
using NodaTime;
using Microsoft.EntityFrameworkCore;
using DysonNetwork.Pass.Localization;
using DysonNetwork.Shared.Geometry;
using DysonNetwork.Shared.Proto;
using DysonNetwork.Shared.Localization;
@@ -31,8 +30,8 @@ public class AuthController(
public class ChallengeRequest
{
[Required] public Shared.Models.ClientPlatform Platform { get; set; }
[Required] [MaxLength(256)] public string Account { get; set; } = null!;
[Required] [MaxLength(512)] public string DeviceId { get; set; } = null!;
[Required][MaxLength(256)] public string Account { get; set; } = null!;
[Required][MaxLength(512)] public string DeviceId { get; set; } = null!;
[MaxLength(1024)] public string? DeviceName { get; set; }
public List<string> Audiences { get; set; } = [];
public List<string> Scopes { get; set; } = [];
@@ -231,16 +230,15 @@ public class AuthController(
if (challenge.StepRemain == 0)
{
AccountService.SetCultureInfo(challenge.Account);
await pusher.SendPushNotificationToUserAsync(new SendPushNotificationToUserRequest
{
Notification = new PushNotification
{
Notification = new PushNotification
{
Topic = "auth.login",
Title = localizer.Get("newLoginTitle", challenge.Account.Language),
Body = localizer.Get("newLoginBody", locale: challenge.Account.Language, args: new { deviceName = challenge.DeviceName ?? "unknown", ipAddress = challenge.IpAddress ?? "unknown" }),
IsSavable = true
},
Topic = "auth.login",
Title = localizer.Get("newLoginTitle", challenge.Account.Language),
Body = localizer.Get("newLoginBody", locale: challenge.Account.Language, args: new { deviceName = challenge.DeviceName ?? "unknown", ipAddress = challenge.IpAddress ?? "unknown" }),
IsSavable = true
},
UserId = challenge.AccountId.ToString()
});
als.CreateActionLogFromRequest(ActionLogType.NewLogin,
@@ -270,7 +268,7 @@ public class AuthController(
public class NewSessionRequest
{
[Required] [MaxLength(512)] public string DeviceId { get; set; } = null!;
[Required][MaxLength(512)] public string DeviceId { get; set; } = null!;
[MaxLength(1024)] public string? DeviceName { get; set; }
[Required] public Shared.Models.ClientPlatform Platform { get; set; }
public Instant? ExpiredAt { get; set; }
@@ -357,4 +355,4 @@ public class AuthController(
return Ok(new TokenExchangeResponse { Token = tk });
}
}
}

View File

@@ -17,7 +17,7 @@ public class RealmService(
)
{
private const string CacheKeyPrefix = "account:realms:";
public async Task<List<Guid>> GetUserRealms(Guid accountId)
{
var cacheKey = $"{CacheKeyPrefix}{accountId}";
@@ -34,19 +34,17 @@ public class RealmService(
// Cache the result for 5 minutes
await cache.SetAsync(cacheKey, realms, TimeSpan.FromMinutes(5));
return realms;
}
public async Task SendInviteNotify(SnRealmMember member)
{
var account = await db.Accounts
.Include(a => a.Profile)
.FirstOrDefaultAsync(a => a.Id == member.AccountId);
if (account == null) throw new InvalidOperationException("Account not found");
CultureService.SetCultureInfo(account.Language);
await pusher.SendPushNotificationToUserAsync(
new SendPushNotificationToUserRequest

View File

@@ -118,8 +118,6 @@ public class RealmServiceGrpc(
if (account == null) throw new RpcException(new Status(StatusCode.NotFound, "Account not found"));
CultureService.SetCultureInfo(account.Language);
await pusher.SendPushNotificationToUserAsync(
new SendPushNotificationToUserRequest
{
@@ -188,4 +186,4 @@ public class RealmServiceGrpc(
return response;
}
}
}