mirror of
https://github.com/QuantumNous/new-api.git
synced 2026-05-02 06:58:35 +00:00
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:
@@ -2,6 +2,7 @@ package controller
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
@@ -213,10 +214,7 @@ func Register(c *gin.Context) {
|
|||||||
token.Group = "auto"
|
token.Group = "auto"
|
||||||
}
|
}
|
||||||
if err := token.Insert(); err != nil {
|
if err := token.Insert(); err != nil {
|
||||||
c.JSON(http.StatusOK, gin.H{
|
common.ApiErrorI18n(c, i18n.MsgCreateDefaultTokenErr)
|
||||||
"success": false,
|
|
||||||
"message": "创建默认令牌失败",
|
|
||||||
})
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -294,20 +292,14 @@ func GenerateAccessToken(c *gin.Context) {
|
|||||||
randI := common.GetRandomInt(4)
|
randI := common.GetRandomInt(4)
|
||||||
key, err := common.GenerateRandomKey(29 + randI)
|
key, err := common.GenerateRandomKey(29 + randI)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusOK, gin.H{
|
common.ApiErrorI18n(c, i18n.MsgGenerateFailed)
|
||||||
"success": false,
|
|
||||||
"message": "生成失败",
|
|
||||||
})
|
|
||||||
common.SysLog("failed to generate key: " + err.Error())
|
common.SysLog("failed to generate key: " + err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
user.SetAccessToken(key)
|
user.SetAccessToken(key)
|
||||||
|
|
||||||
if model.DB.Where("access_token = ?", user.AccessToken).First(user).RowsAffected != 0 {
|
if model.DB.Where("access_token = ?", user.AccessToken).First(user).RowsAffected != 0 {
|
||||||
c.JSON(http.StatusOK, gin.H{
|
common.ApiErrorI18n(c, i18n.MsgUuidDuplicate)
|
||||||
"success": false,
|
|
||||||
"message": "请重试,系统生成的 UUID 竟然重复了!",
|
|
||||||
})
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -658,18 +650,12 @@ func UpdateSelf(c *gin.Context) {
|
|||||||
var user model.User
|
var user model.User
|
||||||
requestDataBytes, err := json.Marshal(requestData)
|
requestDataBytes, err := json.Marshal(requestData)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusOK, gin.H{
|
common.ApiErrorI18n(c, i18n.MsgInvalidParams)
|
||||||
"success": false,
|
|
||||||
"message": "无效的参数",
|
|
||||||
})
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
err = json.Unmarshal(requestDataBytes, &user)
|
err = json.Unmarshal(requestDataBytes, &user)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusOK, gin.H{
|
common.ApiErrorI18n(c, i18n.MsgInvalidParams)
|
||||||
"success": false,
|
|
||||||
"message": "无效的参数",
|
|
||||||
})
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -677,10 +663,7 @@ func UpdateSelf(c *gin.Context) {
|
|||||||
user.Password = "$I_LOVE_U" // make Validator happy :)
|
user.Password = "$I_LOVE_U" // make Validator happy :)
|
||||||
}
|
}
|
||||||
if err := common.Validate.Struct(&user); err != nil {
|
if err := common.Validate.Struct(&user); err != nil {
|
||||||
c.JSON(http.StatusOK, gin.H{
|
common.ApiErrorI18n(c, i18n.MsgInvalidInput)
|
||||||
"success": false,
|
|
||||||
"message": "输入不合法 " + err.Error(),
|
|
||||||
})
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -996,6 +979,10 @@ func TopUp(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
quota, err := model.Redeem(req.Key, id)
|
quota, err := model.Redeem(req.Key, id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if errors.Is(err, model.ErrRedeemFailed) {
|
||||||
|
common.ApiErrorI18n(c, i18n.MsgRedeemFailed)
|
||||||
|
return
|
||||||
|
}
|
||||||
common.ApiError(c, err)
|
common.ApiError(c, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -1149,10 +1136,7 @@ func UpdateUserSetting(c *gin.Context) {
|
|||||||
// 更新用户设置
|
// 更新用户设置
|
||||||
user.SetSetting(settings)
|
user.SetSetting(settings)
|
||||||
if err := user.Update(false); err != nil {
|
if err := user.Update(false); err != nil {
|
||||||
c.JSON(http.StatusOK, gin.H{
|
common.ApiErrorI18n(c, i18n.MsgUpdateFailed)
|
||||||
"success": false,
|
|
||||||
"message": "更新设置失败: " + err.Error(),
|
|
||||||
})
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -268,3 +268,11 @@ const (
|
|||||||
MsgOAuthGetUserErr = "oauth.get_user_error"
|
MsgOAuthGetUserErr = "oauth.get_user_error"
|
||||||
MsgOAuthAccountUsed = "oauth.account_used"
|
MsgOAuthAccountUsed = "oauth.account_used"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Model layer error messages (for translation in controller)
|
||||||
|
const (
|
||||||
|
MsgRedeemFailed = "redeem.failed"
|
||||||
|
MsgCreateDefaultTokenErr = "user.create_default_token_error"
|
||||||
|
MsgUuidDuplicate = "common.uuid_duplicate"
|
||||||
|
MsgInvalidInput = "common.invalid_input"
|
||||||
|
)
|
||||||
|
|||||||
@@ -223,3 +223,9 @@ ability.repair_running: "A repair task is already running, please try again late
|
|||||||
oauth.invalid_code: "Invalid authorization code"
|
oauth.invalid_code: "Invalid authorization code"
|
||||||
oauth.get_user_error: "Failed to get user information"
|
oauth.get_user_error: "Failed to get user information"
|
||||||
oauth.account_used: "This account has been bound to another user"
|
oauth.account_used: "This account has been bound to another user"
|
||||||
|
|
||||||
|
# Model layer error messages
|
||||||
|
redeem.failed: "Redemption failed, please try again later"
|
||||||
|
user.create_default_token_error: "Failed to create default token"
|
||||||
|
common.uuid_duplicate: "Please retry, the system generated a duplicate UUID!"
|
||||||
|
common.invalid_input: "Invalid input"
|
||||||
|
|||||||
@@ -224,3 +224,9 @@ ability.repair_running: "已经有一个修复任务在运行中,请稍后再
|
|||||||
oauth.invalid_code: "无效的授权码"
|
oauth.invalid_code: "无效的授权码"
|
||||||
oauth.get_user_error: "获取用户信息失败"
|
oauth.get_user_error: "获取用户信息失败"
|
||||||
oauth.account_used: "该账户已被其他用户绑定"
|
oauth.account_used: "该账户已被其他用户绑定"
|
||||||
|
|
||||||
|
# Model layer error messages
|
||||||
|
redeem.failed: "兑换失败,请稍后重试"
|
||||||
|
user.create_default_token_error: "创建默认令牌失败"
|
||||||
|
common.uuid_duplicate: "请重试,系统生成的 UUID 竟然重复了!"
|
||||||
|
common.invalid_input: "输入不合法"
|
||||||
|
|||||||
@@ -11,6 +11,9 @@ import (
|
|||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// ErrRedeemFailed is returned when redemption fails due to database error
|
||||||
|
var ErrRedeemFailed = errors.New("redeem.failed")
|
||||||
|
|
||||||
type Redemption struct {
|
type Redemption struct {
|
||||||
Id int `json:"id"`
|
Id int `json:"id"`
|
||||||
UserId int `json:"user_id"`
|
UserId int `json:"user_id"`
|
||||||
@@ -149,7 +152,7 @@ func Redeem(key string, userId int) (quota int, err error) {
|
|||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
common.SysError("redemption failed: " + err.Error())
|
common.SysError("redemption failed: " + err.Error())
|
||||||
return 0, errors.New("兑换失败,请稍后重试")
|
return 0, ErrRedeemFailed
|
||||||
}
|
}
|
||||||
RecordLog(userId, LogTypeTopup, fmt.Sprintf("通过兑换码充值 %s,兑换码ID %d", logger.LogQuota(redemption.Quota), redemption.Id))
|
RecordLog(userId, LogTypeTopup, fmt.Sprintf("通过兑换码充值 %s,兑换码ID %d", logger.LogQuota(redemption.Quota), redemption.Id))
|
||||||
return redemption.Quota, nil
|
return redemption.Quota, nil
|
||||||
|
|||||||
Reference in New Issue
Block a user