From 9eca049323b5fc7dc8f2009bd1b09b7417c92553 Mon Sep 17 00:00:00 2001 From: Virgil Date: Thu, 2 Apr 2026 04:30:09 +0000 Subject: [PATCH] feat(i18n): forward variadic numeric shorthand Co-Authored-By: Virgil --- i18n.go | 10 +++++++--- i18n_test.go | 12 +++++++----- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/i18n.go b/i18n.go index 5900dd1..e219c36 100644 --- a/i18n.go +++ b/i18n.go @@ -93,14 +93,18 @@ func CurrentDebug() bool { return false } -// N formats a number using the i18n.numeric.* namespace. +// N formats a value using the i18n.numeric.* namespace. // // N("number", 1234567) // "1,234,567" // N("percent", 0.85) // "85%" // N("bytes", 1536000) // "1.46 MB" // N("ordinal", 1) // "1st" -func N(format string, value any) string { - return T("i18n.numeric."+format, value) +// +// Multi-argument formats such as "ago" also pass through unchanged: +// +// N("ago", 5, "minutes") // "5 minutes ago" +func N(format string, value any, args ...any) string { + return T("i18n.numeric."+format, append([]any{value}, args...)...) } // Prompt translates a prompt key from the prompt namespace. diff --git a/i18n_test.go b/i18n_test.go index ee53d72..0da93b9 100644 --- a/i18n_test.go +++ b/i18n_test.go @@ -142,16 +142,18 @@ func TestN_Good(t *testing.T) { name string format string value any + args []any want string }{ - {"number", "number", int64(1234567), "1,234,567"}, - {"percent", "percent", 0.85, "85%"}, - {"bytes", "bytes", int64(1536000), "1.46 MB"}, - {"ordinal", "ordinal", 1, "1st"}, + {"number", "number", int64(1234567), nil, "1,234,567"}, + {"percent", "percent", 0.85, nil, "85%"}, + {"bytes", "bytes", int64(1536000), nil, "1.46 MB"}, + {"ordinal", "ordinal", 1, nil, "1st"}, + {"ago", "ago", 5, []any{"minutes"}, "5 minutes ago"}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - got := N(tt.format, tt.value) + got := N(tt.format, tt.value, tt.args...) assert.Equal(t, tt.want, got) }) } -- 2.45.3