Skip to content
webtypPublic

About

Translation for the webtyp ecosystem: dictionary, language detection, Translate and Html

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

fmt/lang

The lang package provides multilingual support and translation services for the webtyp/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 "webtyp.com/lang"

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

Writing messages: think in Spanish first

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:

  1. Write the sentence in Spanish first.
  2. Extract the nouns, then adjectives/state words — that is your word order.
  3. 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.

Two layers

webtyp provides translations for two distinct layers:

  1. The App: What the user builds. The dictionary is kept in config/lang.json by langc sync. In the browser, sitec inlines the merged dictionary in index.html, and lang reads it automatically.
  2. The Framework's Tools: webtyp's own tools (e.g. devtui, server) embed their own lang.json using go:embed. At startup, they call lang.Load to register their dictionaries. You can update a tool's dictionary by running langc sync -tool. Use lang.Current() to read the active language without changing it.

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.
  • Current() string: Returns the current language code without changing it.
  • Load(dicts ...[]byte) error: Installs translation dictionaries for backend tools.

Translation

  • 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.

Constants

  • EN, ES, ZH, HI, AR, PT, FR, DE, RU: Language identifiers.

Translations file

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.

About

Translation for the webtyp ecosystem: dictionary, language detection, Translate and Html

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages