From 1a0cb1b7f93cfe514d2fa0736f3b91c1ce480d48 Mon Sep 17 00:00:00 2001 From: Virgil Date: Thu, 2 Apr 2026 05:24:32 +0000 Subject: [PATCH] feat(i18n): add package-level translate wrapper Co-Authored-By: Virgil --- i18n.go | 8 ++++++++ i18n_test.go | 13 +++++++++++++ 2 files changed, 21 insertions(+) diff --git a/i18n.go b/i18n.go index e219c36..acc8a1a 100644 --- a/i18n.go +++ b/i18n.go @@ -16,6 +16,14 @@ func T(messageID string, args ...any) string { return messageID } +// Translate translates a message using the default service and returns a Core result. +func Translate(messageID string, args ...any) core.Result { + if svc := Default(); svc != nil { + return svc.Translate(messageID, args...) + } + return core.Result{Value: messageID, OK: true} +} + // Raw translates without i18n.* namespace magic. func Raw(messageID string, args ...any) string { if svc := Default(); svc != nil { diff --git a/i18n_test.go b/i18n_test.go index 0da93b9..1058b74 100644 --- a/i18n_test.go +++ b/i18n_test.go @@ -39,6 +39,19 @@ func TestT_Good_MissingKey(t *testing.T) { assert.Equal(t, "nonexistent.key.test", got) } +// --- Package-level Translate() --- + +func TestTranslate_Good(t *testing.T) { + svc, err := New() + require.NoError(t, err) + _ = Init() + SetDefault(svc) + + result := Translate("prompt.yes") + require.True(t, result.OK) + assert.Equal(t, "y", result.Value) +} + // --- Package-level Raw() --- func TestRaw_Good(t *testing.T) { -- 2.45.3