mirror of
https://github.com/QuantumNous/new-api.git
synced 2026-10-28 08:35:33 +08:00
* feat(i18n): translate web console messages in the frontend and AI-client errors in the backend
Chinese text was hard-coded in API messages, stored log content, emails and
server logs, so it reached users of every interface language.
Text is now translated where its reader is:
- Web console APIs return the English source text as message_key with
message_params, and the web console translates it in all seven languages.
message keeps the English text for other API callers.
- Errors returned to AI clients, emails and notifications are translated by
the backend (en, zh-CN, zh-TW) in the language saved on the account, then
the language of the request's Accept-Language header.
- A reader who states no language still gets the original Chinese text of the
messages that were Chinese before, word for word. Gateways that use another
new-api as their upstream match that text, for example to disable a channel
whose account ran out of quota.
- Usage, top-up and system log content is stored as content_parts (key and
params) and rendered in the viewer's language. Task fail reasons and refund
reasons written by the gateway are English keys. Rows written earlier are
shown as before.
- Server logs are English.
Also:
- The saved interface language zhTW, and zh-HK, zh-MO and zh-Hant, now select
Traditional Chinese in the backend instead of Simplified Chinese.
- The built-in group descriptions are translated; descriptions an
administrator saved are shown as written.
- .agents/rules/i18n.md records the conventions. Tests in i18n/ fail on a
non-English string literal, on a web console message without translations
and on a locale key without a constant.
* feat(i18n): print server logs in Chinese when LOG_LANGUAGE asks for it
Server log lines are English by default. With LOG_LANGUAGE=zh-CN, the lines
that were Chinese before print their original Chinese text.
common.LogText formats a log line from its English format, which is also the
key of its Chinese text in common/log_text.zh-CN.json. A line without an entry
prints in English. go vet checks the arguments of every call, and
TestServerLogTexts checks that each entry still matches a call and has the
same verbs as its key.
* docs: list LOG_LANGUAGE in the docker compose example
* feat(i18n): translate every server log line and add DEFAULT_LANGUAGE
Every line the gateway prints goes through common.LogText. With
DEFAULT_LANGUAGE=zh-CN, the lines that have a Chinese text in
common/log_text.zh-CN.json print it and the others print English.
DEFAULT_LANGUAGE replaces LOG_LANGUAGE. It also sets the language of
backend messages, emails and notifications for a reader who states no
language: en, zh-CN or zh-TW, English when unset. The list of messages
that stayed Chinese for such readers is removed.
Review fixes:
- Channel test logs store the token name "Model test"; the console shows
it in the interface language.
- Task fail reasons written by the gateway are fixed sentences the console
translates. The HTTP status and the upstream detail go to the server log
when a polled task is failed.
- The auto-disable reason of a slow channel is written in DEFAULT_LANGUAGE.
Also:
- Drop the repository-scanning lint tests, the i18n rules file and its
read gate. AGENTS.md keeps the conventions in four lines.
- Payment error toasts keep their previous text and only add the
translated message key.
- Remove guards and helpers that no caller needs.
* fix(i18n): address review on language lookup, notices and error responses
- Relay requests no longer load the user a second time to find the
language: the settings TokenAuth put into the request are used as they
are, also when they hold no language.
- Upstream model update notices are rendered in each recipient's saved
language, DEFAULT_LANGUAGE when none.
- /api/usage/token and /api/log/token answer with backend-translated
messages again, with the keys and status codes used before this PR.
Their callers are scripts, not the web console.
- Webhook notifications fill {{value}} placeholders like the other
senders. Running the rendered text through Sprintf appended
%!(EXTRA ...) and broke %2F in links.
- RootUserLanguage reads the setting of the root user it already loaded.
- zh_TW and zh_HK are read as Traditional Chinese.
- Add common.ApiErrorStatus for error responses under another HTTP status
and use it where those responses were built by hand.
- Remove middleware.GetLanguage, which had no callers.
279 lines
8.4 KiB
Go
279 lines
8.4 KiB
Go
package model
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
|
|
"github.com/QuantumNous/new-api/common"
|
|
"github.com/QuantumNous/new-api/constant"
|
|
"github.com/QuantumNous/new-api/relaykit/dto"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
const userCacheSchemaVersion = 2
|
|
|
|
type UserBase struct {
|
|
Id int `json:"id"`
|
|
Group string `json:"group"`
|
|
Email string `json:"email"`
|
|
Quota int `json:"quota"`
|
|
Status int `json:"status"`
|
|
Role int `json:"role"`
|
|
Username string `json:"username"`
|
|
Setting string `json:"setting"`
|
|
AuthVersion int64 `json:"-"`
|
|
CacheSchema int `json:"-"`
|
|
}
|
|
|
|
func (user *UserBase) WriteContext(c *gin.Context) {
|
|
common.SetContextKey(c, constant.ContextKeyUserGroup, user.Group)
|
|
common.SetContextKey(c, constant.ContextKeyUserQuota, user.Quota)
|
|
common.SetContextKey(c, constant.ContextKeyUserStatus, user.Status)
|
|
common.SetContextKey(c, constant.ContextKeyUserEmail, user.Email)
|
|
common.SetContextKey(c, constant.ContextKeyUserName, user.Username)
|
|
common.SetContextKey(c, constant.ContextKeyUserSetting, user.GetSetting())
|
|
}
|
|
|
|
func (user *UserBase) GetSetting() dto.UserSetting {
|
|
setting := dto.UserSetting{}
|
|
if user.Setting != "" {
|
|
err := common.Unmarshal([]byte(user.Setting), &setting)
|
|
if err != nil {
|
|
common.SysLog(common.LogText("failed to unmarshal setting: %s", err.Error()))
|
|
}
|
|
}
|
|
return setting
|
|
}
|
|
|
|
// getUserCacheKey returns the key for user cache
|
|
func getUserCacheKey(userId int) string {
|
|
return fmt.Sprintf("user:%d", userId)
|
|
}
|
|
|
|
func userCacheTTLSeconds() int {
|
|
ttl := common.RedisKeyCacheSeconds()
|
|
if ttl <= 0 {
|
|
return 60
|
|
}
|
|
return ttl
|
|
}
|
|
|
|
// invalidateUserCache clears user cache
|
|
func invalidateUserCache(userId int) error {
|
|
if !common.RedisEnabled {
|
|
return nil
|
|
}
|
|
return common.RedisDelKey(getUserCacheKey(userId))
|
|
}
|
|
|
|
func populateUserCache(user User) error {
|
|
if !common.RedisEnabled {
|
|
return nil
|
|
}
|
|
return writeUserCache(user.ToBaseUser(), true)
|
|
}
|
|
|
|
// updateUserCache refreshes non-quota user cache fields.
|
|
// Quota is maintained by atomic quota delta paths and must not be overwritten
|
|
// by stale user snapshots from profile/settings updates.
|
|
func updateUserCache(user User) error {
|
|
if !common.RedisEnabled {
|
|
return nil
|
|
}
|
|
return writeUserCache(user.ToBaseUser(), false)
|
|
}
|
|
|
|
// GetUserCache gets complete user cache from hash
|
|
func GetUserCache(userId int) (*UserBase, error) {
|
|
// Try getting from Redis first
|
|
userCache, err := cacheGetUserBase(userId)
|
|
if err == nil {
|
|
return userCache, nil
|
|
}
|
|
|
|
// Redis misses and read failures both fall back to the shared database. A
|
|
// version fence newer than the database is the one exception: allowing that
|
|
// snapshot would re-authorize a user while a restrictive update is pending.
|
|
user, err := GetUserById(userId, false)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if common.RedisEnabled {
|
|
floor, floorErr := getUserAuthVersionFloor(userId)
|
|
if floorErr == nil && floor > user.AuthVersion {
|
|
return nil, ErrUserAuthCachePending
|
|
}
|
|
if err := populateUserCache(*user); err != nil {
|
|
if errors.Is(err, ErrUserAuthCachePending) {
|
|
return nil, err
|
|
}
|
|
common.SysLog(common.LogText("failed to synchronously populate user cache: %s", err.Error()))
|
|
}
|
|
}
|
|
return user.ToBaseUser(), nil
|
|
}
|
|
|
|
func cacheGetUserBase(userId int) (*UserBase, error) {
|
|
if !common.RedisEnabled {
|
|
return nil, fmt.Errorf("redis is not enabled")
|
|
}
|
|
var userCache UserBase
|
|
// Try getting from Redis first
|
|
err := common.RedisHGetObj(getUserCacheKey(userId), &userCache)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if userCache.Id != userId || userCache.CacheSchema != userCacheSchemaVersion || userCache.AuthVersion <= 0 {
|
|
return nil, fmt.Errorf("user cache schema is stale")
|
|
}
|
|
floor, err := getUserAuthVersionFloor(userId)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if floor > userCache.AuthVersion {
|
|
return nil, ErrUserAuthCachePending
|
|
}
|
|
return &userCache, nil
|
|
}
|
|
|
|
// Add atomic quota operations using hash fields.
|
|
// 通过守卫式 Lua 脚本执行:哈希不存在时直接跳过(下次读取会从数据库水合),
|
|
// 不会像裸 HINCRBY 那样创建只含 Quota 字段的残缺哈希。
|
|
func cacheIncrUserQuota(userId int, delta int64) error {
|
|
if !common.RedisEnabled {
|
|
return nil
|
|
}
|
|
_, err := cacheApplyUserQuotaDelta(userId, delta)
|
|
return err
|
|
}
|
|
|
|
func cacheDecrUserQuota(userId int, delta int64) error {
|
|
return cacheIncrUserQuota(userId, -delta)
|
|
}
|
|
|
|
// syncCreditUserQuotaCache 在授信事务(充值/兑换等)提交后同步把增量补进缓存
|
|
// 余额。预扣以缓存值为准(存在期间),授信不能绕过它,否则新到账的额度在
|
|
// 缓存过期前不可用;缓存未命中无需处理,下次读取会从已提交的数据库余额水合。
|
|
func syncCreditUserQuotaCache(userId int, quota int, operation string) {
|
|
if quota <= 0 {
|
|
return
|
|
}
|
|
if err := cacheIncrUserQuota(userId, int64(quota)); err != nil {
|
|
common.SysLog(common.LogText("failed to sync %s credit to user quota cache: %s", operation, err.Error()))
|
|
}
|
|
}
|
|
|
|
// Helper functions to get individual fields if needed
|
|
func getUserGroupCache(userId int) (string, error) {
|
|
cache, err := GetUserCache(userId)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return cache.Group, nil
|
|
}
|
|
|
|
func getUserQuotaCache(userId int) (int, error) {
|
|
cache, err := GetUserCache(userId)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return cache.Quota, nil
|
|
}
|
|
|
|
func getUserNameCache(userId int) (string, error) {
|
|
cache, err := GetUserCache(userId)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return cache.Username, nil
|
|
}
|
|
|
|
func getUserSettingCache(userId int) (dto.UserSetting, error) {
|
|
cache, err := GetUserCache(userId)
|
|
if err != nil {
|
|
return dto.UserSetting{}, err
|
|
}
|
|
return cache.GetSetting(), nil
|
|
}
|
|
|
|
// RefreshUserGroupCache writes the database-authoritative group into an
|
|
// existing user hash without changing the user's authentication version.
|
|
func RefreshUserGroupCache(userId int) error {
|
|
if !common.RedisEnabled {
|
|
return nil
|
|
}
|
|
if userId <= 0 {
|
|
return fmt.Errorf("invalid user id")
|
|
}
|
|
var authoritative User
|
|
if err := DB.Select("id", "auth_version", commonGroupCol).Where("id = ?", userId).First(&authoritative).Error; err != nil {
|
|
return err
|
|
}
|
|
// Group transitions intentionally keep the same authentication version. A
|
|
// refresh that read the previous group can therefore arrive after a newer
|
|
// refresh and still pass the auth-version fence. Re-read after every write
|
|
// and repair the cache when the authoritative group changed in between.
|
|
for range 3 {
|
|
if err := updateUserCacheFieldAtVersion(userId, "Group", authoritative.Group, authoritative.AuthVersion); err != nil {
|
|
return err
|
|
}
|
|
|
|
var verified User
|
|
if err := DB.Select("id", "auth_version", commonGroupCol).Where("id = ?", userId).First(&verified).Error; err != nil {
|
|
return err
|
|
}
|
|
if verified.AuthVersion == authoritative.AuthVersion && verified.Group == authoritative.Group {
|
|
return nil
|
|
}
|
|
authoritative = verified
|
|
}
|
|
|
|
// Preserve the freshest snapshot observed even when the row was too busy to
|
|
// stabilize within the bounded retries. Returning an error lets best-effort
|
|
// callers emit an operation-specific warning.
|
|
if err := updateUserCacheFieldAtVersion(userId, "Group", authoritative.Group, authoritative.AuthVersion); err != nil {
|
|
return err
|
|
}
|
|
return fmt.Errorf("user group changed repeatedly during cache refresh")
|
|
}
|
|
|
|
func updateUserEmailCache(userId int, email string) error {
|
|
return updateUserCacheField(userId, "Email", email)
|
|
}
|
|
|
|
func updateUserNameCache(userId int, username string) error {
|
|
return updateUserCacheField(userId, "Username", username)
|
|
}
|
|
|
|
func updateUserSettingCache(userId int, setting string) error {
|
|
return updateUserCacheField(userId, "Setting", setting)
|
|
}
|
|
|
|
// updateUserCacheField prevents individual cache refreshes from bypassing the
|
|
// auth-version fence. It intentionally does nothing when the complete hash is
|
|
// absent; the next GetUserCache call will repopulate it from the database.
|
|
func updateUserCacheField(userId int, field string, value any) error {
|
|
if !common.RedisEnabled {
|
|
return nil
|
|
}
|
|
var user User
|
|
if err := DB.Select("id", "auth_version").Where("id = ?", userId).First(&user).Error; err != nil {
|
|
return err
|
|
}
|
|
if user.AuthVersion <= 0 {
|
|
return fmt.Errorf("invalid user auth version")
|
|
}
|
|
return updateUserCacheFieldAtVersion(userId, field, value, user.AuthVersion)
|
|
}
|
|
|
|
// GetUserLanguage returns the user's language preference from cache
|
|
// Uses the existing GetUserCache mechanism for efficiency
|
|
func GetUserLanguage(userId int) string {
|
|
userCache, err := GetUserCache(userId)
|
|
if err != nil {
|
|
return ""
|
|
}
|
|
return userCache.GetSetting().Language
|
|
}
|