Help us shape the future of Go! Take the 2026 Go Developer Survey

i18n

package
v2.0.13 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2026 License: MIT Imports: 26 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotInitialized = errors.New("i18n: dictionary is not initialized")
	ErrInvalidEntry   = errors.New("i18n: invalid dictionary entry")
	ErrNotFound       = errors.New("i18n: dictionary entry not found")
	ErrConflict       = errors.New("i18n: messageId and language already exist")
)

Functions

func CheckParametersLost

func CheckParametersLost(params map[string]string, paramNames ...string) models.Result[any]

func Close added in v2.0.12

func Close()

Close releases only the SQLite connection owned by i18n.

func Error

func Error(code int, messageId string) models.Result[any]

func ErrorDef

func ErrorDef(def errcode.Definition, args ...any) models.Result[any]

ErrorDef v2.1 新增:基于 errcode.Definition 的标准化错误响应。

把三向映射(errcode.Definition → i18n 文案 → HTTP 状态码)一步完成:

  • 文案通过 def.MessageKey + args 走 i18n.Format
  • 业务码写入返回体的 Code 字段
  • HTTP 状态码由 def.HTTPStatus 决定(调用方需在自己的 handler 里手动 c.JSON(def.HTTPStatus, result))

返回的 result 是已渲染文案的 models.Result,业务侧调用方式:

c.JSON(errcode.LookupDef(errcode.PARAM_ERROR).HTTPStatus,
    i18n.ErrorDef(errcode.PARAM_ERROR, paramName))

func ErrorDefT

func ErrorDefT[T any](def errcode.Definition, args ...any) models.Result[T]

ErrorDefT 泛型版,行为同 ErrorDef。

func ErrorT

func ErrorT[T any](code int, messageId string) models.Result[T]

func ErrorWithMsg

func ErrorWithMsg(code int, messageId, msg string) models.Result[any]

func ErrorWithMsgT

func ErrorWithMsgT[T any](code int, messageId, msg string) models.Result[T]

func Format

func Format(messageId string, args ...any) string

Format 格式化数据,messageId对应的内容为带{}的模板

func GetString added in v2.0.12

func GetString(messageID, language string) string

GetString resolves a message from the built-in dictionary with language fallback.

func GetXLangString

func GetXLangString(messageID, language string) string

GetXLangString is kept for source compatibility and uses the local dictionary. Deprecated: use GetString.

func Init

func Init() error

Init initializes the built-in database dictionary; no external service is used.

func ParamError

func ParamError(param string) models.Result[any]

func ParamLostError

func ParamLostError(param string) models.Result[any]

func RouterGroup added in v2.0.12

func RouterGroup(group *gin.RouterGroup)

RouterGroup mounts dictionary CRUD on a caller-supplied group. GET /list lists entries; GET /get and POST /delete use messageId/language query parameters; POST /save and POST /update accept {"messageId":"hello","language":"en","text":"Hello"}.

func String

func String(messageId string) string

String 将messageId根据当前协程X-Lang参数转换成当前语言字符串

func Success

func Success[T any](data T) models.Result[T]

func SuccessWithPage

func SuccessWithPage[T any](data T, count, index, size, total int) models.Result[T]

Types

type Entry added in v2.0.12

type Entry struct {
	MessageID string    `json:"messageId" gorm:"column:message_id;size:191;primaryKey;autoIncrement:false"`
	Language  string    `json:"language" gorm:"size:35;primaryKey;autoIncrement:false"`
	Text      string    `json:"text" gorm:"type:text;not null"`
	CreatedAt time.Time `json:"createdAt"`
	UpdatedAt time.Time `json:"updatedAt"`
}

Entry stores one translation. A message can have one entry per language.

type ListOptions added in v2.0.12

type ListOptions struct {
	MessageID string
	Language  string
	Page      int
	PageSize  int
}

type Store added in v2.0.12

type Store struct {
	// contains filtered or unexported fields
}

Store uses the application's relational connection, or owns a fallback SQLite connection.

func GetStore added in v2.0.12

func GetStore() (*Store, error)

func NewStore added in v2.0.12

func NewStore(conn *gorm.DB, appName string) (*Store, error)

NewStore creates the dictionary table on an existing GORM connection. The caller retains ownership of the connection.

func (*Store) Create added in v2.0.12

func (s *Store) Create(ctx context.Context, entry *Entry) error

func (*Store) Delete added in v2.0.12

func (s *Store) Delete(ctx context.Context, messageID, language string) error

func (*Store) Get added in v2.0.12

func (s *Store) Get(ctx context.Context, messageID, language string) (*Entry, error)

func (*Store) List added in v2.0.12

func (s *Store) List(ctx context.Context, options ListOptions) ([]Entry, int64, error)

func (*Store) TableName added in v2.0.12

func (s *Store) TableName() string

func (*Store) Translate added in v2.0.12

func (s *Store) Translate(ctx context.Context, messageID, language string) (string, error)

Translate falls back to the configured default language. A missing key returns ErrNotFound.

func (*Store) Update added in v2.0.12

func (s *Store) Update(ctx context.Context, entry *Entry) error

Update changes only the translation text; the key is immutable.

Jump to

Keyboard shortcuts

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