WIP: feat: translation support
Some checks failed
continuous-integration/drone/push Build is failing
Some checks failed
continuous-integration/drone/push Build is failing
See #483
This commit is contained in:
@ -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
|
||||
}
|
||||
|
Reference in New Issue
Block a user