Search/lang.go

34 lines
1.2 KiB
Go
Raw Normal View History

2024-10-07 17:12:22 +02:00
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) {
2024-10-25 13:54:29 +02:00
printWarn("Translation directory for language '%s' not found. Defaulting to English.", siteLanguage)
2024-10-07 17:12:22 +02:00
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 {
2024-10-25 13:54:29 +02:00
printWarn("Translation failed for key '%s'. This may occur if Translate is called before initializing language settings or if the key is missing.", key)
2024-10-07 17:12:22 +02:00
return key // Fallback if translations are not initialized
}
return translations.Get(key, params...)
}