WIP: feat: translation support
Some checks failed
continuous-integration/drone/push Build is failing

See #483
This commit is contained in:
2025-08-19 11:22:52 +02:00
parent 5cf6048ecb
commit a31ec51c24
71 changed files with 804 additions and 796 deletions

View File

@ -1,30 +1,31 @@
package lang
import (
"embed"
"fmt"
"os"
"strings"
"github.com/leonelquinteros/gotext"
)
//go:embed locales
var assetFS embed.FS
func GetLocale() string {
if loc := os.Getenv("LC_MESSAGES"); loc != "" {
return NormalizeLocale(loc)
}
if loc := os.Getenv("LANG"); loc != "" {
return NormalizeLocale(loc)
}
return "C.UTF-8"
return os.Getenv("LANG")
}
func NormalizeLocale(loc string) string {
if idx := strings.Index(loc, "."); idx != -1 {
return loc[:idx]
func LoadLocale(locale string) error {
localePath := fmt.Sprintf("locales/%s/%s.po", locale)
b, err := assetFS.ReadFile(localePath)
if err != nil {
return fmt.Errorf("unable to load %s: %s", localePath, err)
}
if idx := strings.Index(loc, "@"); idx != -1 {
return loc[:idx]
}
po := gotext.NewPo()
po.Parse(b)
gotext.GetStorage().AddTranslator("abra", po)
return loc
return nil
}