i18n

package
v1.6.1 Latest Latest
Warning

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

Go to latest
Published: Aug 8, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package i18n resolves user-facing message codes into localized text.

Services raise errors with a stable code rather than a sentence:

httpserver.ConflictError(err, messages.PhoneNumberInUse)

The HTTP error handler looks the code up in the catalog registered on the server and renders it in the locale the caller asked for, so the response carries both the code a client can branch on and text a person can read.

Index

Constants

View Source
const (
	CodeBadRequest         = "BAD_REQUEST"
	CodeValidation         = "VALIDATION_ERROR"
	CodeUnauthorized       = "UNAUTHORIZED"
	CodeForbidden          = "FORBIDDEN"
	CodeNotFound           = "NOT_FOUND"
	CodeConflict           = "CONFLICT"
	CodeInternal           = "INTERNAL_ERROR"
	CodeServiceUnavailable = "SERVICE_UNAVAILABLE"
)

Generic message codes, one per status a handler can return without naming a specific rule. They are the fallback the error handler reports when a message is literal prose rather than a catalog code, so every error carries a code a client can branch on even before a service is fully migrated.

View Source
const CodeValidationInvalid = validationCodePrefix + "INVALID"

CodeValidationInvalid is the template used for a validate tag with no translation of its own, so an unknown rule still reads as a sentence.

Variables

This section is empty.

Functions

func CodeForStatus

func CodeForStatus(status int) string

CodeForStatus is the generic code reported for an HTTP status.

func FieldCode added in v1.6.0

func FieldCode(field string) string

FieldCode returns the catalog code for a request field, so `phoneNumber` resolves to FIELD_PHONE_NUMBER. The name is whichever one the validator reports: the json tag when a field has one, and the Go field name otherwise, which is what query and path parameters fall back to.

Acronyms stay whole, so `vendorID` and `VendorID` both resolve to FIELD_VENDOR_ID rather than splitting every capital.

func ValidationCode added in v1.6.0

func ValidationCode(tag string) string

ValidationCode returns the catalog code for a validate tag, so `required` resolves to VALIDATION_REQUIRED.

func ValidationMessage added in v1.6.0

func ValidationMessage(catalog Catalog, locale Locale, field, tag, param string) string

ValidationMessage renders one field failure in locale. The template comes from the tag and the field name from the catalog, so a service localizes its request fields by registering labels rather than by touching a handler.

A field with no registered label falls back to the name the client sent, which keeps the message useful while a service's labels are still landing.

Types

type Catalog

type Catalog map[string]map[Locale]string

Catalog maps a message code to its text in each locale. A code missing an entry for the requested locale falls back to Default.

func BuiltinCatalog

func BuiltinCatalog() Catalog

BuiltinCatalog returns text for the generic codes plus a template for every validate tag. Services merge their own domain catalog over it; a service that wants a plain localized "not found" can raise CodeNotFound directly instead of inventing a code.

func (Catalog) Lookup

func (c Catalog) Lookup(code string, locale Locale) (string, bool)

Lookup returns the text registered for code in locale. The bool reports whether code is registered at all, which is what distinguishes a message code from a literal sentence.

func (Catalog) Merge

func (c Catalog) Merge(other Catalog) Catalog

Merge returns a catalog holding every entry of c overlaid with other. Neither receiver nor argument is modified, so a service can extend the builtin catalog without mutating it.

type Locale

type Locale string

Locale is a language tag the catalog carries text for.

const (
	English    Locale = "en"
	Vietnamese Locale = "vi"

	// Default is the locale used when Accept-Language names nothing supported,
	// and the fallback when a catalog entry is missing a translation.
	Default = English
)

func Negotiate

func Negotiate(header string) Locale

Negotiate picks the best supported locale from an Accept-Language header, falling back to Default for an empty, malformed, or unsupported header.

Jump to

Keyboard shortcuts

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