i18n

package
v0.18.1 Latest Latest
Warning

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

Go to latest
Published: May 6, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package i18n provides internationalization as core framework infrastructure.

The middleware detects the user's locale from the Accept-Language header and stores a localizer in the request context.

Use T for simple message translation, TData for messages with template data, and TPlural for messages with plural support. All three fall back to the message key if no localizer is present.

Locale returns the current locale string, defaulting to "en".

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Locale

func Locale(ctx context.Context) string

Locale returns the current locale from context, defaulting to "en".

func T

func T(ctx context.Context, key string) string

T translates a message by ID using the localizer from context. Falls back to the message ID if no localizer is present.

func TData

func TData(ctx context.Context, key string, data map[string]any) string

TData translates a message with template data using the localizer from context. Falls back to the message ID if no localizer is present.

func TPlural

func TPlural(ctx context.Context, key string, count int) string

TPlural translates a message with plural support using the localizer from context. Falls back to the message ID if no localizer is present.

Types

type Bundle

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

Bundle manages i18n translation bundles and locale matching. It is created and managed by the Server; apps should not create it directly.

func NewBundle

func NewBundle(defaultLang string, supported []string) (*Bundle, error)

NewBundle creates a new i18n Bundle with the given default language and supported languages. It loads built-in validation translations automatically.

func NewTestBundle

func NewTestBundle(defaultLang string, translationFSes ...fs.FS) (*Bundle, error)

NewTestBundle creates a minimal i18n Bundle for use in tests. It initializes the bundle with the given default language and English+German support, and loads translations from the given filesystems. This avoids the need for Server-based configuration.

func (*Bundle) AddTranslations

func (b *Bundle) AddTranslations(fsys fs.FS) error

AddTranslations loads translation files from an fs.FS. Files must be in a "translations/" directory matching "translations/*.toml".

func (*Bundle) LocaleMiddleware

func (b *Bundle) LocaleMiddleware() func(http.Handler) http.Handler

LocaleMiddleware returns HTTP middleware that detects the user's locale from the Accept-Language header and stores it in the request context.

func (*Bundle) RequestFuncMap

func (b *Bundle) RequestFuncMap(ctx context.Context) template.FuncMap

RequestFuncMap returns context-scoped template functions for translations.

func (*Bundle) WithLocale

func (b *Bundle) WithLocale(ctx context.Context, lang string) context.Context

WithLocale returns a new context with the given locale set.

Jump to

Keyboard shortcuts

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