Help us shape the future of Go! Take the 2026 Go Developer Survey

locale

package
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Sep 18, 2026 License: Apache-2.0 Imports: 0 Imported by: 0

README

Message selection and formatting

Import github.com/septagon-oss/platformkit/kit/locale to select a language and format plain-text messages in a worker, command or web application. The package has no dependencies outside the standard library and starts no services.

Implement Messages.Select and Formatter.Text, or explicitly select the x/text provider. SelectLocale checks that selection returns a content language and formatter; missing or incomplete configuration panics. Pass preferences in priority order. The provider owns supported languages, fallback, interpolation and plural rules; message keys remain stable identifiers. The executable example supplies a custom worker formatter.

Compose messages before use. Providers must support concurrent selections without process-global state; do not mutate their catalogs while rendering. A formatter returns text, not trusted HTML. Escape it through the consuming renderer, and keep recipient lookup, delivery, request headers and persistence with their owners.

Existing ui/page names remain aliases or forwarding functions. A standalone consumer imports this package directly; importing ui/page still includes that package's web integration. Misconfiguration still panics, with text now naming locale or xtext instead of page. Translation editing, publishing and deployment remain separate operations; this contract performs none of them.

From the foundation repository, run the service-free checks:

go test -race ./kit/locale/...
go list -deps -f '{{if not .Standard}}{{.ImportPath}}{{end}}' ./kit/locale

The dependency listing should contain only kit/locale. The provider's own guide states its additional dependencies and verification.

Documentation

Overview

Package locale supplies language selection and message formatting contracts for web pages, workers and other Go applications. Providers own interpolation, plural rules and language negotiation; this package starts no services.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Formatter

type Formatter interface {
	Text(key, fallback string, args ...any) string
}

Formatter translates a namespaced key, using readable source text when it is missing. Text is plain text: HTML renderers must escape it. The provider owns interpolation and plural rules.

type Locale

type Locale struct {
	Language string
	Formatter
}

Locale contains one render's supported content language and formatter.

func SelectLocale

func SelectLocale(messages Messages, preferences ...string) Locale

SelectLocale negotiates one render's language. Providers return a supported language and a formatter, including when no preference is supported. Missing or incomplete providers panic because they are composition errors.

Example
package main

import (
	"fmt"

	"github.com/septagon-oss/platformkit/kit/locale"
)

// A worker can supply readable source messages without an SDK or web renderer.
type sourceMessages struct{}

func (sourceMessages) Select(...string) locale.Locale {
	return locale.Locale{Language: "en", Formatter: sourceMessages{}}
}

func (sourceMessages) Text(_ string, fallback string, args ...any) string {
	return fmt.Sprintf(fallback, args...)
}

func main() {
	messages := locale.SelectLocale(sourceMessages{}, "en")
	fmt.Println(messages.Language)
	fmt.Println(messages.Text("task.digest", "%d tasks need your review", 2))
}
Output:
en
2 tasks need your review

type Messages

type Messages interface {
	Select(preferences ...string) Locale
}

Messages selects a render-local formatter from a composition's immutable messages. Preferences are ordered: explicit choice, browser, then default. Implementations must support concurrent selection without global state.

Directories

Path Synopsis
providers
xtext
Package xtext adapts an explicitly supplied x/text message catalog to locale.
Package xtext adapts an explicitly supplied x/text message catalog to locale.

Jump to

Keyboard shortcuts

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