i18n

package
v0.11.2 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package i18n is a platform module: translations of API responses and error messages, ported from taply. Requires postgres and api.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Configure added in v0.11.0

func Configure(modules []platform.Module, opts ...Option) error

Configure applies options to the i18n module among the modules, for a project whose wiring is generated: call it in main, between platformModules and platform.Run.

func From

func From(app *platform.App) *i18nx.Client

From returns the translations client from the container.

func SkipTranslation

func SkipTranslation(app *platform.App, fn func(ctx context.Context) bool)

SkipTranslation turns response substitution off for the calls fn picks — taply does it for tablets of a restaurant with translations disabled. fn runs after the project's authentication, so it sees the caller the authentication put into the context. Error messages are still translated.

Types

type Config

type Config struct {
	DefaultLocale i18nx.Locale   // language of the base columns and the fallback
	Locales       []i18nx.Locale // languages the API answers in
}

Config holds module settings. Load fills it from the environment; the platform generator writes the Load call into the project's config.gen.go.

func Load

func Load(l *confx.Loader) Config

Load reads the module settings from environment variables.

type Module

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

Module implements platform.Module.

func New

func New(cfg Config, opts ...Option) *Module

New creates the module.

func (*Module) Init

func (m *Module) Init(ctx context.Context, app *platform.App) error

Init creates the translations table, loads the static translations and puts the translation of requests and responses into the API.

func (*Module) Name

func (m *Module) Name() string

type Option

type Option func(*Module)

Option configures the module.

func WithErrorPhase added in v0.11.0

func WithErrorPhase(phase api.Phase) Option

WithErrorPhase moves the translation of error messages to another phase of the API chain. By default it is api.PhaseErrorsI18n, outside authentication, so the refusals of authentication, guards and validation are translated too. api.PhaseAfterValidate translates only what the handler and the phases after validation return, as taply did: a 400, 401 or 403 of the chain keeps its text whatever Accept-Language says.

func WithExternalErrors added in v0.11.1

func WithExternalErrors() Option

WithExternalErrors translates the failures of external providers too, as taply does: api.ExternalTimeout("zebra", "external service timeout") gets the translation of the key "error.msg.zebra: external service timeout" of i18n/messages.yaml in the language of the request; without an entry the message keeps its text, and the ErrorInfo stays. Off by default, the text names the provider and is left alone. An Unavailable, as api.ExternalUnavailable builds it, is never translated, like any Unavailable.

func WithMessages

func WithMessages(yaml string) Option

WithMessages gives the module the static translations of the project. The generated wiring passes the embedded i18n/messages.yaml.

Jump to

Keyboard shortcuts

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