diff --git a/handler_test.go b/handler_test.go index 1464bb9..ba65042 100644 --- a/handler_test.go +++ b/handler_test.go @@ -123,6 +123,11 @@ func TestCountHandler(t *testing.T) { t.Errorf("CountHandler.Handle(file, Subject.Count(3)) = %q, want %q", got, "3 files") } + got = h.Handle("i18n.count.file", []any{map[string]string{"Count": "3"}}, nil) + if got != "3 files" { + t.Errorf("CountHandler.Handle(file, map[string]string[Count:3]) = %q, want %q", got, "3 files") + } + got = h.Handle("i18n.count.file", []any{C("file").Set("Count", 3)}, nil) if got != "3 files" { t.Errorf("CountHandler.Handle(file, TranslationContext.Count=3) = %q, want %q", got, "3 files") @@ -249,6 +254,7 @@ func TestNumericHandler(t *testing.T) { {"i18n.numeric.ordinal", []any{11}, "11th"}, {"i18n.numeric.percent", []any{0.85}, "85%"}, {"i18n.numeric.bytes", []any{int64(1536000)}, "1.46 MB"}, + {"i18n.numeric.number", []any{"1234567"}, "1,234,567"}, {"i18n.numeric.ago", []any{5, "minutes"}, "5 minutes ago"}, } diff --git a/transform.go b/transform.go index f12f25b..22fb3d0 100644 --- a/transform.go +++ b/transform.go @@ -1,5 +1,11 @@ package i18n +import ( + "strconv" + + "dappco.re/go/core" +) + func getCount(data any) int { if data == nil { return 0 @@ -40,6 +46,13 @@ func getCount(data any) int { if c, ok := d["count"]; ok { return c } + case map[string]string: + if c, ok := d["Count"]; ok { + return toInt(c) + } + if c, ok := d["count"]; ok { + return toInt(c) + } } return toInt(data) } @@ -73,6 +86,13 @@ func toInt(v any) int { return int(n) case float32: return int(n) + case string: + if n == "" { + return 0 + } + if parsed, err := strconv.Atoi(core.Trim(n)); err == nil { + return parsed + } } return 0 } @@ -106,6 +126,13 @@ func toInt64(v any) int64 { return int64(n) case float32: return int64(n) + case string: + if n == "" { + return 0 + } + if parsed, err := strconv.ParseInt(core.Trim(n), 10, 64); err == nil { + return parsed + } } return 0 } @@ -139,6 +166,13 @@ func toFloat64(v any) float64 { return float64(n) case uint8: return float64(n) + case string: + if n == "" { + return 0 + } + if parsed, err := strconv.ParseFloat(core.Trim(n), 64); err == nil { + return parsed + } } return 0 } diff --git a/transform_test.go b/transform_test.go index e21606b..15d1cc5 100644 --- a/transform_test.go +++ b/transform_test.go @@ -18,6 +18,7 @@ func TestGetCount_Good(t *testing.T) { {"map_string_any", map[string]any{"Count": 5}, 5}, {"map_string_any_float", map[string]any{"Count": 3.7}, 3}, {"map_string_int", map[string]int{"Count": 42}, 42}, + {"map_string_string", map[string]string{"Count": "9"}, 9}, {"no_count_key", map[string]any{"Name": "test"}, 0}, {"wrong_type", "a string", 0}, } @@ -50,6 +51,7 @@ func TestToInt_Good(t *testing.T) { {"uint8", uint8(50), 50}, {"float64", float64(3.14), 3}, {"float32", float32(2.71), 2}, + {"string_int", "123", 123}, {"string", "not a number", 0}, {"bool", true, 0}, } @@ -82,6 +84,7 @@ func TestToInt64_Good(t *testing.T) { {"uint8", uint8(50), 50}, {"float64", float64(3.14), 3}, {"float32", float32(2.71), 2}, + {"string_int64", "123", 123}, {"string", "not a number", 0}, {"bool", true, 0}, } @@ -114,6 +117,7 @@ func TestToFloat64_Good(t *testing.T) { {"uint32", uint32(30), 30.0}, {"uint16", uint16(40), 40.0}, {"uint8", uint8(50), 50.0}, + {"string_float", "3.5", 3.5}, {"string", "not a number", 0}, {"bool", true, 0}, }