lang support
This commit is contained in:
parent
5822b75d66
commit
5dd3114e2d
8 changed files with 150 additions and 23 deletions
32
lang.go
Normal file
32
lang.go
Normal file
|
@ -0,0 +1,32 @@
|
|||
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...)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue