i18nx

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 30, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package i18nx is the opt-in localization loader for the F5 toolkit's embed and extract packages.

The embed, extract, and detect packages never compile the locale tables themselves: they accept translation through small TranslatorProvider interfaces and default to English fallback strings. This package embeds the merged locale tables (both the f5messageembed and f5messageextract namespaces) and builds a translator that those packages accept via their SetTranslator(...) methods.

Because nothing in the library (embed/extract/detect) imports i18nx, a minimal build never links the locale tables. Only end-user applications and examples that want localized error messages import this package.

Wiring

t, _ := i18nx.NewTranslator("es-ES")
embed.SetTranslator(t)
extract.SetTranslator(t)

The translator returned by NewTranslator structurally satisfies the TranslatorProvider interface defined by both the embed and extract packages (Translate, TranslateWithArgs, HasKey, SetLocale, GetLocale), so the same instance can be shared between them.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DetectLocale

func DetectLocale() string

DetectLocale automatically detects the system locale from environment variables.

It checks the following environment variables in order:

  • LC_ALL
  • LC_MESSAGES
  • LANG

The detected locale is normalized to the format used by this package (e.g., "en-US"). Common system formats like "en_US.UTF-8" are converted to "en-US".

If the detected locale is not in the list returned by GetSupportedLocales(), or if no locale can be detected, it falls back to "en-US".

Returns:

  • string: The detected locale code (e.g., "en-US") or "en-US" as fallback.

func GetSupportedLocales

func GetSupportedLocales() []string

GetSupportedLocales returns the list of locales supported by this package.

It reads the embedded locale files and returns their locale codes, sorted alphabetically for consistency.

If the embedded filesystem cannot be read (which should never happen in normal operation), this function returns a fallback list containing only "en-US" to ensure graceful degradation.

Returns:

  • []string: Sorted slice of supported locale codes.

func NewTranslator

func NewTranslator(locale ...string) (*i18n.Translator, error)

NewTranslator creates an i18n translator with the embedded locale tables.

The returned *i18n.Translator structurally satisfies the TranslatorProvider interfaces of the embed and extract packages, so it can be passed directly to their SetTranslator methods:

t, _ := i18nx.NewTranslator("es-ES")
embed.SetTranslator(t)
extract.SetTranslator(t)

The created translator:

  • Loads from the embedded locale files (no external files needed).
  • Uses "en-US" as the default/fallback locale.
  • Sets the requested locale as the current locale.
  • Supports every locale returned by GetSupportedLocales().

If the requested locale is not supported, an error is returned. Use GetSupportedLocales() to see the list of available locales.

Auto-detection:

Call without arguments or with an empty string to auto-detect the locale from system environment variables (LC_ALL, LC_MESSAGES, LANG). If detection fails or the detected locale is not supported, it falls back to "en-US".

Parameters:

  • locale: Optional locale code (e.g., "en-US"). Omit or pass an empty string for auto-detection.

Returns:

  • *i18n.Translator: Configured translator instance.
  • error: Error if the locale is not supported or initialization fails.

Types

This section is empty.

Jump to

Keyboard shortcuts

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