Skip to content

Commit

Permalink
chat/translate.go: Translate TranslationString and T/translation argu…
Browse files Browse the repository at this point in the history
…ments.

Resolves #968.
  • Loading branch information
Sandertv committed Dec 23, 2024
1 parent 381a13c commit 0c82bfe
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 15 deletions.
29 changes: 18 additions & 11 deletions server/player/chat/translate.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,25 +75,24 @@ func (t Translation) Resolve(l language.Tag) string {
// F takes arguments for a translation string passed and returns a filled out
// translation that may be sent to players. The number of arguments passed must
// be exactly equal to the number specified in Translate. If not, F will panic.
// Arguments passed are converted to strings using fmt.Sprint(). Exceptions are
// made for argument values of the type TranslationString, Translation and
// translation, which are resolved based on the Translator's language.
// Translations used as arguments should not require any parameters.
func (t Translation) F(a ...any) translation {
if len(a) != t.params {
panic(fmt.Sprintf("translation '%v' requires exactly %v parameters, got %v", t.format, t.params, len(a)))
}
params := make([]string, len(a))
for i, arg := range a {
params[i] = fmt.Sprint(arg)
}
return translation{t: t, params: params, fallbackParams: a}
return translation{t: t, params: a}
}

// translation is a translation string with its arguments filled out. Resolve may
// be called to obtain the translated version of the translation string and
// Params may be called to obtain the parameters passed in Translation.F.
// translation implements the fmt.Stringer and error interfaces.
type translation struct {
t Translation
params []string
fallbackParams []any
t Translation
params []any
}

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

// Params returns a slice of values that are used to parameterise the
// translation returned by Resolve.
func (t translation) Params() []string {
return t.params
func (t translation) Params(l language.Tag) []string {
params := make([]string, len(t.params))
for i, arg := range t.params {
if str, ok := arg.(TranslationString); ok {
params[i] = str.Resolve(l)
continue
}
params[i] = fmt.Sprint(arg)
}
return params
}

// String formats and returns the fallback value of the translation.
func (t translation) String() string {
return fmt.Sprintf(text.Colourf(t.t.format, t.t.fallback), t.fallbackParams...)
return fmt.Sprintf(text.Colourf(t.t.format, t.t.fallback), t.params...)
}

// Error formats and returns the fallback value of the translation.
Expand Down
6 changes: 3 additions & 3 deletions server/session/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ func (s *Session) SendCommandOutput(output *cmd.Output, l language.Tag) {
for _, message := range output.Messages() {
om := protocol.CommandOutputMessage{Success: true, Message: message.String()}
if t, ok := message.(translation); ok {
om.Message, om.Parameters = t.Resolve(l), t.Params()
om.Message, om.Parameters = t.Resolve(l), t.Params(l)
}
messages = append(messages, om)
}
for _, err := range output.Errors() {
om := protocol.CommandOutputMessage{Message: err.Error()}
if t, ok := err.(translation); ok {
om.Message, om.Parameters = t.Resolve(l), t.Params()
om.Message, om.Parameters = t.Resolve(l), t.Params(l)
}
messages = append(messages, om)
}
Expand All @@ -41,7 +41,7 @@ func (s *Session) SendCommandOutput(output *cmd.Output, l language.Tag) {

type translation interface {
Resolve(l language.Tag) string
Params() []string
Params(l language.Tag) []string
}

// sendAvailableCommands sends all available commands of the server. Once sent, they will be visible in the
Expand Down
2 changes: 1 addition & 1 deletion server/session/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (s *Session) SendTranslation(t chat.Translation, l language.Tag, a []any) {
TextType: packet.TextTypeTranslation,
NeedsTranslation: true,
Message: tr.Resolve(l),
Parameters: tr.Params(),
Parameters: tr.Params(l),
})
}

Expand Down

0 comments on commit 0c82bfe

Please sign in to comment.