i18n

package
v0.22.1 Latest Latest
Warning

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

Go to latest
Published: May 7, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultLanguage = "zh-CN"

DefaultLanguage is the default language for the i18n system.

Variables

View Source
var (
	// ErrUnsupportedLanguage is returned when an unsupported language code is provided.
	ErrUnsupportedLanguage = errors.New("unsupported language code")
	// ErrMessageIDEmpty is returned when a translation message ID is empty.
	ErrMessageIDEmpty = errors.New("messageID cannot be empty")
)

Functions

func GetSupportedLanguages

func GetSupportedLanguages() []string

GetSupportedLanguages returns a copy of all supported language codes.

func IsLanguageSupported

func IsLanguageSupported(languageCode string) bool

IsLanguageSupported checks if the given language code is supported.

func SetLanguage

func SetLanguage(languageCode string) error

SetLanguage changes the global translator to use the specified language. This is primarily intended for testing scenarios where you need to verify translations in different languages without restarting the process. If languageCode is empty, uses the environment variable or default language.

func T

func T(messageID string, templateData ...map[string]any) string

T translates a message ID using the global translator. Returns the messageID as fallback if translation fails.

func Te

func Te(messageID string, templateData ...map[string]any) (string, error)

Te translates a message ID with explicit error handling. Use this when you need to handle translation errors programmatically.

Types

type Config

type Config struct {
	// Locales contains the embedded locale files (JSON format).
	Locales embed.FS
}

Config defines the configuration for the i18n system.

type Translator

type Translator interface {
	// T translates a message ID to a localized string with graceful error handling.
	// If translation fails, it returns the original messageID as a fallback.
	T(messageID string, templateData ...map[string]any) string

	// Te translates a message ID to a localized string and returns explicit error information.
	// Use this when you need to distinguish between successful translation and failure.
	Te(messageID string, templateData ...map[string]any) (string, error)
}

Translator defines the interface for message translation services.

func New

func New(config Config) (Translator, error)

New creates a new translator instance with the provided configuration.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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