@@ -75,25 +75,24 @@ func (t Translation) Resolve(l language.Tag) string {
75
75
// F takes arguments for a translation string passed and returns a filled out
76
76
// translation that may be sent to players. The number of arguments passed must
77
77
// be exactly equal to the number specified in Translate. If not, F will panic.
78
+ // Arguments passed are converted to strings using fmt.Sprint(). Exceptions are
79
+ // made for argument values of the type TranslationString, Translation and
80
+ // translation, which are resolved based on the Translator's language.
81
+ // Translations used as arguments should not require any parameters.
78
82
func (t Translation ) F (a ... any ) translation {
79
83
if len (a ) != t .params {
80
84
panic (fmt .Sprintf ("translation '%v' requires exactly %v parameters, got %v" , t .format , t .params , len (a )))
81
85
}
82
- params := make ([]string , len (a ))
83
- for i , arg := range a {
84
- params [i ] = fmt .Sprint (arg )
85
- }
86
- return translation {t : t , params : params , fallbackParams : a }
86
+ return translation {t : t , params : a }
87
87
}
88
88
89
89
// translation is a translation string with its arguments filled out. Resolve may
90
90
// be called to obtain the translated version of the translation string and
91
91
// Params may be called to obtain the parameters passed in Translation.F.
92
92
// translation implements the fmt.Stringer and error interfaces.
93
93
type translation struct {
94
- t Translation
95
- params []string
96
- fallbackParams []any
94
+ t Translation
95
+ params []any
97
96
}
98
97
99
98
// Resolve translates the TranslationString of the translation to the language
@@ -104,13 +103,21 @@ func (t translation) Resolve(l language.Tag) string {
104
103
105
104
// Params returns a slice of values that are used to parameterise the
106
105
// translation returned by Resolve.
107
- func (t translation ) Params () []string {
108
- return t .params
106
+ func (t translation ) Params (l language.Tag ) []string {
107
+ params := make ([]string , len (t .params ))
108
+ for i , arg := range t .params {
109
+ if str , ok := arg .(TranslationString ); ok {
110
+ params [i ] = str .Resolve (l )
111
+ continue
112
+ }
113
+ params [i ] = fmt .Sprint (arg )
114
+ }
115
+ return params
109
116
}
110
117
111
118
// String formats and returns the fallback value of the translation.
112
119
func (t translation ) String () string {
113
- return fmt .Sprintf (text .Colourf (t .t .format , t .t .fallback ), t .fallbackParams ... )
120
+ return fmt .Sprintf (text .Colourf (t .t .format , t .t .fallback ), t .params ... )
114
121
}
115
122
116
123
// Error formats and returns the fallback value of the translation.
0 commit comments