test: lang parsing
All checks were successful
continuous-integration/drone/pr Build is passing
continuous-integration/drone/push Build is passing

See #652
This commit is contained in:
2025-09-06 08:38:38 +02:00
parent 02add8c3ef
commit dc207a0138
2 changed files with 26 additions and 5 deletions

View File

@ -18,9 +18,11 @@ var assetFS embed.FS
var (
DefaultLocale = "en"
Locale = DefaultLocale
_, Mo = LoadLocale()
G = Mo.Get
)
func LoadLocale() *gotext.Mo {
func LoadLocale() (string, *gotext.Mo) {
entries, err := assetFS.ReadDir("locales")
if err != nil {
log.Fatalf("i18n: unable to read embedded locales directory: %s", err)
@ -49,7 +51,7 @@ func LoadLocale() *gotext.Mo {
}
if Locale == DefaultLocale {
return gotext.NewMo()
return Locale, gotext.NewMo()
}
b, err := assetFS.ReadFile(fmt.Sprintf("locales/%s.mo", Locale))
@ -60,7 +62,5 @@ func LoadLocale() *gotext.Mo {
mo := gotext.NewMo()
mo.Parse(b)
return mo
return Locale, mo
}
var G = LoadLocale().Get

21
pkg/i18n/i18n_test.go Normal file
View File

@ -0,0 +1,21 @@
package i18n_test
import (
"os"
"testing"
"coopcloud.tech/abra/pkg/i18n"
)
func TestLoadLocale(t *testing.T) {
originalLang := os.Getenv("LANG")
os.Setenv("LANG", "es_ES.UTF-8")
t.Cleanup(func() {
os.Setenv("LANG", originalLang)
})
locale, _ := i18n.LoadLocale()
if locale != "es" {
t.Fatalf("expected 'es', locale was '%s'", locale)
}
}