lang

package
v0.24.1 Latest Latest
Warning

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

Go to latest
Published: Jun 17, 2026 License: MIT Imports: 3 Imported by: 0

README

fmt/lang

The lang package provides multilingual support and translation services for the tinywasm/fmt library.

Features

  • 9 Supported Languages: English (EN), Spanish (ES), Chinese (ZH), Hindi (HI), Arabic (AR), Portuguese (PT), French (FR), German (DE), Russian (RU).
  • Opt-in Design: Importing this package automatically enables translation support in the root fmt package via a global hook.
  • Noun + Adjective Order: Optimized for Spanish and English natural phrasing.
  • Manual Language Detection: Detects language from environment variables (backend) or browser settings (Wasm).
  • Zero-Allocation: Leverages the root fmt package's buffer pooling for efficient translation.

Installation

import "github.com/tinywasm/fmt/lang"

Simply importing the package activates translation for fmt.Err(), fmt.Html(), and %L formatting in fmt.Sprintf().

API

Global Language Configuration
  • OutLang(l ...any) string: Sets or gets the current global language. Accepts lang constants (e.g., lang.ES) or string codes (e.g., "fr"). Returns the current language code.
  • OutLang(): Auto-detects system/browser language and sets it as the global default.
Translation
  • Translate(values ...any) *fmt.Conv: Translates multiple terms using the dictionary and joins them with spaces.
  • RegisterWords(entries []DictEntry): Adds or merges new words into the global dictionary.
Constants
  • EN, ES, ZH, HI, AR, PT, FR, DE, RU: Language identifiers.

Documentation

For a detailed guide on how to write translatable messages, see the Translation Guide.

Documentation

Index

Constants

View Source
const (
	// Group 1: Core Essential Languages (Maximum Global Reach)
	EN lang = iota // 0 - English (default)
	ES             // 1 - Spanish
	ZH             // 2 - Chinese
	HI             // 3 - Hindi
	AR             // 4 - Arabic

	// Group 2: Extended Reach Languages (Europe & Americas)
	PT // 5 - Portuguese
	FR // 6 - French
	DE // 7 - German
	RU // 8 - Russian
)

Variables

This section is empty.

Functions

func OutLang

func OutLang(l ...any) string

OutLang sets and returns the current output language as a string.

func Printf

func Printf(format string, args ...any)

Printf prints formatted output to stdout (like fmt.Printf)

func Println

func Println(args ...any)

Println prints arguments to stdout followed by newline (like fmt.Println)

func RegisterWords

func RegisterWords(entries []DictEntry)

RegisterWords adds entries to the lookup engine. Safe to call from init().

func SmartArgs

func SmartArgs(c *fmt.Conv, dest fmt.BuffDest, separator string, allowStringCode bool, detectFormat bool, values ...any) *fmt.Conv

SmartArgs handles language detection, format string detection, and argument processing

func Translate

func Translate(values ...any) *fmt.Conv

Translate creates a translated string with support for multilingual translations. EN words are lookup keys (case-insensitive). Pass-through occurs if missing from dictionary.

Types

type DictEntry

type DictEntry struct {
	EN string
	ES string
	FR string
	DE string
	ZH string
	HI string
	AR string
	PT string
	RU string
}

DictEntry describes one translatable word. EN serves as lookup key (case-insensitive) and English display value. Other language fields fall back to EN when empty.

Jump to

Keyboard shortcuts

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