Go Translation AI - An AI-powered HTML translation engine for Go. Features intelligent SHA-256 caching, context-aware translations, RTL support, and efficient batch processing. The Go counterpart to tstlai.
v0.1.0 Released: Initial release with full HTML translation, in-memory and Redis caching, CLI tool, Go source translation, and comprehensive language support.
Uses OpenAI (GPT-4o-mini by default) for high-quality, context-aware translations.
SHA-256 based caching with TTL support. In-memory or Redis backends.
Automatic dir="rtl" for Arabic, Hebrew, Persian, Urdu, and more.
Efficient API usage by batching translations into single requests.
Command-line interface for translating files with diff mode and dry run support.
Translate Go source code strings and comments with GoProcessor.
Exponential backoff for transient failures with configurable retries.
Tiered language support with Tier 1 (high quality) and Tier 2 (good quality) languages.
go get github.com/ZaguanLabs/gotlaipackage main
import (
"context"
"fmt"
"log"
"os"
"github.com/ZaguanLabs/gotlai"
"github.com/ZaguanLabs/gotlai/cache"
"github.com/ZaguanLabs/gotlai/processor"
"github.com/ZaguanLabs/gotlai/provider"
)
func main() {
// Create OpenAI provider
p := provider.NewOpenAIProvider(provider.OpenAIConfig{
APIKey: os.Getenv("OPENAI_API_KEY"),
Model: "gpt-4o-mini",
})
// Create translator with cache and HTML processor
t := gotlai.NewTranslator("es_ES", p,
gotlai.WithCache(cache.NewInMemoryCache(3600)),
gotlai.WithProcessor(processor.NewHTMLProcessor()),
gotlai.WithContext("E-commerce website"),
gotlai.WithExcludedTerms([]string{"API", "SDK"}),
)
// Translate HTML
html := `<html><body><h1>Welcome</h1><p>Hello World</p></body></html>`
result, err := t.ProcessHTML(context.Background(), html)
if err != nil {
log.Fatal(err)
}
fmt.Println(result.Content)
fmt.Printf("Translated: %d, Cached: %d\n", result.TranslatedCount, result.CachedCount)
}go install github.com/ZaguanLabs/gotlai/cmd/gotlai@latest# Translate a file
gotlai --lang es_ES input.html > output.html
# Translate with context
gotlai --lang ja_JP --context "E-commerce website" input.html -o output.html
# Exclude terms from translation
gotlai --lang de_DE --exclude "API,SDK,HTML" input.html
# Read from stdin
cat input.html | gotlai --lang fr_FR > output.html
# Diff mode - see what changed between versions
gotlai --lang es_ES --diff old.html new.html
# Dry run - preview without API calls
gotlai --lang es_ES --dry-run input.html
# JSON output for programmatic use
gotlai --lang es_ES --json input.html--langTarget language (required)--sourceSource language (default: en)--output, -oOutput file path--contextTranslation context hint--excludeComma-separated terms to skip--modelOpenAI model (default: gpt-4o-mini)--diffCompare with previous version--dry-runPreview without API calls--jsonOutput as JSONc := cache.NewInMemoryCache(3600) // TTL in secondsc, err := cache.NewRedisCache(cache.RedisConfig{
URL: "redis://localhost:6379",
TTL: 3600,
KeyPrefix: "gotlai:",
})// Export translations for offline use
exporter := cache.NewExporter(myCache)
exporter.ExportToFile("translations.json", map[string]string{"lang": "es_ES"})
// Import translations
importer := cache.NewImporter(newCache)
importer.ImportFromFile("translations.json")Translate Go source code strings and comments:
proc := processor.NewGoProcessor()
// Or customize:
proc := processor.NewGoProcessor(
processor.WithComments(true),
processor.WithStrings(true),
)provider := gotlai.NewRateLimitedProvider(openaiProvider, gotlai.RateLimitConfig{
RequestsPerMinute: 60,
BurstSize: 10,
})translations, misses := gotlai.ParallelCacheLookup(cache, nodes, "es_ES")en_US, en_GB, de_DE, es_ES, es_MX, fr_FR, it_IT, ja_JP, pt_BR, pt_PT, zh_CN, zh_TW
ar_SA, ko_KR, ru_RU, nl_NL, pl_PL, tr_TR, vi_VN, and more...
Arabic (ar), Hebrew (he), Persian (fa), Urdu (ur), Pashto (ps), Sindhi (sd), Uyghur (ug)
gotlai.NewTranslator("es_ES", provider,
gotlai.WithSourceLang("en"), // Source language (default: "en")
gotlai.WithCache(cache), // Translation cache
gotlai.WithProcessor(processor), // Content processor
gotlai.WithExcludedTerms([]string{"API"}), // Terms to never translate
gotlai.WithContext("Technical docs"), // Global context for AI
)