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:
@@ -7,17 +7,17 @@ package task
|
||||
|
||||
// 任务管理相关错误消息
|
||||
const (
|
||||
InvalidTaskType = "无效的任务类型"
|
||||
InvalidTimeRange = "无效的时间范围"
|
||||
TaskDispatchFailed = "任务下发失败"
|
||||
UserIDRequired = "用户ID必填"
|
||||
TaskNotFound = "任务执行记录不存在"
|
||||
TaskNotRetryable = "该任务不支持重试"
|
||||
TaskNotFailed = "只有失败的任务才能重试"
|
||||
TaskMaxRetryExceeded = "已达到最大重试次数"
|
||||
TaskRetryFailed = "任务重试失败"
|
||||
InvalidCronExpression = "无效的 Cron 表达式"
|
||||
ScheduleNotFound = "定时任务不存在"
|
||||
ScheduleSaveFailed = "保存定时任务失败"
|
||||
ScheduleDeleteFailed = "删除定时任务失败"
|
||||
InvalidTaskType = "Invalid task type"
|
||||
InvalidTimeRange = "Invalid time range"
|
||||
TaskDispatchFailed = "Failed to dispatch task"
|
||||
UserIDRequired = "User ID is required"
|
||||
TaskNotFound = "Task execution was not found"
|
||||
TaskNotRetryable = "this task cannot be retried"
|
||||
TaskNotFailed = "Only failed tasks can be retried"
|
||||
TaskMaxRetryExceeded = "Maximum retry count reached"
|
||||
TaskRetryFailed = "Failed to retry task"
|
||||
InvalidCronExpression = "Invalid Cron expression"
|
||||
ScheduleNotFound = "Schedule was not found"
|
||||
ScheduleSaveFailed = "Failed to save schedule"
|
||||
ScheduleDeleteFailed = "Failed to delete schedule"
|
||||
)
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
// Copyright 2026 Arctel.net
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package task
|
||||
|
||||
import (
|
||||
"slices"
|
||||
|
||||
"github.com/Rain-kl/Wavelet/internal/infra/task"
|
||||
"github.com/Rain-kl/Wavelet/internal/model"
|
||||
"github.com/Rain-kl/Wavelet/pkg/i18n"
|
||||
)
|
||||
|
||||
// Localize copies, never the shared registry or persistent task records.
|
||||
func localizeTaskMeta(locale string, meta task.TaskMeta) task.TaskMeta {
|
||||
meta.Name = i18n.Translate(locale, meta.Name)
|
||||
meta.Description = i18n.Translate(locale, meta.Description)
|
||||
meta.Params = slices.Clone(meta.Params)
|
||||
for index := range meta.Params {
|
||||
param := &meta.Params[index]
|
||||
param.Label = i18n.Translate(locale, param.Label)
|
||||
param.Placeholder = i18n.Translate(locale, param.Placeholder)
|
||||
param.Description = i18n.Translate(locale, param.Description)
|
||||
}
|
||||
return meta
|
||||
}
|
||||
|
||||
func localizeTaskExecution(locale string, execution model.TaskExecution) model.TaskExecution {
|
||||
execution.TaskName = i18n.Translate(locale, execution.TaskName)
|
||||
execution.Log = i18n.TranslateLog(locale, execution.Log)
|
||||
execution.ErrorMessage = i18n.Translate(locale, execution.ErrorMessage)
|
||||
execution.Result = i18n.Translate(locale, execution.Result)
|
||||
return execution
|
||||
}
|
||||
|
||||
func localizeTaskExecutions(locale string, executions []model.TaskExecution) []model.TaskExecution {
|
||||
localized := slices.Clone(executions)
|
||||
for index := range localized {
|
||||
localized[index] = localizeTaskExecution(locale, localized[index])
|
||||
}
|
||||
return localized
|
||||
}
|
||||
|
||||
func localizeScheduleName(locale, name string, meta *task.TaskMeta) string {
|
||||
// Only the built-in default name is system text. Custom schedule names are data.
|
||||
if meta != nil && meta.Type == "system_cleanup" && i18n.Translate("en", name) == "Scheduled system cleanup" {
|
||||
return i18n.Translate(locale, "Scheduled system cleanup")
|
||||
}
|
||||
if meta != nil && i18n.Translate("en", name) == meta.Name {
|
||||
return i18n.Translate(locale, meta.Name)
|
||||
}
|
||||
return name
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
// Copyright 2026 Arctel.net
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package task
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/Rain-kl/Wavelet/internal/infra/task"
|
||||
"github.com/Rain-kl/Wavelet/internal/model"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestLocalizeTaskMetaDoesNotMutateSharedParams(t *testing.T) {
|
||||
source := task.TaskMeta{
|
||||
Name: "Switch log database", AsynqTask: "openflare:log_db_switch",
|
||||
Params: []task.TaskParam{{Name: "target", Label: "Target log database", Placeholder: "postgres|sqlite|clickhouse"}},
|
||||
}
|
||||
localized := localizeTaskMeta("zh-CN", source)
|
||||
assert.Equal(t, "切换日志数据库", localized.Name)
|
||||
assert.Equal(t, "目标日志库", localized.Params[0].Label)
|
||||
assert.Equal(t, "openflare:log_db_switch", localized.AsynqTask)
|
||||
assert.Equal(t, "target", localized.Params[0].Name)
|
||||
assert.Equal(t, "Target log database", source.Params[0].Label)
|
||||
}
|
||||
|
||||
func TestLocalizeTaskExecutionsPreservesSourceAndPayload(t *testing.T) {
|
||||
source := []model.TaskExecution{{
|
||||
TaskName: "OpenFlare SSL 自动续期", Log: "[12:34:56] 开始扫描待续期证书\n",
|
||||
Payload: `{"name":"开始扫描待续期证书"}`, TriggeredBy: "用户甲",
|
||||
}}
|
||||
localized := localizeTaskExecutions("en", source)
|
||||
assert.Equal(t, "OpenFlare automatic SSL renewal", localized[0].TaskName)
|
||||
assert.Equal(t, "[12:34:56] Scanning certificates due for renewal\n", localized[0].Log)
|
||||
assert.Equal(t, source[0].Payload, localized[0].Payload)
|
||||
assert.Equal(t, source[0].TriggeredBy, localized[0].TriggeredBy)
|
||||
assert.Equal(t, "OpenFlare SSL 自动续期", source[0].TaskName)
|
||||
}
|
||||
|
||||
func TestLocalizeScheduleNamePreservesCustomNames(t *testing.T) {
|
||||
meta := &task.TaskMeta{Type: "system_cleanup", Name: "System cleanup"}
|
||||
assert.Equal(t, "Scheduled system cleanup", localizeScheduleName("en", "系统定期垃圾清理", meta))
|
||||
assert.Equal(t, "系统定期垃圾清理", localizeScheduleName("zh-CN", "Scheduled system cleanup", meta))
|
||||
assert.Equal(t, "自定义每周清理", localizeScheduleName("en", "自定义每周清理", meta))
|
||||
}
|
||||
@@ -5,6 +5,7 @@
|
||||
package task
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
@@ -35,7 +36,11 @@ import (
|
||||
// @Failure 403 {object} response.Any "无管理员权限"
|
||||
// @Router /api/v1/admin/tasks/types [get]
|
||||
func ListTaskTypes(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, response.OK(task.GetDispatchableTasks()))
|
||||
metas := task.GetDispatchableTasks()
|
||||
for index := range metas {
|
||||
metas[index] = localizeTaskMeta(response.Locale(c), metas[index])
|
||||
}
|
||||
c.JSON(http.StatusOK, response.OK(metas))
|
||||
}
|
||||
|
||||
// DispatchTaskRequest 下发任务请求
|
||||
@@ -130,7 +135,7 @@ func ListTaskExecutions(c *gin.Context) {
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, response.OK(gin.H{
|
||||
"items": executions,
|
||||
"items": localizeTaskExecutions(response.Locale(c), executions),
|
||||
"total": total,
|
||||
"page": req.Page,
|
||||
"page_size": req.PageSize,
|
||||
@@ -163,7 +168,8 @@ func GetTaskExecution(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, response.OK(execution))
|
||||
localized := localizeTaskExecution(response.Locale(c), *execution)
|
||||
c.JSON(http.StatusOK, response.OK(localized))
|
||||
}
|
||||
|
||||
// RetryTask 重试失败的任务
|
||||
@@ -190,10 +196,11 @@ func RetryTask(c *gin.Context) {
|
||||
newTaskID, err := task.RetryTask(c.Request.Context(), id)
|
||||
if err != nil {
|
||||
errMsg := err.Error()
|
||||
var retryErr *task.RetryError
|
||||
switch {
|
||||
case strings.Contains(errMsg, "不存在"):
|
||||
case errors.As(err, &retryErr) && retryErr.NotFound:
|
||||
response.AbortNotFound(c, errMsg)
|
||||
case strings.Contains(errMsg, "只有失败的任务") || strings.Contains(errMsg, "不支持重试") || strings.Contains(errMsg, "已达到最大重试"):
|
||||
case retryErr != nil:
|
||||
response.AbortBadRequest(c, errMsg)
|
||||
default:
|
||||
response.AbortInternal(c, fmt.Sprintf("%s: %v", TaskRetryFailed, err))
|
||||
@@ -226,6 +233,7 @@ func ListSchedules(c *gin.Context) {
|
||||
if meta != nil && meta.InternalOnly {
|
||||
continue
|
||||
}
|
||||
schedule.Name = localizeScheduleName(response.Locale(c), schedule.Name, meta)
|
||||
visible = append(visible, schedule)
|
||||
}
|
||||
c.JSON(http.StatusOK, response.OK(visible))
|
||||
@@ -300,7 +308,7 @@ func CreateSchedule(c *gin.Context) {
|
||||
|
||||
// 触发调度服务重载
|
||||
if err := scheduler.ReloadScheduler(); err != nil {
|
||||
logger.ErrorF(c.Request.Context(), "[TaskAdmin] 重载调度器失败: %v", err)
|
||||
logger.ErrorF(c.Request.Context(), "[TaskAdmin] Failed to reload scheduler: %v", err)
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, response.OK(schedule))
|
||||
@@ -334,7 +342,7 @@ type UpdateScheduleRequest struct {
|
||||
func UpdateSchedule(c *gin.Context) {
|
||||
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
||||
if err != nil {
|
||||
response.AbortBadRequest(c, "无效的定时任务ID")
|
||||
response.AbortBadRequest(c, "Invalid schedule ID")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -392,7 +400,7 @@ func UpdateSchedule(c *gin.Context) {
|
||||
|
||||
// 触发调度服务重载
|
||||
if err := scheduler.ReloadScheduler(); err != nil {
|
||||
logger.ErrorF(c.Request.Context(), "[TaskAdmin] 重载调度器失败: %v", err)
|
||||
logger.ErrorF(c.Request.Context(), "[TaskAdmin] Failed to reload scheduler: %v", err)
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, response.OK(schedule))
|
||||
@@ -423,7 +431,7 @@ func getAdminTaskMeta(taskType string) *task.TaskMeta {
|
||||
func DeleteSchedule(c *gin.Context) {
|
||||
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
||||
if err != nil {
|
||||
response.AbortBadRequest(c, "无效的定时任务ID")
|
||||
response.AbortBadRequest(c, "Invalid schedule ID")
|
||||
return
|
||||
}
|
||||
schedule, err := repository.GetScheduleByID(c.Request.Context(), id)
|
||||
@@ -443,7 +451,7 @@ func DeleteSchedule(c *gin.Context) {
|
||||
|
||||
// 触发调度服务重载
|
||||
if err := scheduler.ReloadScheduler(); err != nil {
|
||||
logger.ErrorF(c.Request.Context(), "[TaskAdmin] 重载调度器失败: %v", err)
|
||||
logger.ErrorF(c.Request.Context(), "[TaskAdmin] Failed to reload scheduler: %v", err)
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, response.OKNil())
|
||||
|
||||
@@ -410,7 +410,7 @@ func TestDispatchTask(t *testing.T) {
|
||||
assert.Equal(t, http.StatusBadRequest, w.Code)
|
||||
var resp response.Any
|
||||
json.Unmarshal(w.Body.Bytes(), &resp)
|
||||
assert.Contains(t, resp.ErrorMsg, "无效的 JSON 格式")
|
||||
assert.Contains(t, resp.ErrorMsg, "invalid JSON format")
|
||||
})
|
||||
|
||||
t.Run("dispatch send_email task failure with missing fields", func(t *testing.T) {
|
||||
@@ -427,7 +427,7 @@ func TestDispatchTask(t *testing.T) {
|
||||
assert.Equal(t, http.StatusBadRequest, w.Code)
|
||||
var resp response.Any
|
||||
json.Unmarshal(w.Body.Bytes(), &resp)
|
||||
assert.Contains(t, resp.ErrorMsg, "不能为空")
|
||||
assert.Contains(t, resp.ErrorMsg, "must not be empty")
|
||||
})
|
||||
|
||||
t.Run("dispatch invalid task type failure", func(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user