♻️ Refactored localization service

This commit is contained in:
2026-02-04 23:59:41 +08:00
parent c1669286f4
commit 9b6a62ec66
30 changed files with 530 additions and 369 deletions

View File

@@ -95,4 +95,8 @@
<ProjectReference Include="..\DysonNetwork.Shared\DysonNetwork.Shared.csproj" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Resources\Locales\*.json" />
</ItemGroup>
</Project>

View File

@@ -7,7 +7,7 @@ using DysonNetwork.Sphere.Localization;
using DysonNetwork.Sphere.Publisher;
using DysonNetwork.Sphere.ActivityPub;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Localization;
using DysonNetwork.Shared.Localization;
using NodaTime;
using Markdig;
using AngleSharp.Html.Parser;
@@ -19,7 +19,7 @@ namespace DysonNetwork.Sphere.Post;
public partial class PostService(
AppDatabase db,
IStringLocalizer<NotificationResource> localizer,
ILocalizationService localizer,
IServiceScopeFactory factory,
FlushBufferService flushBuffer,
ICacheService cache,
@@ -124,9 +124,9 @@ public partial class PostService(
? string.Concat(post.Content.AsSpan(0, 97), "...")
: post.Content;
var title = post.Title ?? (post.Content?.Length >= 10 ? post.Content[..10] + "..." : post.Content);
title ??= localizer["PostOnlyMedia"];
title ??= localizer.Get("postOnlyMedia");
if (string.IsNullOrWhiteSpace(content))
content = localizer["PostOnlyMedia"];
content = localizer.Get("postOnlyMedia");
return (title, content);
}
@@ -229,7 +229,7 @@ public partial class PostService(
Notification = new PushNotification
{
Topic = "post.replies",
Title = localizer["PostReplyTitle", sender!.Nick],
Title = localizer.Get("postReplyTitle", args: new { senderNick = sender!.Nick }),
Body = ChopPostForNotification(post).content,
IsSavable = true,
ActionUri = $"/posts/{post.Id}"
@@ -703,11 +703,10 @@ public partial class PostService(
Notification = new PushNotification
{
Topic = "posts.reactions.new",
Title = localizer["PostReactTitle", sender.Nick],
Title = localizer.Get("postReactTitle", args: new { senderNick = sender.Nick }),
Body = string.IsNullOrWhiteSpace(post.Title)
? localizer["PostReactBody", sender.Nick, reaction.Symbol]
: localizer["PostReactContentBody", sender.Nick, reaction.Symbol,
post.Title],
? localizer.Get("postReactBody", args: new { senderNick = sender.Nick, reaction.Symbol })
: localizer.Get("postReactContentBody", args: new { senderNick = sender.Nick, reaction.Symbol, post.Title }),
IsSavable = true,
ActionUri = $"/posts/{post.Id}"
}
@@ -1157,11 +1156,10 @@ public partial class PostService(
Notification = new PushNotification
{
Topic = "posts.awards.new",
Title = localizer["PostAwardedTitle", sender.Nick],
Title = localizer.Get("postAwardedTitle", args: new { senderNick = sender.Nick }),
Body = string.IsNullOrWhiteSpace(post.Title)
? localizer["PostAwardedBody", sender.Nick, amount]
: localizer["PostAwardedContentBody", sender.Nick, amount,
post.Title],
? localizer.Get("postAwardedBody", args: new { senderNick = sender.Nick, amount })
: localizer.Get("postAwardedContentBody", args: new { senderNick = sender.Nick, amount, post.Title }),
IsSavable = true,
ActionUri = $"/posts/{post.Id}"
}

View File

@@ -4,14 +4,14 @@ using DysonNetwork.Shared.Models;
using DysonNetwork.Shared.Proto;
using DysonNetwork.Sphere.Localization;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Localization;
using DysonNetwork.Shared.Localization;
namespace DysonNetwork.Sphere.Publisher;
public class PublisherSubscriptionService(
AppDatabase db,
Post.PostService ps,
IStringLocalizer<NotificationResource> localizer,
ILocalizationService localizer,
ICacheService cache,
RingService.RingServiceClient pusher,
AccountService.AccountServiceClient accounts
@@ -118,7 +118,7 @@ public class PublisherSubscriptionService(
var notification = new PushNotification
{
Topic = "posts.new",
Title = localizer["PostSubscriptionTitle", post.Publisher!.Nick, title],
Title = localizer.Get("postSubscriptionTitle", args: new { publisherNick = post.Publisher!.Nick, title }),
Body = message,
Meta = GrpcTypeHelper.ConvertObjectToByteString(data),
IsSavable = true,

View File

@@ -0,0 +1,13 @@
{
"postOnlyMedia": "Media only",
"postReplyTitle": "{user} replied to your post",
"postReactTitle": "{user} reacted to your post",
"postReactBody": "{user} reacted with {reaction}",
"postReactContentBody": "{user} reacted with {reaction} to \"{title}\"",
"postAwardedTitle": "{user} awarded your post",
"postAwardedBody": "{user} awarded {amount} points",
"postAwardedContentBody": "{user} awarded {amount} points to \"{title}\"",
"postSubscriptionTitle": "{publisher}: {title}",
"realmInviteTitle": "Realm Invitation",
"realmInviteBody": "You have been invited to join {realm}"
}

View File

@@ -0,0 +1,13 @@
{
"postOnlyMedia": "仅媒体",
"postReplyTitle": "{user} 回复了您的帖子",
"postReactTitle": "{user} 对您的帖子做出了反应",
"postReactBody": "{user} 用 {reaction} 做出了反应",
"postReactContentBody": "{user} 用 {reaction} 对 \"{title}\" 做出了反应",
"postAwardedTitle": "{user} 打赏了您的帖子",
"postAwardedBody": "{user} 打赏了 {amount} 积分",
"postAwardedContentBody": "{user} 打赏了 {amount} 积分给 \"{title}\"",
"postSubscriptionTitle": "{publisher}: {title}",
"realmInviteTitle": "领域邀请",
"realmInviteBody": "您被邀请加入 {realm}"
}

View File

@@ -49,4 +49,4 @@ public class TranslationController(ITranslationProvider provider, ICacheService
return result;
}
}
}