Translation:
# - DefaultLanguage -
# Specify the default game language (fallback)
DefaultLanguage: 'en'
# - Language -
# Specify the game language
Language: 'en'
# - LanguagePaths -
# Specify the game language file paths
LanguagePaths:
- '_datafiles/localize'
- '_datafiles/world/default/localize'All localize files are in LanguagePaths, filename format is: <language>.yaml, language can be found at: https://github.com/golang/text/blob/master/language/tags.go
Localize files example:
- English:
en.yaml - German:
de.yaml - Chinese:
zh.yaml
Localize file content format is: msgID: 'Translation value'
Example:
MsgFoo: 'Message Foo'
'Msg Bar': 'Message Bar'
Hello:Note: If the
Translation valueis equals to themsgID, theTranslation valuecan be set to empty
- If the
Translation valueof amsgIDfor the current language does not exist or is empty, it will fallback to the default language (usually English). - If the
Translation valueof amsgIDin the default language does not exist or is empty, it will fall back tomsgID.
import (
"github.com/GoMudEngine/GoMud/internal/language"
)
// Simple message
language.T(`Hello`)
// Message with arguments
language.T(`Hello {{ .name }}`, map[string]string{
"name": "alex",
})
// Message with string format
fmt.Sprintf(language.T(`%d users online`), userCt)import (
"github.com/GoMudEngine/GoMud/internal/lauguage"
)
// Passing arguments to template
templates.Process("foo/bar", map[any]any{
"name": "alex",
})// Simple message
{{ t "Hello" }}
// Message with arguments
{{- $map := map "name" .name -}}
{{ t "Hello {{ .name }}" $map }}
// Message with string format
{{ t "%d users online" }}en.yaml:
Hello: 'Hello'
'Hello {{ .name }}': 'Hello {{ .name }}'
'%d users online': '%d users online'de.yaml
Hello: 'Hallo'
'Hello {{ .name }}': 'Hallo {{ .name }}'
'%d users online': '%d benutzer online'If the text to be translated is neither in Golang nor in a template, such as from the configuration file, you need to invoke templates.ProcessText() to translate it.
Example of motd in configuration:
m := configs.GetServerConfig().Motd.String()
text, err := templates.ProcessText(m, nil)
if err != nil {
text = m
}
user.SendText(text)- Completion of all texts to be translated and localized files
- Other scenarios that require translation functionality, such as room descriptions, item names/descriptions, etc.
- Idea: Specify different port numbers (Telnet/HTTP) to output different languages