feat(notification): 通知标题和正文支持多语言

- 通知静态配置支持 title/body i18n
- 前端通知列表和详情页展示本地化内容
- 新增数据库迁移脚本
- 更新通知协议文档
This commit is contained in:
ZL-Q
2026-04-28 17:20:17 +08:00
parent b9617ae152
commit a940f2ea47
16 changed files with 601 additions and 213 deletions
+93 -10
View File
@@ -5,7 +5,12 @@ from uuid import UUID, uuid4
import pytest
from v1.notifications.service import NotificationService, _parse_payload
from v1.notifications.service import (
NotificationService,
_parse_payload,
resolve_i18n_text,
normalize_locale,
)
from v1.notifications.schemas import (
NotificationPayloadNone,
NotificationPayloadRoute,
@@ -39,8 +44,8 @@ class _FakeNotification:
*,
id: UUID,
type: str = "system",
title: str = "Test",
body: str = "Test body",
title: dict[str, str] | None = None,
body: dict[str, str] | None = None,
payload: dict | None = None,
status: str = "published",
deleted_at: datetime | None = None,
@@ -48,8 +53,8 @@ class _FakeNotification:
):
self.id = id
self.type = type
self.title = title
self.body = body
self.title = title or {"zh": "Test"}
self.body = body or {"zh": "Test body"}
self.payload = payload or {"action": "none"}
self.status = status
self.deleted_at = deleted_at
@@ -154,8 +159,8 @@ def _make_notification(
notification_id: UUID | None = None,
is_read: bool = False,
read_at: datetime | None = None,
title: str = "Test",
body: str = "Test body",
title: dict[str, str] | None = None,
body: dict[str, str] | None = None,
payload: dict | None = None,
status: str = "published",
deleted_at: datetime | None = None,
@@ -185,8 +190,12 @@ class TestListNotifications:
async def test_returns_only_user_a_notifications(
self, service: NotificationService, fake_repo: _FakeNotificationRepository
):
un_a, n_a = _make_notification(user_id=USER_A, title="A1")
un_b, n_b = _make_notification(user_id=USER_B, title="B1")
un_a, n_a = _make_notification(
user_id=USER_A, title={"zh": "A1"}, body={"zh": "A1 body"},
)
un_b, n_b = _make_notification(
user_id=USER_B, title={"zh": "B1"}, body={"zh": "B1 body"},
)
fake_repo.add_item(un_a, n_a)
fake_repo.add_item(un_b, n_b)
@@ -219,7 +228,9 @@ class TestListNotifications:
self, service: NotificationService, fake_repo: _FakeNotificationRepository
):
for i in range(3):
un, n = _make_notification(user_id=USER_A, title=f"N{i}")
un, n = _make_notification(
user_id=USER_A, title={"zh": f"N{i}"}, body={"zh": f"N{i} body"},
)
fake_repo.add_item(un, n)
result = await service.list_notifications(user_id=USER_A, limit=2)
@@ -383,3 +394,75 @@ class TestParsePayload:
assert payload.route == "/settings"
assert payload.entity_id is None
assert payload.tab is None
class TestResolveI18nText:
def test_exact_locale_match(self):
text = resolve_i18n_text({"zh": "你好", "en": "Hello"}, "en")
assert text == "Hello"
def test_falls_back_to_default(self):
text = resolve_i18n_text({"zh": "你好", "en": "Hello"}, "zh_Hant")
assert text == "你好"
def test_returns_empty_when_default_missing(self):
text = resolve_i18n_text({"en": "Hello"}, "zh_Hant")
assert text == ""
def test_empty_dict(self):
text = resolve_i18n_text({}, "en")
assert text == ""
class TestNormalizeLocale:
def test_known_locale_passthrough(self):
assert normalize_locale("zh") == "zh"
assert normalize_locale("zh_Hant") == "zh_Hant"
assert normalize_locale("en") == "en"
def test_none_returns_default(self):
assert normalize_locale(None) == "zh"
def test_zh_cn_maps_to_zh(self):
assert normalize_locale("zh_CN") == "zh"
assert normalize_locale("zh_Hans") == "zh"
def test_zh_tw_maps_to_hant(self):
assert normalize_locale("zh_TW") == "zh_Hant"
assert normalize_locale("zh-Hant") == "zh_Hant"
def test_unknown_returns_default(self):
assert normalize_locale("fr") == "zh"
assert normalize_locale("ja") == "zh"
class TestListNotificationsI18n:
@pytest.mark.asyncio
async def test_locale_en_returns_english(
self, service: NotificationService, fake_repo: _FakeNotificationRepository
):
un, n = _make_notification(
user_id=USER_A,
title={"zh": "你好", "en": "Hello"},
body={"zh": "正文", "en": "Body"},
)
fake_repo.add_item(un, n)
result = await service.list_notifications(user_id=USER_A, locale="en")
assert result.items[0].title == "Hello"
assert result.items[0].body == "Body"
@pytest.mark.asyncio
async def test_locale_zh_hant_falls_back_to_zh(
self, service: NotificationService, fake_repo: _FakeNotificationRepository
):
un, n = _make_notification(
user_id=USER_A,
title={"zh": "你好", "en": "Hello"},
body={"zh": "正文", "en": "Body"},
)
fake_repo.add_item(un, n)
result = await service.list_notifications(user_id=USER_A, locale="zh_Hant")
assert result.items[0].title == "你好"
assert result.items[0].body == "正文"
@@ -27,8 +27,12 @@ def test_load_static_notification_file_parses_valid_yaml(tmp_path: Path) -> None
version: 1
type: system
status: published
title: Welcome
body: Welcome to the app.
title:
zh: 欢迎
en: Welcome
body:
zh: 欢迎使用
en: Welcome to the app.
payload:
action: open_route
route: /points
@@ -43,6 +47,8 @@ def test_load_static_notification_file_parses_valid_yaml(tmp_path: Path) -> None
loaded = load_static_notification_file(file_path)
assert loaded.notification.source_key == "welcome_bonus"
assert loaded.notification.title == {"zh": "欢迎", "en": "Welcome"}
assert loaded.notification.body == {"zh": "欢迎使用", "en": "Welcome to the app."}
assert loaded.notification.payload.action == "open_route"
assert loaded.targets.mode == NotificationTargetMode.USER_IDS
assert len(loaded.targets.user_ids or []) == 1
@@ -58,8 +64,10 @@ def test_load_static_notification_file_parses_new_users(tmp_path: Path) -> None:
version: 1
type: system
status: published
title: Welcome
body: You got points.
title:
zh: 欢迎
body:
zh: 你好
payload:
action: open_route
route: /points
@@ -85,8 +93,10 @@ def test_load_static_notification_file_parses_exist_users(tmp_path: Path) -> Non
version: 1
type: system
status: published
title: Come back
body: We miss you.
title:
zh: 回来吧
body:
zh: 想你
payload:
action: none
targets:
@@ -110,8 +120,10 @@ def test_load_static_notification_file_parses_all_users(tmp_path: Path) -> None:
version: 1
type: system
status: published
title: Announcement
body: Maintenance at midnight.
title:
zh: 公告
body:
zh: 午夜维护
payload:
action: none
targets:
@@ -134,8 +146,10 @@ def test_load_static_notification_file_rejects_invalid_targets(tmp_path: Path) -
version: 1
type: system
status: published
title: Invalid
body: Invalid targets.
title:
zh: 无效
body:
zh: 无效
payload:
action: none
targets:
@@ -149,6 +163,88 @@ def test_load_static_notification_file_rejects_invalid_targets(tmp_path: Path) -
load_static_notification_file(file_path)
def test_load_static_notification_file_rejects_i18n_without_zh(
tmp_path: Path,
) -> None:
file_path = tmp_path / "missing_zh.yaml"
_write_yaml(
file_path,
"""
notification:
source_key: missing_zh
version: 1
type: system
status: published
title:
en: Welcome
body:
zh: 正文
payload:
action: none
targets:
mode: all_users
""",
)
with pytest.raises(ValueError, match="Invalid static notification data"):
load_static_notification_file(file_path)
def test_load_static_notification_file_rejects_empty_i18n_text(
tmp_path: Path,
) -> None:
file_path = tmp_path / "empty_i18n.yaml"
_write_yaml(
file_path,
"""
notification:
source_key: empty_i18n
version: 1
type: system
status: published
title:
zh: ""
body:
zh: 正文
payload:
action: none
targets:
mode: all_users
""",
)
with pytest.raises(ValueError, match="Invalid static notification data"):
load_static_notification_file(file_path)
def test_load_static_notification_file_rejects_unknown_i18n_locale(
tmp_path: Path,
) -> None:
file_path = tmp_path / "unknown_locale.yaml"
_write_yaml(
file_path,
"""
notification:
source_key: unknown_locale
version: 1
type: system
status: published
title:
zh: 标题
ja: タイトル
body:
zh: 正文
payload:
action: none
targets:
mode: all_users
""",
)
with pytest.raises(ValueError, match="Invalid static notification data"):
load_static_notification_file(file_path)
def test_load_static_notification_file_rejects_unknown_mode(tmp_path: Path) -> None:
file_path = tmp_path / "bad_mode.yaml"
_write_yaml(
@@ -159,8 +255,10 @@ def test_load_static_notification_file_rejects_unknown_mode(tmp_path: Path) -> N
version: 1
type: system
status: published
title: Bad
body: Bad mode.
title:
zh: 坏
body:
zh: 坏
payload:
action: none
targets:
@@ -184,8 +282,10 @@ def test_load_static_notification_file_rejects_new_users_with_user_ids(
version: 1
type: system
status: published
title: Bad
body: Bad.
title:
zh: 坏
body:
zh: 坏
payload:
action: none
targets:
@@ -211,8 +311,10 @@ def test_load_static_notification_file_rejects_user_ids_without_list(
version: 1
type: system
status: published
title: Bad
body: Bad.
title:
zh: 坏
body:
zh: 坏
payload:
action: none
targets:
@@ -235,8 +337,10 @@ def test_load_static_notification_documents_rejects_duplicate_source_key(
version: 1
type: system
status: published
title: First
body: First body.
title:
zh: 第一
body:
zh: 第一
payload:
action: none
targets:
@@ -251,8 +355,10 @@ def test_load_static_notification_documents_rejects_duplicate_source_key(
version: 1
type: system
status: published
title: Second
body: Second body.
title:
zh: 第二
body:
zh: 第二
payload:
action: none
targets:
@@ -275,8 +381,10 @@ def test_content_hash_changes_when_notification_changes(tmp_path: Path) -> None:
version: 1
type: system
status: published
title: Title A
body: Body A.
title:
zh: 标题A
body:
zh: 正文A
payload:
action: none
targets:
@@ -291,8 +399,10 @@ def test_content_hash_changes_when_notification_changes(tmp_path: Path) -> None:
version: 1
type: system
status: published
title: Title B
body: Body A.
title:
zh: 标题B
body:
zh: 正文A
payload:
action: none
targets:
@@ -319,8 +429,10 @@ def test_load_static_notification_file_supports_deleted_flag(tmp_path: Path) ->
type: system
status: revoked
deleted: true
title: Deleted
body: Deleted body.
title:
zh: 已删
body:
zh: 已删
payload:
action: none
targets: