-
Notifications
You must be signed in to change notification settings - Fork 148
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chat/translate.go: Concept implementation of client-side translations.
- Loading branch information
Showing
4 changed files
with
57 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters