i18n

package
v0.0.0-...-30b4b00 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 5, 2026 License: AGPL-3.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const (
	LangZh      = "zh"
	LangZhCN    = "zh-CN" // 兼容别名(go-i18n 文件名)
	LangZhTW    = "zh-TW"
	LangEn      = "en"
	LangJa      = "ja"
	LangRu      = "ru"
	LangFr      = "fr"
	LangVi      = "vi"
	LangAr      = "ar"
	DefaultLang = LangZh // Fallback to Chinese if language not supported
)
View Source
const (
	MsgInvalidParams     = "common.invalid_params"
	MsgDatabaseError     = "common.database_error"
	MsgRetryLater        = "common.retry_later"
	MsgGenerateFailed    = "common.generate_failed"
	MsgNotFound          = "common.not_found"
	MsgUnauthorized      = "common.unauthorized"
	MsgForbidden         = "common.forbidden"
	MsgInvalidId         = "common.invalid_id"
	MsgIdEmpty           = "common.id_empty"
	MsgFeatureDisabled   = "common.feature_disabled"
	MsgOperationSuccess  = "common.operation_success"
	MsgOperationFailed   = "common.operation_failed"
	MsgUpdateSuccess     = "common.update_success"
	MsgUpdateFailed      = "common.update_failed"
	MsgNameCannotBeEmpty = "common.name_cannot_be_empty"
	MsgBatchTooMany      = "common.batch_too_many"
)

Common error messages

View Source
const (
	MsgAuthNotLoggedIn           = "auth.not_logged_in"
	MsgAuthAccessTokenInvalid    = "auth.access_token_invalid"
	MsgAuthUserInfoInvalid       = "auth.user_info_invalid"
	MsgAuthUserIdNotProvided     = "auth.user_id_not_provided"
	MsgAuthUserIdFormatError     = "auth.user_id_format_error"
	MsgAuthUserIdMismatch        = "auth.user_id_mismatch"
	MsgAuthUserBanned            = "auth.user_banned"
	MsgAuthInsufficientPrivilege = "auth.insufficient_privilege"
)

Auth middleware messages

View Source
const (
	MsgTokenNameTooLong          = "token.name_too_long"
	MsgTokenQuotaNegative        = "token.quota_negative"
	MsgTokenQuotaExceedMax       = "token.quota_exceed_max"
	MsgTokenGenerateFailed       = "token.generate_failed"
	MsgTokenGetInfoFailed        = "token.get_info_failed"
	MsgTokenExpiredCannotEnable  = "token.expired_cannot_enable"
	MsgTokenExhaustedCannotEable = "token.exhausted_cannot_enable"
	MsgTokenInvalid              = "token.invalid"
	MsgTokenNotProvided          = "token.not_provided"
	MsgTokenExpired              = "token.expired"
	MsgTokenExhausted            = "token.exhausted"
	MsgTokenStatusUnavailable    = "token.status_unavailable"
	MsgTokenDbError              = "token.db_error"
)

Token related messages

View Source
const (
	MsgRedemptionNameLength        = "redemption.name_length"
	MsgRedemptionCountPositive     = "redemption.count_positive"
	MsgRedemptionCountMax          = "redemption.count_max"
	MsgRedemptionCreateFailed      = "redemption.create_failed"
	MsgRedemptionInvalid           = "redemption.invalid"
	MsgRedemptionUsed              = "redemption.used"
	MsgRedemptionExpired           = "redemption.expired"
	MsgRedemptionFailed            = "redemption.failed"
	MsgRedemptionNotProvided       = "redemption.not_provided"
	MsgRedemptionExpireTimeInvalid = "redemption.expire_time_invalid"
)

Redemption related messages

View Source
const (
	MsgUserPasswordLoginDisabled     = "user.password_login_disabled"
	MsgUserRegisterDisabled          = "user.register_disabled"
	MsgUserPasswordRegisterDisabled  = "user.password_register_disabled"
	MsgUserUsernameOrPasswordEmpty   = "user.username_or_password_empty"
	MsgUserUsernameOrPasswordError   = "user.username_or_password_error"
	MsgUserEmailOrPasswordEmpty      = "user.email_or_password_empty"
	MsgUserExists                    = "user.exists"
	MsgUserNotExists                 = "user.not_exists"
	MsgUserDisabled                  = "user.disabled"
	MsgUserSessionSaveFailed         = "user.session_save_failed"
	MsgUserRequire2FA                = "user.require_2fa"
	MsgUserEmailVerificationRequired = "user.email_verification_required"
	MsgUserVerificationCodeError     = "user.verification_code_error"
	MsgUserInputInvalid              = "user.input_invalid"
	MsgUserNoPermissionSameLevel     = "user.no_permission_same_level"
	MsgUserNoPermissionHigherLevel   = "user.no_permission_higher_level"
	MsgUserCannotCreateHigherLevel   = "user.cannot_create_higher_level"
	MsgUserCannotDeleteRootUser      = "user.cannot_delete_root_user"
	MsgUserCannotDisableRootUser     = "user.cannot_disable_root_user"
	MsgUserCannotDemoteRootUser      = "user.cannot_demote_root_user"
	MsgUserIpLimit                   = "user.ip_limit"
	MsgUserAlreadyAdmin              = "user.already_admin"
	MsgUserAlreadyCommon             = "user.already_common"
	MsgUserAdminCannotPromote        = "user.admin_cannot_promote"
	MsgUserOriginalPasswordError     = "user.original_password_error"
	MsgUserInviteQuotaInsufficient   = "user.invite_quota_insufficient"
	MsgUserTransferQuotaMinimum      = "user.transfer_quota_minimum"
	MsgUserTransferSuccess           = "user.transfer_success"
	MsgUserTransferFailed            = "user.transfer_failed"
	MsgUserTopUpProcessing           = "user.topup_processing"
	MsgUserRegisterFailed            = "user.register_failed"
	MsgUserRegisterSuccess           = "user.register_success"
	MsgUserDefaultTokenFailed        = "user.default_token_failed"
	MsgUserAffCodeEmpty              = "user.aff_code_empty"
	MsgUserEmailEmpty                = "user.email_empty"
	MsgUserOidcIdEmpty               = "user.oidc_id_empty"
	MsgUserWeChatIdEmpty             = "user.wechat_id_empty"
	MsgUserQuotaChangeZero           = "user.quota_change_zero"
)

User related messages

View Source
const (
	MsgQuotaNegative        = "quota.negative"
	MsgQuotaExceedMax       = "quota.exceed_max"
	MsgQuotaInsufficient    = "quota.insufficient"
	MsgQuotaWarningInvalid  = "quota.warning_invalid"
	MsgQuotaThresholdGtZero = "quota.threshold_gt_zero"
)

Quota related messages

View Source
const (
	MsgSubscriptionNotEnabled       = "subscription.not_enabled"
	MsgSubscriptionTitleEmpty       = "subscription.title_empty"
	MsgSubscriptionPriceNegative    = "subscription.price_negative"
	MsgSubscriptionPriceMax         = "subscription.price_max"
	MsgSubscriptionPurchaseLimitNeg = "subscription.purchase_limit_negative"
	MsgSubscriptionQuotaNegative    = "subscription.quota_negative"
	MsgSubscriptionGroupNotExists   = "subscription.group_not_exists"
	MsgSubscriptionResetCycleGtZero = "subscription.reset_cycle_gt_zero"
	MsgSubscriptionPurchaseMax      = "subscription.purchase_max"
	MsgSubscriptionInvalidId        = "subscription.invalid_id"
	MsgSubscriptionInvalidUserId    = "subscription.invalid_user_id"
)

Subscription related messages

View Source
const (
	MsgPaymentNotConfigured      = "payment.not_configured"
	MsgPaymentMethodNotExists    = "payment.method_not_exists"
	MsgPaymentCallbackError      = "payment.callback_error"
	MsgPaymentCreateFailed       = "payment.create_failed"
	MsgPaymentStartFailed        = "payment.start_failed"
	MsgPaymentAmountTooLow       = "payment.amount_too_low"
	MsgPaymentStripeNotConfig    = "payment.stripe_not_configured"
	MsgPaymentWebhookNotConfig   = "payment.webhook_not_configured"
	MsgPaymentPriceIdNotConfig   = "payment.price_id_not_configured"
	MsgPaymentCreemNotConfig     = "payment.creem_not_configured"
	MsgPaymentComplianceRequired = "payment.compliance_required"
)

Payment related messages

View Source
const (
	MsgTopupNotProvided    = "topup.not_provided"
	MsgTopupOrderNotExists = "topup.order_not_exists"
	MsgTopupOrderStatus    = "topup.order_status"
	MsgTopupFailed         = "topup.failed"
	MsgTopupInvalidQuota   = "topup.invalid_quota"
)

Topup related messages

View Source
const (
	MsgChannelIdFormatError      = "channel.id_format_error"
	MsgChannelGetListFailed      = "channel.get_list_failed"
	MsgChannelGetTagsFailed      = "channel.get_tags_failed"
	MsgChannelGetKeyFailed       = "channel.get_key_failed"
	MsgChannelGetOllamaFailed    = "channel.get_ollama_failed"
	MsgChannelQueryFailed        = "channel.query_failed"
	MsgChannelNoValidUpstream    = "channel.no_valid_upstream"
	MsgChannelUpstreamSaturated  = "channel.upstream_saturated"
	MsgChannelGetAvailableFailed = "channel.get_available_failed"
)

Channel related messages

View Source
const (
	MsgModelNameEmpty     = "model.name_empty"
	MsgModelNameExists    = "model.name_exists"
	MsgModelIdMissing     = "model.id_missing"
	MsgModelGetListFailed = "model.get_list_failed"
	MsgModelGetFailed     = "model.get_failed"
	MsgModelResetSuccess  = "model.reset_success"
)

Model related messages

View Source
const (
	MsgVendorNameEmpty  = "vendor.name_empty"
	MsgVendorNameExists = "vendor.name_exists"
	MsgVendorIdMissing  = "vendor.id_missing"
)

Vendor related messages

View Source
const (
	MsgGroupNameTypeEmpty = "group.name_type_empty"
	MsgGroupNameExists    = "group.name_exists"
	MsgGroupIdMissing     = "group.id_missing"
)

Group related messages

View Source
const (
	MsgCheckinFailed      = "checkin.failed"
	MsgCheckinQuotaFailed = "checkin.quota_failed"
)

Checkin related messages

View Source
const (
	MsgPasskeyCreateFailed  = "passkey.create_failed"
	MsgPasskeyLoginAbnormal = "passkey.login_abnormal"
	MsgPasskeyUpdateFailed  = "passkey.update_failed"
	MsgPasskeyInvalidUserId = "passkey.invalid_user_id"
	MsgPasskeyVerifyFailed  = "passkey.verify_failed"
)

Passkey related messages

View Source
const (
	MsgTwoFANotEnabled    = "twofa.not_enabled"
	MsgTwoFAUserIdEmpty   = "twofa.user_id_empty"
	MsgTwoFAAlreadyExists = "twofa.already_exists"
	MsgTwoFARecordIdEmpty = "twofa.record_id_empty"
	MsgTwoFACodeInvalid   = "twofa.code_invalid"
)

2FA related messages

View Source
const (
	MsgRateLimitReached      = "rate_limit.reached"
	MsgRateLimitTotalReached = "rate_limit.total_reached"
)

Rate limit related messages

View Source
const (
	MsgSettingInvalidType      = "setting.invalid_type"
	MsgSettingWebhookEmpty     = "setting.webhook_empty"
	MsgSettingWebhookInvalid   = "setting.webhook_invalid"
	MsgSettingEmailInvalid     = "setting.email_invalid"
	MsgSettingBarkUrlEmpty     = "setting.bark_url_empty"
	MsgSettingBarkUrlInvalid   = "setting.bark_url_invalid"
	MsgSettingGotifyUrlEmpty   = "setting.gotify_url_empty"
	MsgSettingGotifyTokenEmpty = "setting.gotify_token_empty"
	MsgSettingGotifyUrlInvalid = "setting.gotify_url_invalid"
	MsgSettingUrlMustHttp      = "setting.url_must_http"
	MsgSettingSaved            = "setting.saved"
)

Setting related messages

View Source
const (
	MsgPerfDiskCacheCleared = "performance.disk_cache_cleared"
	MsgPerfStatsReset       = "performance.stats_reset"
	MsgPerfGcExecuted       = "performance.gc_executed"
)

Performance related messages

View Source
const (
	MsgOAuthInvalidCode     = "oauth.invalid_code"
	MsgOAuthGetUserErr      = "oauth.get_user_error"
	MsgOAuthAccountUsed     = "oauth.account_used"
	MsgOAuthUnknownProvider = "oauth.unknown_provider"
	MsgOAuthStateInvalid    = "oauth.state_invalid"
	MsgOAuthNotEnabled      = "oauth.not_enabled"
	MsgOAuthUserDeleted     = "oauth.user_deleted"
	MsgOAuthUserBanned      = "oauth.user_banned"
	MsgOAuthBindSuccess     = "oauth.bind_success"
	MsgOAuthAlreadyBound    = "oauth.already_bound"
	MsgOAuthConnectFailed   = "oauth.connect_failed"
	MsgOAuthTokenFailed     = "oauth.token_failed"
	MsgOAuthUserInfoEmpty   = "oauth.user_info_empty"
	MsgOAuthTrustLevelLow   = "oauth.trust_level_low"
)

OAuth related messages

View Source
const (
	MsgRedeemFailed          = "redeem.failed"
	MsgCreateDefaultTokenErr = "user.create_default_token_error"
	MsgUuidDuplicate         = "common.uuid_duplicate"
	MsgInvalidInput          = "common.invalid_input"
)

Model layer error messages (for translation in controller)

View Source
const (
	MsgDistributorInvalidRequest          = "distributor.invalid_request"
	MsgDistributorInvalidChannelId        = "distributor.invalid_channel_id"
	MsgDistributorChannelDisabled         = "distributor.channel_disabled"
	MsgDistributorAffinityChannelDisabled = "distributor.affinity_channel_disabled"
	MsgDistributorTokenNoModelAccess      = "distributor.token_no_model_access"
	MsgDistributorTokenModelForbidden     = "distributor.token_model_forbidden"
	MsgDistributorModelNameRequired       = "distributor.model_name_required"
	MsgDistributorInvalidPlayground       = "distributor.invalid_playground_request"
	MsgDistributorGroupAccessDenied       = "distributor.group_access_denied"
	MsgDistributorGetChannelFailed        = "distributor.get_channel_failed"
	MsgDistributorNoAvailableChannel      = "distributor.no_available_channel"
	MsgDistributorInvalidMidjourney       = "distributor.invalid_midjourney_request"
	MsgDistributorInvalidParseModel       = "distributor.invalid_request_parse_model"
)

Distributor related messages

View Source
const (
	MsgInvalidPhone           = "user.invalid_phone"
	MsgPhoneAlreadyRegistered = "user.phone_already_registered"
	MsgPhoneNotRegistered     = "user.phone_not_registered"
	MsgInvalidSMSCode         = "user.invalid_sms_code"
	MsgSMSSent                = "user.sms_sent"
	MsgSMSVerified            = "user.sms_verified"
	MsgPhoneBindSuccess       = "user.phone_bind_success"
	MsgPhoneUnbindSuccess     = "user.phone_unbind_success"
)

Phone related messages

View Source
const (
	MsgWeChatNotAuthorized   = "user.wechat_not_authorized"
	MsgWeChatUserInfoInvalid = "user.wechat_user_info_invalid"
	MsgWeChatOpenIDMissing   = "user.wechat_openid_missing"
	MsgWeChatAlreadyBound    = "user.wechat_already_bound"
	MsgWeChatBindSuccess     = "user.wechat_bind_success"
	MsgWeChatUnbindSuccess   = "user.wechat_unbind_success"
)

WeChat binding messages

View Source
const (
	MsgCustomOAuthBindingNotFound = "custom_oauth.binding_not_found"
)

Custom OAuth provider related messages

View Source
const (
	MsgNotImplemented = "common.not_implemented"
)

Generic messages

Variables

This section is empty.

Functions

func GetLangFromContext

func GetLangFromContext(c *gin.Context) string

GetLangFromContext extracts the language setting from gin context It checks multiple sources in priority order: 1. User settings (ContextKeyUserSetting) - if already loaded (e.g., by TokenAuth) 2. Lazy load user language from cache/DB using user ID 3. Language set by middleware (ContextKeyLanguage) - from Accept-Language header 4. Default language (Chinese)

func GetLocalizer

func GetLocalizer(lang string) *i18n.Localizer

GetLocalizer returns a localizer for the specified language

func Init

func Init() error

Init initializes the i18n bundle and loads all translation files

func IsSupported

func IsSupported(lang string) bool

IsSupported checks if a language code is supported

func Msg

func Msg(c *gin.Context, src string) string

Msg returns the localized user-facing message for the current request locale. The source text (usually Chinese) is used as the translation key in the i18n_messages table; if no override exists for the locale, the source text is returned (always readable, zero regression).

func ParseAcceptLanguage

func ParseAcceptLanguage(header string) string

ParseAcceptLanguage parses the Accept-Language header and returns the preferred language

func SetMessageLoader

func SetMessageLoader(loader func(key, locale string) (string, bool))

SetMessageLoader registers the DB-backed message loader. Once set, Msg() resolves user-facing strings via the i18n_messages table so translations can be edited at runtime without redeployment (配置即数据).

func SetUserLangLoader

func SetUserLangLoader(loader func(userId int) string)

SetUserLangLoader sets the function to load user language (called from model package)

func SupportedLanguages

func SupportedLanguages() []string

SupportedLanguages returns a list of supported language codes

func T

func T(c *gin.Context, key string, args ...map[string]any) string

T translates a message key using the language from gin context

func Translate

func Translate(lang, key string, args ...map[string]any) string

Translate translates a message key for the specified language

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL