feat(i18n): localise count string helpers

Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
Virgil 2026-04-02 08:12:33 +00:00
parent f13446fb73
commit 1c45f39d36
4 changed files with 32 additions and 4 deletions

View file

@ -100,7 +100,7 @@ func (s *Subject) CountString() string {
if s == nil {
return "1"
}
return core.Sprintf("%d", s.count)
return FormatNumber(int64(s.count))
}
func (s *Subject) GenderString() string {
if s == nil {

View file

@ -41,6 +41,21 @@ func TestSubject_Count_Good(t *testing.T) {
assert.True(t, subj.IsPlural())
}
func TestSubject_CountString_UsesLocaleFormatting(t *testing.T) {
svc, err := New()
require.NoError(t, err)
prev := Default()
SetDefault(svc)
t.Cleanup(func() {
SetDefault(prev)
})
require.NoError(t, SetLanguage("fr"))
subj := S("file", "test.txt").Count(1234)
assert.Equal(t, "1 234", subj.CountString())
}
func TestSubject_Count_Bad_NilReceiver(t *testing.T) {
var s *Subject
result := s.Count(5)

View file

@ -1,7 +1,5 @@
package i18n
import "dappco.re/go/core"
// TranslationContext provides disambiguation for translations.
//
// T("direction.right", C("navigation")) // "rechts" (German)
@ -137,7 +135,7 @@ func (c *TranslationContext) CountString() string {
if c == nil {
return "1"
}
return core.Sprintf("%d", c.count)
return FormatNumber(int64(c.count))
}
// IsPlural reports whether the count is plural.

View file

@ -101,6 +101,21 @@ func TestTranslationContext_WithFormality_Good(t *testing.T) {
}
}
func TestTranslationContext_CountString_UsesLocaleFormatting(t *testing.T) {
svc, err := New()
require.NoError(t, err)
prev := Default()
SetDefault(svc)
t.Cleanup(func() {
SetDefault(prev)
})
require.NoError(t, SetLanguage("fr"))
ctx := C("test").Count(1234)
assert.Equal(t, "1 234", ctx.CountString())
}
// --- Set / Get ---
func TestTranslationContext_SetGet_Good(t *testing.T) {