i18n: replace gettext implementation (#1056)

This commit is contained in:
UUBulb
2025-04-13 12:26:03 +08:00
committed by GitHub
parent 663688ea94
commit 91cb5e903f
5 changed files with 108 additions and 63 deletions

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

@@ -0,0 +1,32 @@
package i18n
import (
"testing"
)
func TestI18n(t *testing.T) {
const testStr = "database error"
t.Run("SwitchLocale", func(t *testing.T) {
loc := NewLocalizer("zh_CN", "nezha", "translations", Translations)
translated := loc.T(testStr)
if translated != "数据库错误" {
t.Fatalf("expected %s, but got %s", "数据库错误", translated)
}
loc.AppendIntl("zh_TW")
loc.SetLanguage("zh_TW")
translated = loc.T(testStr)
if translated != "資料庫錯誤" {
t.Fatalf("expected %s, but got %s", "資料庫錯誤", translated)
}
})
t.Run("Fallback", func(t *testing.T) {
loc := NewLocalizer("invalid", "nezha", "translations", Translations)
fallbackStr := loc.T(testStr)
if fallbackStr != testStr {
t.Fatalf("expected %s, but got %s", testStr, fallbackStr)
}
})
}