package i18n import ( "encoding/json" "github.com/gofiber/fiber/v2" "log" "os" "path/filepath" ) var translations map[string]map[string]string const DefaultLang = "en" func LoadTranslations() { translations = make(map[string]map[string]string) dir := "locales" files, err := filepath.Glob(filepath.Join(dir, "*.json")) if err != nil { log.Fatalf("Error loading language files: %v", err) } for _, file := range files { lang := filepath.Base(file) lang = lang[:len(lang)-len(filepath.Ext(lang))] file, err := os.Open(file) if err != nil { log.Printf("Error opening translation file %s: %v", file, err) continue } defer file.Close() var messages map[string]string if err := json.NewDecoder(file).Decode(&messages); err != nil { log.Printf("Error decoding translation file %s: %v", file, err) continue } translations[lang] = messages } } func Translate(c *fiber.Ctx, key string) string { lang := c.Locals("lang").(string) if messages, ok := translations[lang]; ok { if message, ok := messages[key]; ok { return message } } if messages, ok := translations[DefaultLang]; ok { if message, ok := messages[key]; ok { return message } } return key } func GetTranslations() map[string]map[string]string { return translations }