i18n: 中文化所有错误消息和用户交互文本
Deploy PR-Helper / deploy (push) Successful in 31s

- Go 后端 handler 错误消息统一中文化(middleware/repos/generate/review/settings)
- 前端 JS 加载和错误消息中文化(diff-viewer.js, graph.js)
- 模板页面错误 fallback 和静态文本中文化(index/generate/review/base)
- 消除中英文混杂,提升中文用户体验
This commit is contained in:
2026-06-21 15:11:20 +08:00
parent a6bd46db34
commit 55bfdf08ce
11 changed files with 66 additions and 66 deletions
+7 -7
View File
@@ -22,7 +22,7 @@ func NewGenerateHandler(db *sql.DB) *GenerateHandler {
func (h *GenerateHandler) Generate(c *gin.Context) {
user := GetCurrentUser(c)
if user == nil {
c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthenticated"})
c.JSON(http.StatusUnauthorized, gin.H{"error": "未登录"})
return
}
@@ -32,7 +32,7 @@ func (h *GenerateHandler) Generate(c *gin.Context) {
var localPath string
err := h.db.QueryRow(`SELECT local_path FROM repositories WHERE id = ? AND user_id = ?`, id, user.ID).Scan(&localPath)
if err == sql.ErrNoRows {
c.JSON(http.StatusNotFound, gin.H{"error": "repository not found"})
c.JSON(http.StatusNotFound, gin.H{"error": "仓库未找到"})
return
}
if err != nil {
@@ -46,7 +46,7 @@ func (h *GenerateHandler) Generate(c *gin.Context) {
Head string `json:"head" binding:"required"`
}
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "base and head are required"})
c.JSON(http.StatusBadRequest, gin.H{"error": "请选择 Base 和 Head 分支"})
return
}
@@ -59,14 +59,14 @@ func (h *GenerateHandler) Generate(c *gin.Context) {
flusher, ok := c.Writer.(http.Flusher)
if !ok {
c.JSON(http.StatusInternalServerError, gin.H{"error": "streaming not supported"})
c.JSON(http.StatusInternalServerError, gin.H{"error": "服务器不支持流式传输"})
return
}
sendEvent := func(event string, data interface{}) {
jsonData, err := json.Marshal(data)
if err != nil {
jsonData = []byte(`{"error":"failed to marshal event data"}`)
jsonData = []byte(`{"error":"序列化事件数据失败"}`)
}
fmt.Fprintf(c.Writer, "event: %s\ndata: %s\n\n", event, jsonData)
flusher.Flush()
@@ -85,12 +85,12 @@ func (h *GenerateHandler) Generate(c *gin.Context) {
// Save analysis to DB with user_id
resultJSON, err := json.Marshal(pr)
if err != nil {
sendEvent("error", map[string]interface{}{"message": "marshal result: " + err.Error()})
sendEvent("error", map[string]interface{}{"message": "序列化结果失败: " + err.Error()})
return
}
if _, err := h.db.Exec(`INSERT INTO analyses (user_id, repo_id, type, base_ref, head_ref, result) VALUES (?, ?, 'pr_description', ?, ?, ?)`,
user.ID, id, req.Base, req.Head, string(resultJSON)); err != nil {
sendEvent("error", map[string]interface{}{"message": "save analysis: " + err.Error()})
sendEvent("error", map[string]interface{}{"message": "保存分析结果失败: " + err.Error()})
return
}
}
+2 -2
View File
@@ -19,7 +19,7 @@ func AuthRequired(db *sql.DB) gin.HandlerFunc {
if userID == nil {
// Check if it's an API request
if len(c.Request.URL.Path) > 4 && c.Request.URL.Path[:5] == "/api/" {
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "unauthenticated"})
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "未登录"})
} else {
c.Redirect(http.StatusFound, "/login")
c.Abort()
@@ -36,7 +36,7 @@ func AuthRequired(db *sql.DB) gin.HandlerFunc {
session.Clear()
session.Save()
if len(c.Request.URL.Path) > 4 && c.Request.URL.Path[:5] == "/api/" {
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "unauthenticated"})
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "未登录"})
} else {
c.Redirect(http.StatusFound, "/login")
c.Abort()
+20 -20
View File
@@ -26,7 +26,7 @@ func NewReposHandler(db *sql.DB, reposDir string) *ReposHandler {
func (h *ReposHandler) ListRepos(c *gin.Context) {
user := GetCurrentUser(c)
if user == nil {
c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthenticated"})
c.JSON(http.StatusUnauthorized, gin.H{"error": "未登录"})
return
}
@@ -62,7 +62,7 @@ func (h *ReposHandler) ListRepos(c *gin.Context) {
func (h *ReposHandler) DeleteRepo(c *gin.Context) {
user := GetCurrentUser(c)
if user == nil {
c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthenticated"})
c.JSON(http.StatusUnauthorized, gin.H{"error": "未登录"})
return
}
@@ -70,7 +70,7 @@ func (h *ReposHandler) DeleteRepo(c *gin.Context) {
var localPath string
err := h.db.QueryRow(`SELECT local_path FROM repositories WHERE id = ? AND user_id = ?`, id, user.ID).Scan(&localPath)
if err == sql.ErrNoRows {
c.JSON(http.StatusNotFound, gin.H{"error": "repository not found"})
c.JSON(http.StatusNotFound, gin.H{"error": "仓库未找到"})
return
}
if err != nil {
@@ -95,7 +95,7 @@ func (h *ReposHandler) DeleteRepo(c *gin.Context) {
func (h *ReposHandler) CleanupRepos(c *gin.Context) {
user := GetCurrentUser(c)
if user == nil {
c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthenticated"})
c.JSON(http.StatusUnauthorized, gin.H{"error": "未登录"})
return
}
@@ -144,7 +144,7 @@ func (h *ReposHandler) CleanupRepos(c *gin.Context) {
func (h *ReposHandler) PullRepo(c *gin.Context) {
user := GetCurrentUser(c)
if user == nil {
c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthenticated"})
c.JSON(http.StatusUnauthorized, gin.H{"error": "未登录"})
return
}
@@ -153,7 +153,7 @@ func (h *ReposHandler) PullRepo(c *gin.Context) {
var credential sql.NullString
err := h.db.QueryRow(`SELECT local_path, auth_type, credential FROM repositories WHERE id = ? AND user_id = ?`, id, user.ID).Scan(&localPath, &authType, &credential)
if err == sql.ErrNoRows {
c.JSON(http.StatusNotFound, gin.H{"error": "repository not found"})
c.JSON(http.StatusNotFound, gin.H{"error": "仓库未找到"})
return
}
if err != nil {
@@ -192,7 +192,7 @@ func (h *ReposHandler) PullRepo(c *gin.Context) {
func (h *ReposHandler) CloneRepo(c *gin.Context) {
user := GetCurrentUser(c)
if user == nil {
c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthenticated"})
c.JSON(http.StatusUnauthorized, gin.H{"error": "未登录"})
return
}
@@ -202,7 +202,7 @@ func (h *ReposHandler) CloneRepo(c *gin.Context) {
Password string `json:"password"`
}
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "url is required"})
c.JSON(http.StatusBadRequest, gin.H{"error": "请输入仓库地址"})
return
}
@@ -215,14 +215,14 @@ func (h *ReposHandler) CloneRepo(c *gin.Context) {
flusher, ok := c.Writer.(http.Flusher)
if !ok {
c.JSON(http.StatusInternalServerError, gin.H{"error": "streaming not supported"})
c.JSON(http.StatusInternalServerError, gin.H{"error": "服务器不支持流式传输"})
return
}
sendEvent := func(event string, data interface{}) {
jsonData, err := json.Marshal(data)
if err != nil {
jsonData = []byte(`{"error":"failed to marshal event data"}`)
jsonData = []byte(`{"error":"序列化事件数据失败"}`)
}
fmt.Fprintf(c.Writer, "event: %s\ndata: %s\n\n", event, jsonData)
flusher.Flush()
@@ -288,7 +288,7 @@ func (h *ReposHandler) CloneRepo(c *gin.Context) {
func (h *ReposHandler) GetGraph(c *gin.Context) {
user := GetCurrentUser(c)
if user == nil {
c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthenticated"})
c.JSON(http.StatusUnauthorized, gin.H{"error": "未登录"})
return
}
@@ -296,7 +296,7 @@ func (h *ReposHandler) GetGraph(c *gin.Context) {
var localPath string
err := h.db.QueryRow(`SELECT local_path FROM repositories WHERE id = ? AND user_id = ?`, id, user.ID).Scan(&localPath)
if err == sql.ErrNoRows {
c.JSON(http.StatusNotFound, gin.H{"error": "repository not found"})
c.JSON(http.StatusNotFound, gin.H{"error": "仓库未找到"})
return
}
if err != nil {
@@ -309,7 +309,7 @@ func (h *ReposHandler) GetGraph(c *gin.Context) {
repo, err := services.OpenRepo(localPath)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "open repo: " + err.Error()})
c.JSON(http.StatusInternalServerError, gin.H{"error": "打开仓库失败: " + err.Error()})
return
}
@@ -333,7 +333,7 @@ func (h *ReposHandler) GetGraph(c *gin.Context) {
func (h *ReposHandler) GetDiff(c *gin.Context) {
user := GetCurrentUser(c)
if user == nil {
c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthenticated"})
c.JSON(http.StatusUnauthorized, gin.H{"error": "未登录"})
return
}
@@ -342,14 +342,14 @@ func (h *ReposHandler) GetDiff(c *gin.Context) {
head := c.Query("head")
if base == "" || head == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "base and head query params are required"})
c.JSON(http.StatusBadRequest, gin.H{"error": "请选择 Base 和 Head 分支"})
return
}
var localPath string
err := h.db.QueryRow(`SELECT local_path FROM repositories WHERE id = ? AND user_id = ?`, id, user.ID).Scan(&localPath)
if err == sql.ErrNoRows {
c.JSON(http.StatusNotFound, gin.H{"error": "repository not found"})
c.JSON(http.StatusNotFound, gin.H{"error": "仓库未找到"})
return
}
if err != nil {
@@ -390,7 +390,7 @@ func (h *ReposHandler) GetDiff(c *gin.Context) {
func (h *ReposHandler) GetRefs(c *gin.Context) {
user := GetCurrentUser(c)
if user == nil {
c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthenticated"})
c.JSON(http.StatusUnauthorized, gin.H{"error": "未登录"})
return
}
@@ -398,7 +398,7 @@ func (h *ReposHandler) GetRefs(c *gin.Context) {
var localPath string
err := h.db.QueryRow(`SELECT local_path FROM repositories WHERE id = ? AND user_id = ?`, id, user.ID).Scan(&localPath)
if err == sql.ErrNoRows {
c.JSON(http.StatusNotFound, gin.H{"error": "repository not found"})
c.JSON(http.StatusNotFound, gin.H{"error": "仓库未找到"})
return
}
if err != nil {
@@ -425,7 +425,7 @@ func (h *ReposHandler) GetRefs(c *gin.Context) {
func (h *ReposHandler) GetCommits(c *gin.Context) {
user := GetCurrentUser(c)
if user == nil {
c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthenticated"})
c.JSON(http.StatusUnauthorized, gin.H{"error": "未登录"})
return
}
@@ -444,7 +444,7 @@ func (h *ReposHandler) GetCommits(c *gin.Context) {
var localPath string
err := h.db.QueryRow(`SELECT local_path FROM repositories WHERE id = ? AND user_id = ?`, id, user.ID).Scan(&localPath)
if err == sql.ErrNoRows {
c.JSON(http.StatusNotFound, gin.H{"error": "repository not found"})
c.JSON(http.StatusNotFound, gin.H{"error": "仓库未找到"})
return
}
if err != nil {
+19 -19
View File
@@ -24,7 +24,7 @@ func NewReviewHandler(db *sql.DB) *ReviewHandler {
func (h *ReviewHandler) Review(c *gin.Context) {
user := GetCurrentUser(c)
if user == nil {
c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthenticated"})
c.JSON(http.StatusUnauthorized, gin.H{"error": "未登录"})
return
}
@@ -34,7 +34,7 @@ func (h *ReviewHandler) Review(c *gin.Context) {
var localPath string
err := h.db.QueryRow(`SELECT local_path FROM repositories WHERE id = ? AND user_id = ?`, id, user.ID).Scan(&localPath)
if err == sql.ErrNoRows {
c.JSON(http.StatusNotFound, gin.H{"error": "repository not found"})
c.JSON(http.StatusNotFound, gin.H{"error": "仓库未找到"})
return
}
if err != nil {
@@ -50,7 +50,7 @@ func (h *ReviewHandler) Review(c *gin.Context) {
Concurrency *int `json:"concurrency"`
}
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "base and head are required"})
c.JSON(http.StatusBadRequest, gin.H{"error": "请选择 Base 和 Head 分支"})
return
}
@@ -91,14 +91,14 @@ func (h *ReviewHandler) Review(c *gin.Context) {
flusher, ok := c.Writer.(http.Flusher)
if !ok {
c.JSON(http.StatusInternalServerError, gin.H{"error": "streaming not supported"})
c.JSON(http.StatusInternalServerError, gin.H{"error": "服务器不支持流式传输"})
return
}
sendEvent := func(event string, data interface{}) {
jsonData, err := json.Marshal(data)
if err != nil {
jsonData = []byte(`{"error":"failed to marshal event data"}`)
jsonData = []byte(`{"error":"序列化事件数据失败"}`)
}
fmt.Fprintf(c.Writer, "event: %s\ndata: %s\n\n", event, jsonData)
flusher.Flush()
@@ -117,18 +117,18 @@ func (h *ReviewHandler) Review(c *gin.Context) {
// Save analysis to DB with user_id
resultJSON, err := json.Marshal(reviewResult)
if err != nil {
sendEvent("error", map[string]interface{}{"message": "marshal result: " + err.Error()})
sendEvent("error", map[string]interface{}{"message": "序列化结果失败: " + err.Error()})
return
}
res, err := h.db.Exec(`INSERT INTO analyses (user_id, repo_id, type, base_ref, head_ref, result) VALUES (?, ?, 'code_review', ?, ?, ?)`,
user.ID, id, req.Base, req.Head, string(resultJSON))
if err != nil {
sendEvent("error", map[string]interface{}{"message": "save analysis: " + err.Error()})
sendEvent("error", map[string]interface{}{"message": "保存分析结果失败: " + err.Error()})
return
}
analysisID, err := res.LastInsertId()
if err != nil {
sendEvent("error", map[string]interface{}{"message": "get analysis id: " + err.Error()})
sendEvent("error", map[string]interface{}{"message": "获取分析记录失败: " + err.Error()})
return
}
sendEvent("analysis_saved", map[string]interface{}{
@@ -140,7 +140,7 @@ func (h *ReviewHandler) Review(c *gin.Context) {
func (h *ReviewHandler) SaveNotes(c *gin.Context) {
user := GetCurrentUser(c)
if user == nil {
c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthenticated"})
c.JSON(http.StatusUnauthorized, gin.H{"error": "未登录"})
return
}
@@ -151,7 +151,7 @@ func (h *ReviewHandler) SaveNotes(c *gin.Context) {
Content string `json:"content"`
}
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "analysis_id and scope are required"})
c.JSON(http.StatusBadRequest, gin.H{"error": "缺少必要参数"})
return
}
@@ -160,7 +160,7 @@ func (h *ReviewHandler) SaveNotes(c *gin.Context) {
case "overall", "file", "suggestion":
// valid
default:
c.JSON(http.StatusBadRequest, gin.H{"error": "scope must be overall, file, or suggestion"})
c.JSON(http.StatusBadRequest, gin.H{"error": "scope 参数无效"})
return
}
@@ -168,7 +168,7 @@ func (h *ReviewHandler) SaveNotes(c *gin.Context) {
var analysisOwnerID int64
err := h.db.QueryRow(`SELECT user_id FROM analyses WHERE id = ?`, req.AnalysisID).Scan(&analysisOwnerID)
if err == sql.ErrNoRows || analysisOwnerID != user.ID {
c.JSON(http.StatusNotFound, gin.H{"error": "analysis not found"})
c.JSON(http.StatusNotFound, gin.H{"error": "分析记录未找到"})
return
}
if err != nil {
@@ -189,19 +189,19 @@ func (h *ReviewHandler) SaveNotes(c *gin.Context) {
func (h *ReviewHandler) GetNotes(c *gin.Context) {
user := GetCurrentUser(c)
if user == nil {
c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthenticated"})
c.JSON(http.StatusUnauthorized, gin.H{"error": "未登录"})
return
}
analysisIDStr := c.Query("analysis_id")
if analysisIDStr == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "analysis_id query parameter is required"})
c.JSON(http.StatusBadRequest, gin.H{"error": "缺少 analysis_id 参数"})
return
}
analysisID, err := strconv.ParseInt(analysisIDStr, 10, 64)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid analysis_id"})
c.JSON(http.StatusBadRequest, gin.H{"error": "analysis_id 无效"})
return
}
@@ -209,7 +209,7 @@ func (h *ReviewHandler) GetNotes(c *gin.Context) {
var analysisOwnerID int64
err = h.db.QueryRow(`SELECT user_id FROM analyses WHERE id = ?`, analysisID).Scan(&analysisOwnerID)
if err == sql.ErrNoRows || analysisOwnerID != user.ID {
c.JSON(http.StatusNotFound, gin.H{"error": "analysis not found"})
c.JSON(http.StatusNotFound, gin.H{"error": "分析记录未找到"})
return
}
if err != nil {
@@ -235,7 +235,7 @@ func (h *ReviewHandler) GetNotes(c *gin.Context) {
func (h *ReviewHandler) ListReviews(c *gin.Context) {
user := GetCurrentUser(c)
if user == nil {
c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthenticated"})
c.JSON(http.StatusUnauthorized, gin.H{"error": "未登录"})
return
}
@@ -278,7 +278,7 @@ func (h *ReviewHandler) ListReviews(c *gin.Context) {
func (h *ReviewHandler) GetReview(c *gin.Context) {
user := GetCurrentUser(c)
if user == nil {
c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthenticated"})
c.JSON(http.StatusUnauthorized, gin.H{"error": "未登录"})
return
}
@@ -287,7 +287,7 @@ func (h *ReviewHandler) GetReview(c *gin.Context) {
var result, baseRef, headRef, createdAt string
err := h.db.QueryRow(`SELECT result, base_ref, head_ref, created_at FROM analyses WHERE id = ? AND user_id = ? AND type = 'code_review'`, aid, user.ID).Scan(&result, &baseRef, &headRef, &createdAt)
if err == sql.ErrNoRows {
c.JSON(http.StatusNotFound, gin.H{"error": "analysis not found"})
c.JSON(http.StatusNotFound, gin.H{"error": "分析记录未找到"})
return
}
if err != nil {
+3 -3
View File
@@ -18,7 +18,7 @@ func NewSettingsHandler(db *sql.DB) *SettingsHandler {
func (h *SettingsHandler) GetSettings(c *gin.Context) {
user := GetCurrentUser(c)
if user == nil {
c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthenticated"})
c.JSON(http.StatusUnauthorized, gin.H{"error": "未登录"})
return
}
@@ -41,13 +41,13 @@ func (h *SettingsHandler) GetSettings(c *gin.Context) {
func (h *SettingsHandler) UpdateSettings(c *gin.Context) {
user := GetCurrentUser(c)
if user == nil {
c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthenticated"})
c.JSON(http.StatusUnauthorized, gin.H{"error": "未登录"})
return
}
var body map[string]string
if err := c.ShouldBindJSON(&body); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid request body"})
c.JSON(http.StatusBadRequest, gin.H{"error": "请求格式无效"})
return
}
tx, err := h.db.Begin()