From ced82a1cabef7dab32b64ef7ca0db6ed1aa0deda Mon Sep 17 00:00:00 2001 From: Tnze Date: Tue, 6 Aug 2019 14:51:48 +0800 Subject: [PATCH] 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" --- chat/chatMsg.go | 35 +++++++++++++++++++++++++--------- chat/chatMsg_test.go | 18 +++++++++++------ data/{ => lang/en-us}/en_us.go | 11 ++++++----- 3 files changed, 44 insertions(+), 20 deletions(-) rename data/{ => lang/en-us}/en_us.go (99%) diff --git a/chat/chatMsg.go b/chat/chatMsg.go index cb524d1..c58fcd3 100644 --- a/chat/chatMsg.go +++ b/chat/chatMsg.go @@ -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 { diff --git a/chat/chatMsg_test.go b/chat/chatMsg_test.go index 6636daa..2165f0c 100644 --- a/chat/chatMsg_test.go +++ b/chat/chatMsg_test.go @@ -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 { diff --git a/data/en_us.go b/data/lang/en-us/en_us.go similarity index 99% rename from data/en_us.go rename to data/lang/en-us/en_us.go index 21a8d0a..65e8ca0 100644 --- a/data/en_us.go +++ b/data/lang/en-us/en_us.go @@ -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",