Skip to content

Commit

Permalink
chat/translate.go: Concept implementation of client-side translations.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sandertv committed Dec 20, 2024
1 parent b297b65 commit 1460b2d
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 0 deletions.
12 changes: 12 additions & 0 deletions server/player/chat/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ func (chat *Chat) WriteString(s string) (n int, err error) {
return len(s), nil
}

func (chat *Chat) Writet(t Translation) {
chat.m.Lock()
defer chat.m.Unlock()
for _, subscriber := range chat.subscribers {
if translator, ok := subscriber.(Translator); ok {
translator.Messaget(t)
continue
}
subscriber.Message(t.String())
}
}

// Subscribe adds a subscriber to the chat, sending it every message written to
// the chat. In order to remove it again, use Chat.Unsubscribe().
func (chat *Chat) Subscribe(s Subscriber) {
Expand Down
4 changes: 4 additions & 0 deletions server/player/chat/subscriber.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ type Subscriber interface {
Message(a ...any)
}

type Translator interface {
Messaget(t Translation)
}

// StdoutSubscriber is an implementation of Subscriber that forwards messages
// sent to the chat to the stdout.
type StdoutSubscriber struct{}
Expand Down
40 changes: 40 additions & 0 deletions server/player/chat/translate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package chat

import (
"fmt"
"github.com/df-mc/dragonfly/server/internal/sliceutil"
)

var JoinMessage = translate("%multiplayer.player.joined", 1, "%v joined the game")
var QuitMessage = translate("%multiplayer.player.left", 1, "%v left the game")

func translate(format string, params int, fallback string) Translatable {
return Translatable{format: format, params: params, fallback: fallback}
}

type Translatable struct {
format string
params int
fallback string
}

func (t Translatable) 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{format: t.format, fallback: t.fallback, params: params}
}

type Translation struct {
format string
fallback string
params []string
}

func (t Translation) String() string {
return fmt.Sprintf(t.fallback, sliceutil.Convert[any](t.params)...)
}
1 change: 1 addition & 0 deletions server/session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ func (s *Session) close(tx *world.Tx, c Controllable) {
s.chunkLoader.Close(tx)

if s.quitMessage != "" {
chat.Global.Writet(chat.QuitMessage.F(s.conn.IdentityData().DisplayName))
_, _ = fmt.Fprintln(chat.Global, text.Colourf("<yellow>%v</yellow>", fmt.Sprintf(s.quitMessage, s.conn.IdentityData().DisplayName)))
}
chat.Global.Unsubscribe(c)
Expand Down

0 comments on commit 1460b2d

Please sign in to comment.