mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-11-16 14:54:23 +08:00
fix(i18n): use English runtime text and localize system UI
This commit is contained in:
@@ -221,9 +221,9 @@ func TestChannel(c *gin.Context) {
|
||||
default:
|
||||
config = pkgpush.Config{Channel: channelCustom, URL: url}
|
||||
customPushReq := CustomPushRequest{
|
||||
Title: "通道测试通知",
|
||||
Content: "这是一条来自系统的消息通道连通性测试消息。",
|
||||
Description: "系统通道测试",
|
||||
Title: "Channel test notification",
|
||||
Content: "This is a system notification channel connectivity test.",
|
||||
Description: "System channel test",
|
||||
URL: "https://example.com",
|
||||
To: req.Target,
|
||||
}
|
||||
@@ -235,8 +235,8 @@ func TestChannel(c *gin.Context) {
|
||||
Config: config,
|
||||
Target: req.Target,
|
||||
Body: NotificationMessage{
|
||||
Title: "通道测试通知",
|
||||
Content: "这是一条来自系统的消息通道连通性测试消息。",
|
||||
Title: "Channel test notification",
|
||||
Content: "This is a system notification channel connectivity test.",
|
||||
Level: defaultLevelInfo,
|
||||
},
|
||||
Template: renderedJSON,
|
||||
|
||||
@@ -81,24 +81,24 @@ func init() {
|
||||
// Register custom webhook channel
|
||||
RegisterChannelDefinition(Definition{
|
||||
Type: channelCustom,
|
||||
Name: "自定义消息通道",
|
||||
Description: "使用自定义 HTTP POST 请求向外部 Webhook 发送数据。",
|
||||
Name: "Custom notification channel",
|
||||
Description: "Send data to an external webhook using a custom HTTP POST request.",
|
||||
Fields: []Field{
|
||||
{
|
||||
Key: KeyURL,
|
||||
Label: "请求地址",
|
||||
Label: "Request URL",
|
||||
Type: TypeText,
|
||||
Required: true,
|
||||
Placeholder: "在此填写完整的请求地址,必须使用 HTTPS 协议",
|
||||
Description: "接口请求的完整 HTTPS URL,例如 https://api.example.com/webhook",
|
||||
Placeholder: "Enter the complete request URL using HTTPS",
|
||||
Description: "Complete HTTPS endpoint URL, for example https://api.example.com/webhook",
|
||||
},
|
||||
{
|
||||
Key: KeyOther,
|
||||
Label: "请求体 (JSON)",
|
||||
Label: "Request body (JSON)",
|
||||
Type: TypeTextarea,
|
||||
Required: true,
|
||||
Placeholder: "在此输入请求体,支持模板变量,必须为合法的 JSON 格式",
|
||||
Description: "可使用的变量:$title, $description, $content, $url, $to。例如 {\"text\": \"$content\"}",
|
||||
Placeholder: "Enter a valid JSON request body; template variables are supported",
|
||||
Description: `Available variables: $title, $description, $content, $url, $to. Example: {"text": "$content"}`,
|
||||
},
|
||||
},
|
||||
})
|
||||
@@ -106,33 +106,33 @@ func init() {
|
||||
// Register Lark robot channel
|
||||
RegisterChannelDefinition(Definition{
|
||||
Type: channelLark,
|
||||
Name: "飞书群机器人",
|
||||
Description: "配置飞书群自定义机器人的 Webhook 接口投递。",
|
||||
Name: "Feishu group bot",
|
||||
Description: "Deliver notifications through a Feishu group bot webhook.",
|
||||
Fields: []Field{
|
||||
{
|
||||
Key: KeyURL,
|
||||
Label: "Webhook 地址",
|
||||
Label: "Webhook URL",
|
||||
Type: TypeText,
|
||||
Required: true,
|
||||
Placeholder: "https://open.feishu.cn/open-apis/bot/v2/hook/YOUR_TOKEN",
|
||||
Description: "从飞书群机器人设置中复制 of Webhook URL",
|
||||
Description: "Copy the webhook URL from the Feishu group bot settings",
|
||||
// Note: using 'of' was in feishu.go, let's keep original wording or fix it
|
||||
},
|
||||
{
|
||||
Key: KeyToken,
|
||||
Label: "签名校验密钥 (Secret) (可选)",
|
||||
Label: "Signing secret (optional)",
|
||||
Type: TypeText,
|
||||
Required: false,
|
||||
Placeholder: "可选,若机器人启用了安全设置中的签名校验,请在此输入",
|
||||
Description: "飞书群机器人安全设置中的签名校验 Key",
|
||||
Placeholder: "Enter the secret if signature verification is enabled for the bot",
|
||||
Description: "Signature verification key from the Feishu group bot security settings",
|
||||
},
|
||||
{
|
||||
Key: KeyOther,
|
||||
Label: "自定义卡片 JSON 模版 (可选)",
|
||||
Label: "Custom card JSON template (optional)",
|
||||
Type: TypeTextarea,
|
||||
Required: false,
|
||||
Placeholder: "可选,留空则默认使用系统内置的精美互动卡片",
|
||||
Description: "若填写,必须是合法的飞书卡片 JSON 格式",
|
||||
Placeholder: "Leave empty to use the built-in interactive card",
|
||||
Description: "If specified, this must be valid Feishu card JSON",
|
||||
},
|
||||
},
|
||||
})
|
||||
@@ -140,32 +140,32 @@ func init() {
|
||||
// Register Telegram channel
|
||||
RegisterChannelDefinition(Definition{
|
||||
Type: channelTelegram,
|
||||
Name: "Telegram 机器人",
|
||||
Description: "配置 Telegram 机器人推送消息。",
|
||||
Name: "Telegram bot",
|
||||
Description: "Configure Telegram bot notifications.",
|
||||
Fields: []Field{
|
||||
{
|
||||
Key: KeyURL,
|
||||
Label: "API 基础地址 (可选)",
|
||||
Label: "API base URL (optional)",
|
||||
Type: TypeText,
|
||||
Required: false,
|
||||
Placeholder: "https://api.telegram.org",
|
||||
Description: "接口请求的 HTTPS 基础地址,留空默认为 https://api.telegram.org",
|
||||
Description: "HTTPS API base URL; defaults to https://api.telegram.org",
|
||||
},
|
||||
{
|
||||
Key: KeyToken,
|
||||
Label: "机器人 Token (Bot Token)",
|
||||
Label: "Bot token",
|
||||
Type: TypePassword,
|
||||
Required: true,
|
||||
Placeholder: "在此输入 Telegram 机器人的 Bot Token",
|
||||
Description: "通过 BotFather 申请到的机器人 Access Token",
|
||||
Placeholder: "Enter the Telegram bot token",
|
||||
Description: "Bot access token issued by BotFather",
|
||||
},
|
||||
{
|
||||
Key: KeyOther,
|
||||
Label: "默认会话 ID (Chat ID) (可选)",
|
||||
Label: "Default chat ID (optional)",
|
||||
Type: TypeText,
|
||||
Required: false,
|
||||
Placeholder: "例如 -100123456789 或 @channel_name",
|
||||
Description: "默认的消息接收 Chat ID。如果通知事件中未配置 targets,将推送到此 ID",
|
||||
Placeholder: "For example -100123456789 or @channel_name",
|
||||
Description: "Default recipient chat ID, used when the notification event has no targets",
|
||||
},
|
||||
},
|
||||
})
|
||||
@@ -173,8 +173,8 @@ func init() {
|
||||
// Register Email channel
|
||||
RegisterChannelDefinition(Definition{
|
||||
Type: channelEmail,
|
||||
Name: "邮件推送通道",
|
||||
Description: "邮件推送通道直接使用系统全局 SMTP 设置进行发送,无需在此填写服务器配置。",
|
||||
Name: "Email notification channel",
|
||||
Description: "Email notifications use the global SMTP settings; no server configuration is required here.",
|
||||
Fields: []Field{},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -15,13 +15,13 @@ import (
|
||||
// AdminLogin is the metadata definition for the admin login event.
|
||||
var AdminLogin = push.EventMetadata{
|
||||
Key: "admin_login",
|
||||
Name: "管理员登录",
|
||||
Name: "Administrator login",
|
||||
DefaultTemplate: push.NotificationMessage{
|
||||
Title: "管理员登录提醒",
|
||||
Content: "管理员 {{user.username}} 于 {{time}} 从 IP {{ip}} 登录系统。",
|
||||
Title: "Administrator login notification",
|
||||
Content: "Administrator {{user.username}} signed in at {{time}} from IP {{ip}}.",
|
||||
Level: "INFO",
|
||||
},
|
||||
Description: "当管理员成功登录系统时触发此通知",
|
||||
Description: "Triggered when an administrator signs in successfully",
|
||||
}
|
||||
|
||||
func handleAdminLogin(ctx context.Context, event listener.AdminLoggedIn) {
|
||||
|
||||
@@ -138,7 +138,7 @@ func TestAdminLoginPushIntegration(t *testing.T) {
|
||||
|
||||
assert.Equal(t, AdminLogin.Key, payload.EventKey)
|
||||
assert.Equal(t, "ops_team", payload.Target)
|
||||
assert.Equal(t, "管理员登录提醒", payload.Body.Title)
|
||||
assert.Equal(t, "Administrator login notification", payload.Body.Title)
|
||||
assert.Contains(t, payload.Body.Content, "super_admin")
|
||||
assert.Contains(t, payload.Body.Content, "203.0.113.42")
|
||||
})
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
// Copyright 2026 Arctel.net
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package push
|
||||
|
||||
import (
|
||||
"github.com/Rain-kl/Wavelet/internal/infra/task"
|
||||
"github.com/Rain-kl/Wavelet/internal/model"
|
||||
"github.com/Rain-kl/Wavelet/pkg/i18n"
|
||||
)
|
||||
|
||||
func localizeEventMetadata(locale string, meta EventMetadata) EventMetadata {
|
||||
meta.Name = i18n.Translate(locale, meta.Name)
|
||||
meta.Description = i18n.Translate(locale, meta.Description)
|
||||
meta.DefaultTemplate.Title = i18n.Translate(locale, meta.DefaultTemplate.Title)
|
||||
meta.DefaultTemplate.Content = i18n.Translate(locale, meta.DefaultTemplate.Content)
|
||||
return meta
|
||||
}
|
||||
|
||||
func localizePushEvent(locale string, event model.PushEvent) model.PushEvent {
|
||||
if meta, found := findBuiltInEvent(event.EventKey); found {
|
||||
if event.Name == meta.Name || i18n.Translate("en", event.Name) == meta.Name {
|
||||
event.Name = i18n.Translate(locale, meta.Name)
|
||||
}
|
||||
} else if meta := task.GetTaskMetaByAsynqTask(event.TaskType); meta != nil {
|
||||
prefix := "Task completed: "
|
||||
english := prefix + meta.Name
|
||||
chinese := i18n.Translate("zh-CN", prefix) + i18n.Translate("zh-CN", meta.Name)
|
||||
if event.Name == english || event.Name == chinese {
|
||||
event.Name = i18n.Translate(locale, prefix) + i18n.Translate(locale, meta.Name)
|
||||
}
|
||||
}
|
||||
// Templates and targets are administrator-owned data, even when they happen
|
||||
// to match a built-in message. Never rewrite them during presentation.
|
||||
return event
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
// Copyright 2026 Arctel.net
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package push
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/Rain-kl/Wavelet/internal/model"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestLocalizeEventMetadataPreservesTemplatesAndRegistry(t *testing.T) {
|
||||
source := EventMetadata{Name: "Administrator login", DefaultTemplate: NotificationMessage{Title: "Administrator login notification"}}
|
||||
localized := localizeEventMetadata("zh-CN", source)
|
||||
assert.Equal(t, "管理员登录", localized.Name)
|
||||
assert.Equal(t, "管理员登录提醒", localized.DefaultTemplate.Title)
|
||||
assert.Equal(t, "Administrator login", source.Name)
|
||||
assert.Equal(t, "Administrator login notification", source.DefaultTemplate.Title)
|
||||
|
||||
event := model.PushEvent{Name: "自定义事件", Template: `{"title":"管理员登录提醒"}`, Targets: []string{"系统"}}
|
||||
assert.Equal(t, event, localizePushEvent("en", event))
|
||||
}
|
||||
@@ -13,6 +13,7 @@ import (
|
||||
"github.com/Rain-kl/Wavelet/internal/infra/task"
|
||||
"github.com/Rain-kl/Wavelet/internal/model"
|
||||
"github.com/Rain-kl/Wavelet/internal/repository"
|
||||
"github.com/Rain-kl/Wavelet/pkg/i18n"
|
||||
pkgpush "github.com/Rain-kl/Wavelet/pkg/push"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
@@ -278,7 +279,7 @@ func recordPushHistory(ctx context.Context, req SendPayload, status, errMsg stri
|
||||
content := req.Body.Content
|
||||
level := req.Body.Level
|
||||
if title == "" {
|
||||
title = "系统通知"
|
||||
title = "System notification"
|
||||
}
|
||||
if level == "" {
|
||||
level = defaultLevelInfo
|
||||
@@ -331,7 +332,7 @@ func resolveTargetUser(ctx context.Context, resolved string, _ string) (model.Us
|
||||
}
|
||||
|
||||
func resolveSystemTarget(ctx context.Context, resolved string, channel string) (string, bool) {
|
||||
if resolved != "系统" && resolved != "system" && resolved != "0" {
|
||||
if i18n.Translate("en", resolved) != "System" && resolved != "system" && resolved != "0" {
|
||||
return "", false
|
||||
}
|
||||
adminUser, err := repository.GetFirstAdminUser(ctx)
|
||||
@@ -383,10 +384,10 @@ func getEventInfo(req CreateEventRequest) (string, string, []byte, error) {
|
||||
return "", "", nil, errors.New("unsupported task type")
|
||||
}
|
||||
eventKey := "task_completed:" + req.TaskType
|
||||
eventName := "任务完成: " + meta.Name
|
||||
eventName := "Task completed: " + meta.Name
|
||||
defaultTemplate := NotificationMessage{
|
||||
Title: "任务完成: " + meta.Name,
|
||||
Content: "异步任务 {{task_name}} (ID: {{task_id}}) 已完成。状态: {{task_status}},耗时: {{task_duration}} ms。",
|
||||
Title: "Task completed: " + meta.Name,
|
||||
Content: "Asynchronous task {{task_name}} (ID: {{task_id}}) completed. Status: {{task_status}}; duration: {{task_duration}} ms.",
|
||||
Level: defaultLevelInfo,
|
||||
}
|
||||
defaultTemplateBytes, err := json.Marshal(defaultTemplate)
|
||||
|
||||
@@ -312,7 +312,7 @@ func TestPushHandler(t *testing.T) {
|
||||
t.Run("execute task successfully", func(t *testing.T) {
|
||||
res, execErr := handler.Execute(context.Background(), payloadBytes)
|
||||
require.NoError(t, execErr)
|
||||
assert.Contains(t, res.Message, "推送成功")
|
||||
assert.Contains(t, res.Message, "Notification sent")
|
||||
|
||||
// Verify mock pusher received flattened variables
|
||||
mPusher.mu.Lock()
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"slices"
|
||||
"strconv"
|
||||
|
||||
"github.com/Rain-kl/Wavelet/internal/model"
|
||||
@@ -54,6 +55,9 @@ func ListEvents(c *gin.Context) {
|
||||
response.AbortInternal(c, err.Error())
|
||||
return
|
||||
}
|
||||
for index := range events {
|
||||
events[index] = localizePushEvent(response.Locale(c), events[index])
|
||||
}
|
||||
c.JSON(http.StatusOK, response.OK(events))
|
||||
}
|
||||
|
||||
@@ -85,7 +89,11 @@ func findBuiltInEvent(key string) (EventMetadata, bool) {
|
||||
// @Success 200 {object} response.Any{data=[]EventMetadata} "内置通知事件列表"
|
||||
// @Router /api/v1/admin/push/events/builtin [get]
|
||||
func ListBuiltInEvents(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, response.OK(BuiltInEvents))
|
||||
metas := slices.Clone(BuiltInEvents)
|
||||
for index := range metas {
|
||||
metas[index] = localizeEventMetadata(response.Locale(c), metas[index])
|
||||
}
|
||||
c.JSON(http.StatusOK, response.OK(metas))
|
||||
}
|
||||
|
||||
// CreateEvent 创建通知事件
|
||||
@@ -110,7 +118,7 @@ func CreateEvent(c *gin.Context) {
|
||||
response.AbortBadRequest(c, err.Error())
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, response.OK(event))
|
||||
c.JSON(http.StatusOK, response.OK(localizePushEvent(response.Locale(c), event)))
|
||||
}
|
||||
|
||||
// DeleteEvent 删除通知事件配置
|
||||
@@ -280,8 +288,8 @@ func TestPush(c *gin.Context) {
|
||||
applySMTPFallbackToPushConfig(c.Request.Context(), &req.Config)
|
||||
|
||||
testBody := map[string]any{
|
||||
keyTitle: "测试通道推送",
|
||||
keyContent: "当您收到这条消息,说明当前渠道连通性测试通过。",
|
||||
keyTitle: "Notification channel test",
|
||||
keyContent: "Receiving this message confirms that the notification channel connectivity test succeeded.",
|
||||
keyLevel: defaultLevelInfo,
|
||||
}
|
||||
if _, err := pusher.Send(c.Request.Context(), req.Config, req.Target, testBody, "", nil); err != nil {
|
||||
|
||||
@@ -67,7 +67,7 @@ func handleTaskCompleted(ctx context.Context, execution *model.TaskExecution, re
|
||||
meta := EventMetadata{
|
||||
Key: event.EventKey,
|
||||
Name: event.Name,
|
||||
Description: "异步任务执行完毕触发的自动通知",
|
||||
Description: "Automatic notification triggered when an asynchronous task finishes",
|
||||
}
|
||||
DefaultTrigger.Trigger(ctx, meta, body)
|
||||
}
|
||||
|
||||
@@ -25,8 +25,8 @@ const (
|
||||
var SendNotificationMeta = task.TaskMeta{
|
||||
Type: TaskTypeSendNotification,
|
||||
AsynqTask: SendNotificationTask,
|
||||
Name: "推送通知",
|
||||
Description: "异步执行系统通知的多渠道派发与推送",
|
||||
Name: "Send notification",
|
||||
Description: "Dispatch system notifications asynchronously across multiple channels",
|
||||
SupportsTime: false,
|
||||
MaxRetry: task.DefaultMaxRetry,
|
||||
Queue: task.QueueDefault,
|
||||
@@ -34,14 +34,14 @@ var SendNotificationMeta = task.TaskMeta{
|
||||
Params: []task.TaskParam{
|
||||
{
|
||||
Name: "event_key",
|
||||
Label: "事件标识",
|
||||
Label: "Event key",
|
||||
Type: "string",
|
||||
Required: true,
|
||||
Placeholder: "admin_login",
|
||||
},
|
||||
{
|
||||
Name: "target",
|
||||
Label: "目标接收者",
|
||||
Label: "Recipient",
|
||||
Type: "string",
|
||||
Required: false,
|
||||
},
|
||||
@@ -75,16 +75,16 @@ func (h *PushHandler) ValidatePayload(payload []byte) ([]byte, error) {
|
||||
func (h *PushHandler) Execute(ctx context.Context, payload []byte) (*task.TaskResult, error) {
|
||||
var req SendPayload
|
||||
if err := json.Unmarshal(payload, &req); err != nil {
|
||||
task.AppendLog(ctx, "解析推送参数失败: %v", err)
|
||||
task.AppendLog(ctx, "Failed to parse notification parameters: %v", err)
|
||||
return nil, fmt.Errorf("parse payload failed: %w", err)
|
||||
}
|
||||
|
||||
task.AppendLog(ctx, "开始推送通知: 事件 = %s, 渠道 = %s, 接收目标 = %s", req.EventKey, req.Config.Channel, req.Target)
|
||||
task.AppendLog(ctx, "Starting notification: event = %s, channel = %s, recipient = %s", req.EventKey, req.Config.Channel, req.Target)
|
||||
|
||||
pusher, err := push.GetPusher(req.Config.Channel)
|
||||
if err != nil {
|
||||
errWrap := fmt.Errorf("get pusher failed: %w", err)
|
||||
task.AppendLog(ctx, "推送失败: %v", errWrap)
|
||||
task.AppendLog(ctx, "Notification failed: %v", errWrap)
|
||||
if task.IsFinalAttempt(ctx) {
|
||||
h.recordHistory(ctx, req, "failed", errWrap.Error())
|
||||
}
|
||||
@@ -99,9 +99,9 @@ func (h *PushHandler) Execute(ctx context.Context, payload []byte) (*task.TaskRe
|
||||
content := req.Body.Content
|
||||
|
||||
if err != nil {
|
||||
task.AppendLog(ctx, "消息推送失败 (标题: %s): %v", title, err)
|
||||
task.AppendLog(ctx, "Failed to send notification (title: %s): %v", title, err)
|
||||
if upstreamResp != "" {
|
||||
task.AppendLog(ctx, "上游返回: %s", upstreamResp)
|
||||
task.AppendLog(ctx, "Upstream response: %s", upstreamResp)
|
||||
}
|
||||
if task.IsFinalAttempt(ctx) {
|
||||
h.recordHistory(ctx, req, "failed", err.Error())
|
||||
@@ -109,19 +109,19 @@ func (h *PushHandler) Execute(ctx context.Context, payload []byte) (*task.TaskRe
|
||||
return nil, fmt.Errorf("pusher.Send failed: %w", err)
|
||||
}
|
||||
|
||||
task.AppendLog(ctx, "消息推送成功 (标题: %s, 内容摘要: %s)", title, content)
|
||||
task.AppendLog(ctx, "Notification sent (title: %s, content summary: %s)", title, content)
|
||||
if upstreamResp != "" {
|
||||
task.AppendLog(ctx, "上游返回: %s", upstreamResp)
|
||||
task.AppendLog(ctx, "Upstream response: %s", upstreamResp)
|
||||
}
|
||||
h.recordHistory(ctx, req, "success", "")
|
||||
|
||||
return &task.TaskResult{
|
||||
Message: fmt.Sprintf("推送成功: [%s] -> %s", req.Config.Channel, req.Target),
|
||||
Message: fmt.Sprintf("Notification sent: [%s] -> %s", req.Config.Channel, req.Target),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (h *PushHandler) recordHistory(ctx context.Context, req SendPayload, status string, errMsg string) {
|
||||
if dbErr := recordPushHistory(ctx, req, status, errMsg); dbErr != nil {
|
||||
task.AppendLog(ctx, "写入推送历史审计记录失败: %v", dbErr)
|
||||
task.AppendLog(ctx, "Failed to write notification audit history: %v", dbErr)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user