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

i18n

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2026 License: AGPL-3.0 Imports: 10 Imported by: 0

Documentation

Overview

Package i18n loads translation catalogs and resolves the request locale.

Catalog contract: every catalog FS (the embedded core one and any returned by a module's kernel.CatalogProvider) has files at "catalog/<module>.<locale>.json", each a flat {"key": "text"} object. Keys are stored with the module prefix: "app.name" in core.en.json is looked up as "core.app.name". Duplicate full keys across catalogs are an error, and so is a key that already starts with "<module>." (it would be stored as "core.core.x"). T falls back locale → en → the key itself.

Index

Constants

This section is empty.

Variables

View Source
var DefaultLocale = "ar"

DefaultLocale is used when neither the user preference nor Accept-Language names a supported locale. GoRun is Arabic/RTL-first.

View Source
var NativeNames = map[string]string{"ar": "العربية", "en": "English"}

NativeNames are the supported locales in their own language (language switchers).

View Source
var Supported = []string{"ar", "en"}

Supported lists the locales LocaleFromRequest may return.

Functions

func Locale

func Locale(ctx context.Context) string

Locale returns the locale stored by WithLocale, or DefaultLocale.

func LocaleFromRequest

func LocaleFromRequest(r *http.Request, userPref string) string

LocaleFromRequest picks the locale: a supported userPref wins, then the highest-weighted supported Accept-Language entry, then DefaultLocale.

func LocalePrefix

func LocalePrefix(loc, site string) string

LocalePrefix is the public path prefix of loc on a site whose locale is site: "/<loc>" for another supported locale, "" for the site locale, "" (unprefixed) or an unsupported one.

func LocalizePath

func LocalizePath(loc, site, p string) string

LocalizePath puts p (a site path starting with "/") under the prefix of loc.

func NormalizeArabic

func NormalizeArabic(s string) string

NormalizeArabic folds Arabic spelling variants so that search matches what a reader considers the same word. It is applied to both the indexed text and the query, and is idempotent. Text outside the Arabic block is returned unchanged (Latin case folding is left to the search engine).

Rules:

  • alef with hamza or madda, and alef wasla (أ إ آ ٱ) → bare alef ا
  • alef maqsura ى → ي, and taa marbuta ة → ه (the two common spelling swaps)
  • waw with hamza ؤ → و, and yaa with hamza ئ → ي: writers often drop the seat's hamza, so the bare letter is what a query is most likely to contain. The standalone hamza ء is a letter of its own and is kept.
  • tatweel ـ, the harakat and other combining marks U+064B–U+065F (tanween, shadda, sukun, and the combining hamza above/below, so decomposed forms fold like the precomposed ones), the superscript alef U+0670 and the combining Quranic annotation marks U+06D6–U+06DC, U+06DF–U+06E4, U+06E7–U+06E8, U+06EA–U+06ED are removed. The visible Quranic symbols (۝ ۞ ۩) are kept.
  • Arabic-Indic ٠-٩ and Extended (Persian) ۰-۹ digits → ASCII 0-9.

func SplitLocalePath

func SplitLocalePath(p string) (loc, rest string)

SplitLocalePath splits a leading supported-locale segment off p: "/en/post/x" → "en", "/post/x"; "/en" → "en", "/". Any other path is returned whole with loc "".

func WithLocale

func WithLocale(ctx context.Context, locale string) context.Context

WithLocale returns ctx carrying locale.

Types

type Bundle

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

Bundle holds merged messages: locale → full key → text.

func Load

func Load(catalogs ...fs.FS) (*Bundle, error)

Load merges the embedded core catalog with the given catalogs.

func (*Bundle) Dir

func (b *Bundle) Dir(locale string) string

Dir returns "rtl" for right-to-left locales and "ltr" otherwise.

func (*Bundle) T

func (b *Bundle) T(locale, key string, args ...any) string

T translates key ("module.key") for locale, formatting args with fmt.Sprintf when given. Falls back to en, then to the key itself.

Jump to

Keyboard shortcuts

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