Skip to content

Commit 0c82bfe

Browse files
committed
chat/translate.go: Translate TranslationString and T/translation arguments.
Resolves #968.
1 parent 381a13c commit 0c82bfe

File tree

3 files changed

+22
-15
lines changed

3 files changed

+22
-15
lines changed

server/player/chat/translate.go

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -75,25 +75,24 @@ func (t Translation) Resolve(l language.Tag) string {
7575
// F takes arguments for a translation string passed and returns a filled out
7676
// translation that may be sent to players. The number of arguments passed must
7777
// 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.
7882
func (t Translation) F(a ...any) translation {
7983
if len(a) != t.params {
8084
panic(fmt.Sprintf("translation '%v' requires exactly %v parameters, got %v", t.format, t.params, len(a)))
8185
}
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}
8787
}
8888

8989
// translation is a translation string with its arguments filled out. Resolve may
9090
// be called to obtain the translated version of the translation string and
9191
// Params may be called to obtain the parameters passed in Translation.F.
9292
// translation implements the fmt.Stringer and error interfaces.
9393
type translation struct {
94-
t Translation
95-
params []string
96-
fallbackParams []any
94+
t Translation
95+
params []any
9796
}
9897

9998
// Resolve translates the TranslationString of the translation to the language
@@ -104,13 +103,21 @@ func (t translation) Resolve(l language.Tag) string {
104103

105104
// Params returns a slice of values that are used to parameterise the
106105
// 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
109116
}
110117

111118
// String formats and returns the fallback value of the translation.
112119
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...)
114121
}
115122

116123
// Error formats and returns the fallback value of the translation.

server/session/command.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ func (s *Session) SendCommandOutput(output *cmd.Output, l language.Tag) {
1919
for _, message := range output.Messages() {
2020
om := protocol.CommandOutputMessage{Success: true, Message: message.String()}
2121
if t, ok := message.(translation); ok {
22-
om.Message, om.Parameters = t.Resolve(l), t.Params()
22+
om.Message, om.Parameters = t.Resolve(l), t.Params(l)
2323
}
2424
messages = append(messages, om)
2525
}
2626
for _, err := range output.Errors() {
2727
om := protocol.CommandOutputMessage{Message: err.Error()}
2828
if t, ok := err.(translation); ok {
29-
om.Message, om.Parameters = t.Resolve(l), t.Params()
29+
om.Message, om.Parameters = t.Resolve(l), t.Params(l)
3030
}
3131
messages = append(messages, om)
3232
}
@@ -41,7 +41,7 @@ func (s *Session) SendCommandOutput(output *cmd.Output, l language.Tag) {
4141

4242
type translation interface {
4343
Resolve(l language.Tag) string
44-
Params() []string
44+
Params(l language.Tag) []string
4545
}
4646

4747
// sendAvailableCommands sends all available commands of the server. Once sent, they will be visible in the

server/session/text.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func (s *Session) SendTranslation(t chat.Translation, l language.Tag, a []any) {
2424
TextType: packet.TextTypeTranslation,
2525
NeedsTranslation: true,
2626
Message: tr.Resolve(l),
27-
Parameters: tr.Params(),
27+
Parameters: tr.Params(l),
2828
})
2929
}
3030

0 commit comments

Comments
 (0)