mirror of
https://github.com/songquanpeng/one-api.git
synced 2025-11-09 02:03:42 +08:00
refactor: update UI text and error messages to English for better accessibility
This commit is contained in:
@@ -40,7 +40,7 @@ func SearchRedemptions(keyword string) (redemptions []*Redemption, err error) {
|
||||
|
||||
func GetRedemptionById(id int) (*Redemption, error) {
|
||||
if id == 0 {
|
||||
return nil, errors.New("id 为空!")
|
||||
return nil, errors.New("id is empty!")
|
||||
}
|
||||
redemption := Redemption{Id: id}
|
||||
var err error = nil
|
||||
@@ -50,10 +50,10 @@ func GetRedemptionById(id int) (*Redemption, error) {
|
||||
|
||||
func Redeem(key string, userId int) (quota int64, err error) {
|
||||
if key == "" {
|
||||
return 0, errors.New("未提供兑换码")
|
||||
return 0, errors.New("No redemption code provided")
|
||||
}
|
||||
if userId == 0 {
|
||||
return 0, errors.New("无效的 user id")
|
||||
return 0, errors.New("Invalid user id")
|
||||
}
|
||||
redemption := &Redemption{}
|
||||
|
||||
@@ -65,10 +65,10 @@ func Redeem(key string, userId int) (quota int64, err error) {
|
||||
err = DB.Transaction(func(tx *gorm.DB) error {
|
||||
err := tx.Set("gorm:query_option", "FOR UPDATE").Where(keyCol+" = ?", key).First(redemption).Error
|
||||
if err != nil {
|
||||
return errors.New("无效的兑换码")
|
||||
return errors.New("Invalid redemption code")
|
||||
}
|
||||
if redemption.Status != RedemptionCodeStatusEnabled {
|
||||
return errors.New("该兑换码已被使用")
|
||||
return errors.New("The redemption code has been used")
|
||||
}
|
||||
err = tx.Model(&User{}).Where("id = ?", userId).Update("quota", gorm.Expr("quota + ?", redemption.Quota)).Error
|
||||
if err != nil {
|
||||
@@ -80,9 +80,9 @@ func Redeem(key string, userId int) (quota int64, err error) {
|
||||
return err
|
||||
})
|
||||
if err != nil {
|
||||
return 0, errors.New("兑换失败," + err.Error())
|
||||
return 0, errors.New("Redeem失败," + err.Error())
|
||||
}
|
||||
RecordLog(userId, LogTypeTopup, fmt.Sprintf("通过兑换码充值 %s", common.LogQuota(redemption.Quota)))
|
||||
RecordLog(userId, LogTypeTopup, fmt.Sprintf("Recharge %s through redemption code", common.LogQuota(redemption.Quota)))
|
||||
return redemption.Quota, nil
|
||||
}
|
||||
|
||||
@@ -112,7 +112,7 @@ func (redemption *Redemption) Delete() error {
|
||||
|
||||
func DeleteRedemptionById(id int) (err error) {
|
||||
if id == 0 {
|
||||
return errors.New("id 为空!")
|
||||
return errors.New("id is empty!")
|
||||
}
|
||||
redemption := Redemption{Id: id}
|
||||
err = DB.Where(redemption).First(&redemption).Error
|
||||
|
||||
@@ -60,7 +60,7 @@ func SearchUserTokens(userId int, keyword string) (tokens []*Token, err error) {
|
||||
|
||||
func ValidateUserToken(key string) (token *Token, err error) {
|
||||
if key == "" {
|
||||
return nil, errors.New("未提供令牌")
|
||||
return nil, errors.New("No token provided")
|
||||
}
|
||||
token, err = CacheGetTokenByKey(key)
|
||||
if err != nil {
|
||||
@@ -72,12 +72,12 @@ func ValidateUserToken(key string) (token *Token, err error) {
|
||||
return nil, errors.Wrap(err, "failed to get token by key")
|
||||
}
|
||||
if token.Status == TokenStatusExhausted {
|
||||
return nil, fmt.Errorf("令牌 %s(#%d)额度已用尽", token.Name, token.Id)
|
||||
return nil, fmt.Errorf("API Keys %s(#%d)Quota已用尽", token.Name, token.Id)
|
||||
} else if token.Status == TokenStatusExpired {
|
||||
return nil, errors.New("该令牌已过期")
|
||||
return nil, errors.New("The token has expired")
|
||||
}
|
||||
if token.Status != TokenStatusEnabled {
|
||||
return nil, errors.New("该令牌状态不可用")
|
||||
return nil, errors.New("The token status is not available")
|
||||
}
|
||||
if token.ExpiredTime != -1 && token.ExpiredTime < helper.GetTimestamp() {
|
||||
if !common.RedisEnabled {
|
||||
@@ -87,7 +87,7 @@ func ValidateUserToken(key string) (token *Token, err error) {
|
||||
logger.SysError("failed to update token status" + err.Error())
|
||||
}
|
||||
}
|
||||
return nil, errors.New("该令牌已过期")
|
||||
return nil, errors.New("The token has expired")
|
||||
}
|
||||
if !token.UnlimitedQuota && token.RemainQuota <= 0 {
|
||||
if !common.RedisEnabled {
|
||||
@@ -98,14 +98,14 @@ func ValidateUserToken(key string) (token *Token, err error) {
|
||||
logger.SysError("failed to update token status" + err.Error())
|
||||
}
|
||||
}
|
||||
return nil, errors.New("该令牌额度已用尽")
|
||||
return nil, errors.New("The token quota has been used up")
|
||||
}
|
||||
return token, nil
|
||||
}
|
||||
|
||||
func GetTokenByIds(id int, userId int) (*Token, error) {
|
||||
if id == 0 || userId == 0 {
|
||||
return nil, errors.New("id 或 userId 为空!")
|
||||
return nil, errors.New("id or userId is empty!")
|
||||
}
|
||||
token := Token{Id: id, UserId: userId}
|
||||
var err error = nil
|
||||
@@ -115,7 +115,7 @@ func GetTokenByIds(id int, userId int) (*Token, error) {
|
||||
|
||||
func GetTokenById(id int) (*Token, error) {
|
||||
if id == 0 {
|
||||
return nil, errors.New("id 为空!")
|
||||
return nil, errors.New("id is empty!")
|
||||
}
|
||||
token := Token{Id: id}
|
||||
var err error = nil
|
||||
@@ -160,7 +160,7 @@ func (t *Token) GetModels() string {
|
||||
func DeleteTokenById(id int, userId int) (err error) {
|
||||
// Why we need userId here? In case user want to delete other's token.
|
||||
if id == 0 || userId == 0 {
|
||||
return errors.New("id 或 userId 为空!")
|
||||
return errors.New("id or userId is empty!")
|
||||
}
|
||||
token := Token{Id: id, UserId: userId}
|
||||
err = DB.Where(token).First(&token).Error
|
||||
@@ -172,7 +172,7 @@ func DeleteTokenById(id int, userId int) (err error) {
|
||||
|
||||
func IncreaseTokenQuota(id int, quota int64) (err error) {
|
||||
if quota < 0 {
|
||||
return errors.New("quota 不能为负数!")
|
||||
return errors.New("quota cannot be negative!")
|
||||
}
|
||||
if config.BatchUpdateEnabled {
|
||||
addNewRecord(BatchUpdateTypeTokenQuota, id, quota)
|
||||
@@ -194,7 +194,7 @@ func increaseTokenQuota(id int, quota int64) (err error) {
|
||||
|
||||
func DecreaseTokenQuota(id int, quota int64) (err error) {
|
||||
if quota < 0 {
|
||||
return errors.New("quota 不能为负数!")
|
||||
return errors.New("quota cannot be negative!")
|
||||
}
|
||||
if config.BatchUpdateEnabled {
|
||||
addNewRecord(BatchUpdateTypeTokenQuota, id, -quota)
|
||||
@@ -216,21 +216,21 @@ func decreaseTokenQuota(id int, quota int64) (err error) {
|
||||
|
||||
func PreConsumeTokenQuota(tokenId int, quota int64) (err error) {
|
||||
if quota < 0 {
|
||||
return errors.New("quota 不能为负数!")
|
||||
return errors.New("quota cannot be negative!")
|
||||
}
|
||||
token, err := GetTokenById(tokenId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !token.UnlimitedQuota && token.RemainQuota < quota {
|
||||
return errors.New("令牌额度不足")
|
||||
return errors.New("Insufficient token quota")
|
||||
}
|
||||
userQuota, err := GetUserQuota(token.UserId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if userQuota < quota {
|
||||
return errors.New("用户额度不足")
|
||||
return errors.New("Insufficient user quota")
|
||||
}
|
||||
quotaTooLow := userQuota >= config.QuotaRemindThreshold && userQuota-quota < config.QuotaRemindThreshold
|
||||
noMoreQuota := userQuota-quota <= 0
|
||||
@@ -240,14 +240,14 @@ func PreConsumeTokenQuota(tokenId int, quota int64) (err error) {
|
||||
if err != nil {
|
||||
logger.SysError("failed to fetch user email: " + err.Error())
|
||||
}
|
||||
prompt := "您的额度即将用尽"
|
||||
prompt := "Your quota is about to run out"
|
||||
if noMoreQuota {
|
||||
prompt = "您的额度已用尽"
|
||||
prompt = "Your quota has been used up"
|
||||
}
|
||||
if email != "" {
|
||||
topUpLink := fmt.Sprintf("%s/topup", config.ServerAddress)
|
||||
err = message.SendEmail(prompt, email,
|
||||
fmt.Sprintf("%s,当前剩余额度为 %d,为了不影响您的使用,请及时充值。<br/>充值链接:<a href='%s'>%s</a>", prompt, userQuota, topUpLink, topUpLink))
|
||||
fmt.Sprintf("%s, the current remaining quota is %d, in order not to affect your use, please recharge in time. <br/> Recharge link: <a href='%s'>%s</a>", prompt, userQuota, topUpLink, topUpLink))
|
||||
if err != nil {
|
||||
logger.SysError("failed to send email" + err.Error())
|
||||
}
|
||||
|
||||
@@ -86,7 +86,7 @@ func SearchUsers(keyword string) (users []*User, err error) {
|
||||
|
||||
func GetUserById(id int, selectAll bool) (*User, error) {
|
||||
if id == 0 {
|
||||
return nil, errors.New("id 为空!")
|
||||
return nil, errors.New("id is empty!")
|
||||
}
|
||||
user := User{Id: id}
|
||||
var err error = nil
|
||||
@@ -100,7 +100,7 @@ func GetUserById(id int, selectAll bool) (*User, error) {
|
||||
|
||||
func GetUserIdByAffCode(affCode string) (int, error) {
|
||||
if affCode == "" {
|
||||
return 0, errors.New("affCode 为空!")
|
||||
return 0, errors.New("affCode is empty!")
|
||||
}
|
||||
var user User
|
||||
err := DB.Select("id").First(&user, "aff_code = ?", affCode).Error
|
||||
@@ -109,7 +109,7 @@ func GetUserIdByAffCode(affCode string) (int, error) {
|
||||
|
||||
func DeleteUserById(id int) (err error) {
|
||||
if id == 0 {
|
||||
return errors.New("id 为空!")
|
||||
return errors.New("id is empty!")
|
||||
}
|
||||
user := User{Id: id}
|
||||
return user.Delete()
|
||||
@@ -131,16 +131,16 @@ func (user *User) Insert(inviterId int) error {
|
||||
return result.Error
|
||||
}
|
||||
if config.QuotaForNewUser > 0 {
|
||||
RecordLog(user.Id, LogTypeSystem, fmt.Sprintf("新用户注册赠送 %s", common.LogQuota(config.QuotaForNewUser)))
|
||||
RecordLog(user.Id, LogTypeSystem, fmt.Sprintf("New user registration gives %s", common.LogQuota(config.QuotaForNewUser)))
|
||||
}
|
||||
if inviterId != 0 {
|
||||
if config.QuotaForInvitee > 0 {
|
||||
_ = IncreaseUserQuota(user.Id, config.QuotaForInvitee)
|
||||
RecordLog(user.Id, LogTypeSystem, fmt.Sprintf("使用邀请码赠送 %s", common.LogQuota(config.QuotaForInvitee)))
|
||||
RecordLog(user.Id, LogTypeSystem, fmt.Sprintf("Use invitation code to give %s", common.LogQuota(config.QuotaForInvitee)))
|
||||
}
|
||||
if config.QuotaForInviter > 0 {
|
||||
_ = IncreaseUserQuota(inviterId, config.QuotaForInviter)
|
||||
RecordLog(inviterId, LogTypeSystem, fmt.Sprintf("邀请用户赠送 %s", common.LogQuota(config.QuotaForInviter)))
|
||||
RecordLog(inviterId, LogTypeSystem, fmt.Sprintf("Invite users to give %s", common.LogQuota(config.QuotaForInviter)))
|
||||
}
|
||||
}
|
||||
// create default token
|
||||
@@ -181,7 +181,7 @@ func (user *User) Update(updatePassword bool) error {
|
||||
|
||||
func (user *User) Delete() error {
|
||||
if user.Id == 0 {
|
||||
return errors.New("id 为空!")
|
||||
return errors.New("id is empty!")
|
||||
}
|
||||
blacklist.BanUser(user.Id)
|
||||
user.Username = fmt.Sprintf("deleted_%s", random.GetUUID())
|
||||
@@ -197,7 +197,7 @@ func (user *User) ValidateAndFill() (err error) {
|
||||
// it won’t be used to build query conditions
|
||||
password := user.Password
|
||||
if user.Username == "" || password == "" {
|
||||
return errors.New("用户名或密码为空")
|
||||
return errors.New("Username or password is empty")
|
||||
}
|
||||
err = DB.Where("username = ?", user.Username).First(user).Error
|
||||
if err != nil {
|
||||
@@ -205,19 +205,19 @@ func (user *User) ValidateAndFill() (err error) {
|
||||
// consider this case: a malicious user set his username as other's email
|
||||
err := DB.Where("email = ?", user.Username).First(user).Error
|
||||
if err != nil {
|
||||
return errors.New("用户名或密码错误,或用户已被封禁")
|
||||
return errors.New("Username or password is wrong, or user has been banned")
|
||||
}
|
||||
}
|
||||
okay := common.ValidatePasswordAndHash(password, user.Password)
|
||||
if !okay || user.Status != UserStatusEnabled {
|
||||
return errors.New("用户名或密码错误,或用户已被封禁")
|
||||
return errors.New("Username or password is wrong, or user has been banned")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (user *User) FillUserById() error {
|
||||
if user.Id == 0 {
|
||||
return errors.New("id 为空!")
|
||||
return errors.New("id is empty!")
|
||||
}
|
||||
DB.Where(User{Id: user.Id}).First(user)
|
||||
return nil
|
||||
@@ -225,7 +225,7 @@ func (user *User) FillUserById() error {
|
||||
|
||||
func (user *User) FillUserByEmail() error {
|
||||
if user.Email == "" {
|
||||
return errors.New("email 为空!")
|
||||
return errors.New("email is empty!")
|
||||
}
|
||||
DB.Where(User{Email: user.Email}).First(user)
|
||||
return nil
|
||||
@@ -233,7 +233,7 @@ func (user *User) FillUserByEmail() error {
|
||||
|
||||
func (user *User) FillUserByGitHubId() error {
|
||||
if user.GitHubId == "" {
|
||||
return errors.New("GitHub id 为空!")
|
||||
return errors.New("GitHub id is empty!")
|
||||
}
|
||||
DB.Where(User{GitHubId: user.GitHubId}).First(user)
|
||||
return nil
|
||||
@@ -241,7 +241,7 @@ func (user *User) FillUserByGitHubId() error {
|
||||
|
||||
func (user *User) FillUserByLarkId() error {
|
||||
if user.LarkId == "" {
|
||||
return errors.New("lark id 为空!")
|
||||
return errors.New("lark id is empty!")
|
||||
}
|
||||
DB.Where(User{LarkId: user.LarkId}).First(user)
|
||||
return nil
|
||||
@@ -249,7 +249,7 @@ func (user *User) FillUserByLarkId() error {
|
||||
|
||||
func (user *User) FillUserByOidcId() error {
|
||||
if user.OidcId == "" {
|
||||
return errors.New("oidc id 为空!")
|
||||
return errors.New("oidc id is empty!")
|
||||
}
|
||||
DB.Where(User{OidcId: user.OidcId}).First(user)
|
||||
return nil
|
||||
@@ -257,7 +257,7 @@ func (user *User) FillUserByOidcId() error {
|
||||
|
||||
func (user *User) FillUserByWeChatId() error {
|
||||
if user.WeChatId == "" {
|
||||
return errors.New("WeChat id 为空!")
|
||||
return errors.New("WeChat id is empty!")
|
||||
}
|
||||
DB.Where(User{WeChatId: user.WeChatId}).First(user)
|
||||
return nil
|
||||
@@ -265,7 +265,7 @@ func (user *User) FillUserByWeChatId() error {
|
||||
|
||||
func (user *User) FillUserByUsername() error {
|
||||
if user.Username == "" {
|
||||
return errors.New("username 为空!")
|
||||
return errors.New("username is empty!")
|
||||
}
|
||||
DB.Where(User{Username: user.Username}).First(user)
|
||||
return nil
|
||||
@@ -297,7 +297,7 @@ func IsUsernameAlreadyTaken(username string) bool {
|
||||
|
||||
func ResetUserPasswordByEmail(email string, password string) error {
|
||||
if email == "" || password == "" {
|
||||
return errors.New("邮箱地址或密码为空!")
|
||||
return errors.New("Email address or password is empty!")
|
||||
}
|
||||
hashedPassword, err := common.Password2Hash(password)
|
||||
if err != nil {
|
||||
@@ -371,7 +371,7 @@ func GetUserGroup(id int) (group string, err error) {
|
||||
|
||||
func IncreaseUserQuota(id int, quota int64) (err error) {
|
||||
if quota < 0 {
|
||||
return errors.New("quota 不能为负数!")
|
||||
return errors.New("quota cannot be negative!")
|
||||
}
|
||||
if config.BatchUpdateEnabled {
|
||||
addNewRecord(BatchUpdateTypeUserQuota, id, quota)
|
||||
@@ -387,7 +387,7 @@ func increaseUserQuota(id int, quota int64) (err error) {
|
||||
|
||||
func DecreaseUserQuota(id int, quota int64) (err error) {
|
||||
if quota < 0 {
|
||||
return errors.New("quota 不能为负数!")
|
||||
return errors.New("quota cannot be negative!")
|
||||
}
|
||||
if config.BatchUpdateEnabled {
|
||||
addNewRecord(BatchUpdateTypeUserQuota, id, -quota)
|
||||
|
||||
Reference in New Issue
Block a user