33 lines
1 KiB
Go
33 lines
1 KiB
Go
|
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' does not exist. Using default.", 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 {
|
||
|
return key // Fallback if translations are not initialized
|
||
|
}
|
||
|
return translations.Get(key, params...)
|
||
|
}
|