locale

package module
v0.0.0-...-affabfa Latest Latest
Warning

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

Go to latest
Published: Jul 9, 2026 License: BSD-3-Clause Imports: 4 Imported by: 0

Documentation

Overview

Package locale is a pure-Go (no cgo, standard-library only) port of the locale-handling behaviour of the Ruby fast_gettext gem, i.e. the parts of FastGettext::Storage that accept, normalize and negotiate locales.

It provides:

  • Format and Normalize for turning a single BCP-47 / POSIX locale tag into fast_gettext's canonical "lang_REGION" form. Format reproduces the gem's exact behaviour (rewrite only a bare "ll[-_]RR" tag); Normalize is a fuller cleanup that also strips a ".charset" encoding suffix and an "@modifier".
  • ParseAcceptLanguage which turns an HTTP Accept-Language header into a quality-ranked list of normalized locale tags, mirroring fast_gettext's formatted_sorted_locales.
  • Negotiate which picks the best available locale for a given Accept-Language header, mirroring fast_gettext's best_locale_in: an exact match wins, otherwise the language part (before the region) is matched, and a nil "available" list means "anything is acceptable".

The package has no dependency on any Ruby runtime; the surface is Go-typed so a Ruby binding layer (or the companion fast-gettext translation library) can build on it.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Format

func Format(tag string) string

Format normalizes a single locale tag exactly the way fast_gettext's format_locale does: a bare "ll[-_]RR" tag becomes "ll_RR" (language lower, region upper); anything else (a bare language, a weight fragment, an already odd string) is returned unchanged.

func Negotiate

func Negotiate(header string, available []string) string

Negotiate returns the best available locale for the given Accept-Language header, mirroring fast_gettext's best_locale_in.

Candidates are considered in descending quality order. For each candidate an exact match against available wins; otherwise the language part (the text before the first "_") is matched. A nil available slice means "any locale is acceptable", so the top-ranked candidate is returned as-is. An empty (but non-nil) slice matches nothing. When nothing matches, "" is returned.

func Normalize

func Normalize(tag string) string

Normalize is a fuller BCP-47 / POSIX normalization. It trims surrounding whitespace, drops an optional ".charset" encoding suffix (e.g. ".UTF-8") and an "@modifier" suffix (e.g. "@euro"), converts a "-" separator to "_", and then applies Format. An empty or separator-only input yields "".

Types

type Ranked

type Ranked struct {
	Tag     string
	Quality float64
}

Ranked is one entry parsed from an Accept-Language header: a normalized locale tag and its quality weight in the range (0, 1].

func ParseAcceptLanguage

func ParseAcceptLanguage(header string) []Ranked

ParseAcceptLanguage parses an HTTP Accept-Language header into normalized locale tags ranked by descending quality, mirroring fast_gettext's formatted_sorted_locales. Whitespace is ignored, an absent ";q=" defaults to quality 1.0, and a malformed quality is treated as 1.0. Entries with quality 0 (RFC 7231 "not acceptable") and empty tags are dropped. Ties keep their original left-to-right order (a stable sort), so the result is deterministic.

Jump to

Keyboard shortcuts

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