From 33c648825fc316288d733e064c758e94bb2aa9d3 Mon Sep 17 00:00:00 2001 From: jvoisin Date: Sun, 6 Jul 2025 00:30:09 +0200 Subject: [PATCH] refactor(locale): make Printf's code structure similar to Print's And also change the order of the cases in the Plural function, to make it explicit that []string shouldn't match []any. --- internal/locale/printer.go | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/internal/locale/printer.go b/internal/locale/printer.go index 900cae9e..fe0d3d19 100644 --- a/internal/locale/printer.go +++ b/internal/locale/printer.go @@ -25,12 +25,9 @@ func (p *Printer) Print(key string) string { func (p *Printer) Printf(key string, args ...any) string { translation := key - str, found := dict[key] - if found { - var valid bool - translation, valid = str.(string) - if !valid { if dict, err := getTranslationDict(p.language); err == nil { + if str, ok := dict[key]; ok { + if translation, ok = str.(string); !ok { translation = key } } @@ -50,12 +47,12 @@ func (p *Printer) Plural(key string, n int, args ...interface{}) string { var plurals []string switch v := choices.(type) { - case []interface{}: + case []string: + plurals = v + case []any: for _, v := range v { plurals = append(plurals, fmt.Sprint(v)) } - case []string: - plurals = v default: return key }