Now we could use package chat to translate in many language.

But you must call chat.SetLanguage now.
(or just import _ "github.com/Tnze/go-mc/data/lang/en-us"
This commit is contained in:
Tnze
2019-08-06 14:51:48 +08:00
parent dad5e0b75f
commit ced82a1cab
3 changed files with 44 additions and 20 deletions

View File

@ -1,3 +1,11 @@
// Package chat implements Minecraft's chat message encoding system.
//
// The type Message is the Minecraft chat message. Can be encode as JSON
// or net/packet.Field .
//
// It's very recommended that use SetLanguage before using Message.String or Message.ClearString,
// or the translate message will be ignore.
// Note: The package of data/lang/... will SetLanguage on theirs init() so you don't need do again.
package chat
import (
@ -7,7 +15,6 @@ import (
"regexp"
"strings"
"github.com/Tnze/go-mc/data"
pk "github.com/Tnze/go-mc/net/packet"
)
@ -102,22 +109,31 @@ var colors = map[string]string{
"white": "97",
}
// ClearString return the message without escape sequence for ansi color.
// translateMap is the translation table.
// By default it's a void map.
var translateMap = map[string]string{}
// SetLanguage set the translate map to this map.
func SetLanguage(trans map[string]string) {
translateMap = trans
}
// ClearString return the message String without escape sequence for ansi color.
func (m Message) ClearString() string {
var msg strings.Builder
text, _ := trans(m.Text, false)
msg.WriteString(text)
//handle translate
if m.Translate != "" {
if m.Translate != "" && translateMap != nil {
args := make([]interface{}, len(m.With))
for i, v := range m.With {
var arg Message
_ = arg.UnmarshalJSON(v) //ignore error
args[i] = arg.ClearString()
}
_, _ = fmt.Fprintf(&msg, data.EnUs[m.Translate], args...)
_, _ = fmt.Fprintf(&msg, translateMap[m.Translate], args...)
}
if m.Extra != nil {
@ -129,8 +145,8 @@ func (m Message) ClearString() string {
}
// String return the message string with escape sequence for ansi color.
// On windows, you may want print this string using
// github.com/mattn/go-colorable.
// To convert Translated Message to string, you must set
// On windows, you may want print this string using github.com/mattn/go-colorable.
func (m Message) String() string {
var msg, format strings.Builder
if m.Bold {
@ -156,7 +172,8 @@ func (m Message) String() string {
msg.WriteString(text)
//handle translate
if m.Translate != "" {
if m.Translate != "" && translateMap != nil {
args := make([]interface{}, len(m.With))
for i, v := range m.With {
var arg Message
@ -164,7 +181,7 @@ func (m Message) String() string {
args[i] = arg
}
_, _ = fmt.Fprintf(&msg, data.EnUs[m.Translate], args...)
_, _ = fmt.Fprintf(&msg, translateMap[m.Translate], args...)
}
if m.Extra != nil {

View File

@ -1,9 +1,12 @@
package chat
package chat_test
import (
"bytes"
pk "github.com/Tnze/go-mc/net/packet"
"github.com/Tnze/go-mc/chat"
"testing"
_ "github.com/Tnze/go-mc/data/lang/en-us"
pk "github.com/Tnze/go-mc/net/packet"
)
/*
@ -63,8 +66,9 @@ var ctexts = []string{
}
func TestChatMsgFormatString(t *testing.T) {
for i, v := range jsons {
var cm Message
var cm chat.Message
err := cm.UnmarshalJSON([]byte(v))
if err != nil {
t.Error(err)
@ -77,19 +81,21 @@ func TestChatMsgFormatString(t *testing.T) {
func TestChatMsgClearString(t *testing.T) {
for i, v := range jsons {
var cm Message
var cm chat.Message
err := cm.UnmarshalJSON([]byte(v))
if err != nil {
t.Error(err)
}
if str := cm.ClearString(); str != ctexts[i] {
str := cm.ClearString()
if str != ctexts[i] {
t.Errorf("gets %q, wants %q", str, texts[i])
}
}
}
func TestMessage_Encode(t *testing.T) {
codeMsg := Message{Translate: "multiplayer.disconnect.server_full"}.Encode()
codeMsg := chat.Message{Translate: "multiplayer.disconnect.server_full"}.Encode()
var msg pk.Chat
if err := msg.Decode(bytes.NewReader(codeMsg)); err != nil {

View File

@ -1,10 +1,11 @@
package data
package en_us
//TODO: check updates en_us translation
import "github.com/Tnze/go-mc/chat"
// EnUs , the default language translate data
// When update this data, replace "%[0-9]\$s" with "%[0-9]s"
var EnUs = map[string]string{
func init() { chat.SetLanguage(Map) }
// Map is the translate map of en-us
var Map = map[string]string{
"language.name": "English",
"language.region": "United States",
"language.code": "en_us",