This repository has been archived on 2025-08-04. You can view files and clone it, but cannot push or open issues or pull requests.
Files
translations-test/main.go
2025-08-04 14:33:31 +01:00

50 lines
937 B
Go

package main
import (
"fmt"
"os"
"strings"
"github.com/leonelquinteros/gotext"
)
func main() {
// Set locale from environment
locale := getLocale()
// Configure gettext
gotext.Configure("locales", locale, "default")
// Translate and print
fmt.Println(gotext.Get("Hello World"))
fmt.Println(gotext.Get("Goodbye World"))
}
// Determine locale from environment variables
func getLocale() string {
// Check LC_MESSAGES first
if loc := os.Getenv("LC_MESSAGES"); loc != "" {
return normalizeLocale(loc)
}
// Fallback to LANG if LC_MESSAGES is empty
if loc := os.Getenv("LANG"); loc != "" {
return normalizeLocale(loc)
}
// Default to English
return "en_US"
}
// Normalize locale format (es_ES.UTF-8 → es_ES)
func normalizeLocale(loc string) string {
if idx := strings.Index(loc, "."); idx != -1 {
return loc[:idx]
}
if idx := strings.Index(loc, "@"); idx != -1 {
return loc[:idx]
}
return loc
}