Management and use of multilingual data using INI
files.
- Easy to use, supports loading multiple languages, multiple files
- Two data loading modes: single file
FileMode
(default), folderDirMode
- Support to set the default language and fallback language
- when the default language data is not found, it will automatically try to find the fallback language
- Support parameter replacement, there are two modes
SprintfMode
replaces parameters viafmt.Sprintf
ReplaceMode
uses funcstrings.Replacer
go get github.com/gookit/i18n
Structs on use single FileMode
mode:
lang/
en.ini
ru.ini
zh-CN.ini
zh-TW.ini
... ...
Structs on use folder DirMode
mode:
lang/
en/
default.ini
... ...
zh-CN/
default.ini
... ...
zh-TW/
default.ini
... ...
import "github/gookit/i18n"
languages := map[string]string{
"en": "English",
"zh-CN": "简体中文",
// "zh-TW": "繁体中文",
}
// The default instance initialized directly here
i18n.Init("conf/lang", "en", languages)
// Create a custom new instance
// i18n.New(langDir string, defLang string, languages)
// i18n.NewEmpty()
// Translate from default language
msg = i18n.Dt("key")
// with arguments.
msg = i18n.DefTr("key1", "arg1", "arg2")
// Translate from the specified language
msg := i18n.Tr("en", "key")
Function list:
// Translate from default language
func Dt(key string, args ...interface{}) string
func Dtr(key string, args ...interface{}) string
func DefTr(key string, args ...interface{}) string
// Translate from the specified language
func T(lang, key string, args ...interface{}) string
func Tr(lang, key string, args ...interface{}) string
TIP: default mode is
SprintfMode
# en.ini
desc = I am %s, age is %d
Usage with parameters like sprintf:
msg := i18n.Tr("en", "desc", "tom", 22)
// Output: "I am tom, age is 22"
Enable replace mode:
// set mode
i18n.Std().TransMode = i18n.ReplaceMode
// OR
i18n.Config(func(l *i18n.I18n) {
l.TransMode = i18n.ReplaceMode
})
Examples for language data:
# en.ini
desc = I am {name}, age is {age}
Usage with parameters:
// args is {"name": "tom", "age": 22}
msg := i18n.Tr("en", "desc", "name", "tom", "age", 22)
// Output: "I am tom, age is 22"
Usage with kv-map parameters:
msg := i18n.Tr("en", "desc", map[string]interface{}{
"name": "tom",
"age": 22,
})
// Output: "I am tom, age is 22"
go test -cover
- gookit/ini is an INI config file/data manage implement