package main

import (
	"os"
	"path/filepath"

	"github.com/leonelquinteros/gotext"
)

var translations *gotext.Locale

// InitializeLanguage loads the language strings from .po files based on the selected language.
func InitializeLanguage(siteLanguage string) {
	translationsDir := "lang"
	localeDir := filepath.Join(translationsDir, siteLanguage, "LC_MESSAGES")
	if _, err := os.Stat(localeDir); os.IsNotExist(err) {
		printWarn("Translation directory for language '%s' not found. Defaulting to English.", siteLanguage)
		siteLanguage = "en" // Use default language if not found
		localeDir = filepath.Join(translationsDir, siteLanguage, "LC_MESSAGES")
	}

	translations = gotext.NewLocale(translationsDir, siteLanguage)
	translations.AddDomain("default")
}

// Translate wraps gotext.Get and returns a translated string based on the given key.
func Translate(key string, params ...interface{}) string {
	if translations == nil {
		printWarn("Translation failed for key '%s'. This may occur if Translate is called before initializing language settings or if the key is missing.", key)
		return key // Fallback if translations are not initialized
	}
	return translations.Get(key, params...)
}