fix(i18n): add missing translations and improve language fallback

- Change default language fallback to English instead of Chinese
- Add ErrRedeemFailed typed error for model layer translation
- Migrate remaining hardcoded messages in controller/user.go
- Add translation keys: redeem.failed, user.create_default_token_error, common.uuid_duplicate, common.invalid_input
This commit is contained in:
CaIon
2026-02-05 00:16:17 +08:00
parent f60fce6584
commit 42b5aeaae4
5 changed files with 36 additions and 29 deletions

View File

@@ -2,6 +2,7 @@ package controller
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
@@ -213,10 +214,7 @@ func Register(c *gin.Context) {
token.Group = "auto"
}
if err := token.Insert(); err != nil {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": "创建默认令牌失败",
})
common.ApiErrorI18n(c, i18n.MsgCreateDefaultTokenErr)
return
}
}
@@ -294,20 +292,14 @@ func GenerateAccessToken(c *gin.Context) {
randI := common.GetRandomInt(4)
key, err := common.GenerateRandomKey(29 + randI)
if err != nil {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": "生成失败",
})
common.ApiErrorI18n(c, i18n.MsgGenerateFailed)
common.SysLog("failed to generate key: " + err.Error())
return
}
user.SetAccessToken(key)
if model.DB.Where("access_token = ?", user.AccessToken).First(user).RowsAffected != 0 {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": "请重试,系统生成的 UUID 竟然重复了!",
})
common.ApiErrorI18n(c, i18n.MsgUuidDuplicate)
return
}
@@ -658,18 +650,12 @@ func UpdateSelf(c *gin.Context) {
var user model.User
requestDataBytes, err := json.Marshal(requestData)
if err != nil {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": "无效的参数",
})
common.ApiErrorI18n(c, i18n.MsgInvalidParams)
return
}
err = json.Unmarshal(requestDataBytes, &user)
if err != nil {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": "无效的参数",
})
common.ApiErrorI18n(c, i18n.MsgInvalidParams)
return
}
@@ -677,10 +663,7 @@ func UpdateSelf(c *gin.Context) {
user.Password = "$I_LOVE_U" // make Validator happy :)
}
if err := common.Validate.Struct(&user); err != nil {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": "输入不合法 " + err.Error(),
})
common.ApiErrorI18n(c, i18n.MsgInvalidInput)
return
}
@@ -996,6 +979,10 @@ func TopUp(c *gin.Context) {
}
quota, err := model.Redeem(req.Key, id)
if err != nil {
if errors.Is(err, model.ErrRedeemFailed) {
common.ApiErrorI18n(c, i18n.MsgRedeemFailed)
return
}
common.ApiError(c, err)
return
}
@@ -1149,10 +1136,7 @@ func UpdateUserSetting(c *gin.Context) {
// 更新用户设置
user.SetSetting(settings)
if err := user.Update(false); err != nil {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": "更新设置失败: " + err.Error(),
})
common.ApiErrorI18n(c, i18n.MsgUpdateFailed)
return
}