i18n

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Aug 2, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package i18n provides UI string translation for okpos.

Only English ships today, but every user-facing string is already routed through T with a stable message ID, so adding a language later is a data-only change: drop a new locales/<tag>.json file and register it in init.

Downstream modules (e.g. a commercial cloud build) extend this layer by calling RegisterMessagesFS with their own message files, loading them into the same shared bundle T reads from — no fork of this package is needed. To avoid ID collisions with okpos's own messages (and with each other), downstream message IDs must be prefixed with a namespace of their own, e.g. "cloud.billing.upgrade_cta".

Index

Constants

View Source
const NameKey = "Name"

NameKey is the conventional template-data key for a "{{.Name}}"-style placeholder (e.g. "Edit {{.Name}}"). Shared across callers so the literal "Name" string isn't repeated ad hoc at every call site.

Variables

This section is empty.

Functions

func RegisterMessagesFS

func RegisterMessagesFS(fsys fs.FS, paths ...string) error

RegisterMessagesFS loads additional message files into the shared bundle that T reads from. Callers must do this once at startup, before any request is served — the bundle is not safe to mutate concurrently with lookups. It's additive only: okpos's own messages are never replaced.

func T

func T(ctx context.Context, messageID string, tmplData ...map[string]any) string

T resolves messageID to a string in the locale carried by ctx (as detected by locale.FromContext, already populated on every request by the existing locale-detection middleware — no separate i18n middleware is needed). It falls back to English when the detected locale has no translation for messageID, and to messageID itself if the ID was never registered in any loaded message file at all, so a missing translation is visibly wrong rather than silently broken.

tmplData, if provided, supplies template variables for messages containing "{{.Name}}"-style placeholders; only the first argument is used.

Types

This section is empty.

Jump to

Keyboard shortcuts

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