Explorar el Código

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.
jvoisin hace 1 año
padre
commit
33c648825f
Se han modificado 1 ficheros con 5 adiciones y 8 borrados
  1. 5 8
      internal/locale/printer.go

+ 5 - 8
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
 		}