i18n

package
v0.60.8 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrMissingTranslation = errors.New("i18n: missing translation")

ErrMissingTranslation is returned when no translation can be resolved.

Functions

func RegisterTemplateHelpers

func RegisterTemplateHelpers() error

Types

type Config

type Config struct {
	DefaultLocale     string         `json:"default_locale"`
	Locales           []LocaleConfig `json:"locales"`
	TemplateHelperKey string         `json:"template_helper_key"`
	LocaleContextKey  string         `json:"locale_context_key"`
}

Config bridges CMS locale configuration to the i18n service.

func FromModuleConfig

func FromModuleConfig(defaultLocale string, locales []string) Config

FromModuleConfig produces the i18n config given CMS configuration values.

func (Config) Fallbacks

func (c Config) Fallbacks(locale string) []string

Fallbacks returns the configured fallback chain for a locale.

func (Config) HelperConfig

func (c Config) HelperConfig() interfaces.HelperConfig

HelperConfig converts the module config into an interfaces.HelperConfig.

func (Config) LocaleCodes

func (c Config) LocaleCodes() []string

LocaleCodes returns the set of configured locale codes.

func (Config) MarshalJSON

func (c Config) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler ensuring we serialise unique locales.

func (*Config) UnmarshalJSON

func (c *Config) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler for Config.

func (*Config) WithFallbacks

func (c *Config) WithFallbacks(locale string, fallbacks ...string)

WithFallbacks registers fallbacks for the provided locale code.

type Fixture

type Fixture struct {
	Config       Config                       `json:"config"`
	Translations map[string]map[string]string `json:"translations"`
}

Fixture represents a serialised bundle of configuration + translations.

func DefaultFixture

func DefaultFixture() (*Fixture, error)

DefaultFixture loads the built-in translation fixture.

type Loader

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

Loader reads translation fixtures from disk.

func NewLoader

func NewLoader(path string) *Loader

NewLoader constructs a loader that reads the provided file path.

func (*Loader) Load

func (l *Loader) Load(ctx context.Context) (*Fixture, error)

Load parses the configured fixture file.

type LocaleConfig

type LocaleConfig struct {
	Code      string   `json:"code"`
	Fallbacks []string `json:"fallbacks,omitempty"`
}

LocaleConfig describes a locale definition and its fallback behaviour.

type MissingTranslationStrategy

type MissingTranslationStrategy func(locale, key string, args []any, err error) string

MissingTranslationStrategy defines how helpers handle missing translations.

type NoOpService

type NoOpService struct{}

NoOpService is a placeholder that satisfies the contract without performing translations.

func (NoOpService) Culture

func (NoOpService) DefaultLocale

func (NoOpService) DefaultLocale() string

func (NoOpService) TemplateHelpers

func (NoOpService) TemplateHelpers(cfg interfaces.HelperConfig) map[string]any

func (NoOpService) Translator

func (NoOpService) Translator() interfaces.Translator

type Service

type Service interface {
	interfaces.Service
}

Service aggregates translator + helper wiring.

func NewInMemoryService

func NewInMemoryService(cfg Config, translations map[string]map[string]string) (Service, error)

NewInMemoryService creates a service backed by the provided translations.

func NewNoOpService

func NewNoOpService() Service

NewNoOpService constructs a stub i18n service.

func NewService

func NewService(opts ServiceOptions) Service

NewService constructs a concrete Service from the provided options.

type ServiceOptions

type ServiceOptions struct {
	Translator      interfaces.Translator
	Culture         interfaces.CultureService
	Helpers         TemplateHelpersProvider
	DefaultLocale   string
	FallbackOnError MissingTranslationStrategy
}

ServiceOptions configures the concrete i18n service.

type TemplateHelpersFunc

TemplateHelpersFunc adapts a function into a TemplateHelpersProvider.

func (TemplateHelpersFunc) TemplateHelpers

TemplateHelpers satisfies TemplateHelpersProvider.

type TemplateHelpersProvider

type TemplateHelpersProvider interface {
	TemplateHelpers(translator interfaces.Translator, cfg interfaces.HelperConfig, fallback MissingTranslationStrategy) map[string]any
}

TemplateHelpersProvider builds helper maps for template renderers.

Jump to

Keyboard shortcuts

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