feat: translation support
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
See #483
This commit is contained in:
62
pkg/i18n/i18n.go
Normal file
62
pkg/i18n/i18n.go
Normal file
@ -0,0 +1,62 @@
|
||||
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 (
|
||||
DefaultLocale = "en"
|
||||
Locale = DefaultLocale
|
||||
)
|
||||
|
||||
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, " "))
|
||||
}
|
||||
}
|
||||
|
||||
if Locale == DefaultLocale {
|
||||
return gotext.NewMo()
|
||||
}
|
||||
|
||||
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
|
4677
pkg/i18n/locales/abra.pot
Normal file
4677
pkg/i18n/locales/abra.pot
Normal file
File diff suppressed because it is too large
Load Diff
BIN
pkg/i18n/locales/es.mo
Normal file
BIN
pkg/i18n/locales/es.mo
Normal file
Binary file not shown.
4903
pkg/i18n/locales/es.po
Normal file
4903
pkg/i18n/locales/es.po
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user