The lang package provides multilingual support and translation services for the webtyp/fmt library.
- 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
fmtpackage 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
fmtpackage's buffer pooling for efficient translation.
import "webtyp.com/lang"Simply importing the package activates translation for fmt.Err(), fmt.Println, and %L formatting in fmt.Sprintf().
English word order is flexible — it tolerates both "Invalid Format" and "Format Invalid". Spanish is not: adjectives follow the noun, and natural phrasing matters more.
Rule: before choosing your EN word sequence, verify it produces natural ES output. Write the words in the order that makes sense in Spanish; English will still be readable.
| Goal | Wrong EN order | Why it breaks in ES | Correct order |
|---|---|---|---|
| Format error | "invalid", "format" |
"Inválido Formato" ❌ | "format", "invalid" → "Formato Inválido" ✓ |
| Directory not initialized | "not", "initialized", "directory" |
"No Inicializado Directorio" ❌ | "directory", "not", "initialized" → "Directorio No Inicializado" ✓ |
| Project root missing | "missing", "project", "root" |
"Falta Proyecto Raíz" ❌ | "project", "root", "missing" → "Proyecto Raíz Falta" ✓ |
Quick checklist before writing a Translate(...) call:
- Write the sentence in Spanish first.
- Extract the nouns, then adjectives/state words — that is your word order.
- Use that same order for the EN keys passed to
Translate().
Unknown words (e.g. "Go", version numbers, paths) pass through unchanged in both languages.
webtyp provides translations for two distinct layers:
- The App: What the user builds. The dictionary is kept in
config/lang.jsonbylangc sync. In the browser,sitecinlines the merged dictionary inindex.html, andlangreads it automatically. - The Framework's Tools: webtyp's own tools (e.g. devtui, server) embed their own
lang.jsonusinggo:embed. At startup, they calllang.Loadto register their dictionaries. You can update a tool's dictionary by runninglangc sync -tool. Uselang.Current()to read the active language without changing it.
OutLang(l ...any) string: Sets or gets the current global language. Acceptslangconstants (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.Current() string: Returns the current language code without changing it.Load(dicts ...[]byte) error: Installs translation dictionaries for backend tools.
Translate(values ...any) *fmt.Conv: Translates multiple terms using the dictionary and joins them with spaces. Each string argument is one key, exactly as written.
EN, ES, ZH, HI, AR, PT, FR, DE, RU: Language identifiers.
Translations are stored in config/lang.json and are generated by langc sync. Append new languages at the end of languages; never reorder or remove one without moving the values.
{
"default": "es",
"languages": ["es", "fr"],
"keys": {
"Delete": ["Eliminar", "Supprimer"],
"Pick a conversation": ["Elige una conversación", "Choisissez une conversation"],
"Send": ["", ""]
}
}Each argument of Translate is one key, exactly as written.
A library ships its own lang.json at its module root (same shape, no default); sitec merges every
library's file with the project's, and the project wins. A component field holding fixed UI text is
typed lang.Text (callers still write a plain string) and translated in Render; data stays
string and is never translated.