Some checks failed
continuous-integration/drone/push Build is failing
See #483
56 lines
1.0 KiB
Go
56 lines
1.0 KiB
Go
package i18n
|
|
|
|
import (
|
|
"embed"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"slices"
|
|
"strings"
|
|
|
|
"coopcloud.tech/abra/pkg/log"
|
|
"github.com/leonelquinteros/gotext"
|
|
)
|
|
|
|
//go:embed locales/*.mo
|
|
var assetFS embed.FS
|
|
|
|
var Locale = "en"
|
|
|
|
func LoadLocale() *gotext.Mo {
|
|
entries, err := assetFS.ReadDir("locales")
|
|
if err != nil {
|
|
log.Fatalf("i18n: unable to read embedded locales directory: %s", err)
|
|
}
|
|
|
|
var linguas []string
|
|
for _, entry := range entries {
|
|
if filepath.Ext(entry.Name()) == ".mo" {
|
|
fname := entry.Name()
|
|
fnameWithoutExt := strings.TrimSuffix(fname, filepath.Ext(fname))
|
|
linguas = append(linguas, fnameWithoutExt)
|
|
}
|
|
}
|
|
|
|
locale := os.Getenv("LANG")
|
|
if locale != "" {
|
|
if slices.Contains(linguas, locale) {
|
|
Locale = locale
|
|
} else {
|
|
log.Debugf("unsupported language: %s (want: %s)", locale, strings.Join(linguas, " "))
|
|
}
|
|
}
|
|
|
|
b, err := assetFS.ReadFile(fmt.Sprintf("locales/%s.mo", Locale))
|
|
if err != nil {
|
|
log.Fatalf("i18n: %s", err)
|
|
}
|
|
|
|
mo := gotext.NewMo()
|
|
mo.Parse(b)
|
|
|
|
return mo
|
|
}
|
|
|
|
var G = LoadLocale().Get
|