locales

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package locales checks the content languages a site offers, such as "en" and "nl-BE", and writes their codes the same way everywhere. Which languages are enabled comes from a Provider, usually the Site settings; how a language shows in addresses, and the admin's own language, are decided elsewhere.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalid is returned for a language code that is not a valid BCP 47
	// tag, such as "en_US" or an empty code.
	ErrInvalid = errors.New("invalid_locale")
	// ErrDisabled is returned for a valid language code that the site does
	// not offer.
	ErrDisabled = errors.New("locale_disabled")
	// ErrConfiguration is returned for a list of languages that cannot work:
	// none, more than 100, an invalid or repeated code, or a default that is
	// not one of them.
	ErrConfiguration = errors.New("invalid_locale_configuration")
)

Functions

func Normalize

func Normalize(code string) (string, error)

Normalize returns the canonical form of a BCP 47 language code, such as "en-GB" for "en-gb", so the same language is always stored the same way. It returns ErrInvalid for a code that is not a language tag; codes with an underscore, such as "en_US", are refused rather than guessed.

Types

type Config

type Config struct {
	Default string
	Enabled []string
}

Config is the list of content languages: Default is the language a visitor gets when the address names none, and Enabled are all languages in the order editors and visitors see them, the default among them.

func Validate

func Validate(config Config) (Config, error)

Validate checks a list of languages and returns it normalized, in the order given, as editors chose it. The input is copied, never changed. It returns ErrConfiguration when the list cannot work; see that error.

func (Config) Require

func (c Config) Require(code string) (string, error)

Require returns the normalized form of code when it is one of the enabled languages. It returns ErrInvalid for a code that is no language and ErrDisabled for a language the site does not offer.

type Provider

type Provider interface {
	Configuration(context.Context) (Config, error)
}

Provider gives the content languages, from the application's configuration or from the Site settings. Services ask for them in every operation, so a change in the settings takes effect at once.

type Service

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

Service is a Provider with a fixed list of languages, checked once by New. Tests and tools use it where a site would pass its Site settings.

func New

func New(config Config) (*Service, error)

New returns a Service for config after checking it with Validate, so a mistake in the list fails at once with ErrConfiguration.

func (*Service) Configuration

func (s *Service) Configuration(ctx context.Context) (Config, error)

Configuration implements Provider. It returns a copy, so callers may change the list without changing the Service.

Jump to

Keyboard shortcuts

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