Documentation ¶
Overview ¶
Package i18n is a simple language manager, use INI format file.
Source code and other details for the project are available at GitHub:
https://github.com/gookit/i18n
Language files:
// structs on mode is FileMode(default) lang/ en.ini zh-CN.ini // structs on mode is DirMode lang/ en/ default.ini other.ini zh-CN/ default.ini other.ini
Load:
defaultLang = "en" languages := map[string]string{ "en": "English", "zh-CN": "简体中文", "zh-TW": "繁体中文", } i18n.Init("conf/lang", defaultLang, languages)
Usage:
// translate from default language val := i18n.Dtr("key") // translate from special language val := i18n.Tr("en", "key")
Example ¶
languages := map[string]string{ "en": "English", "zh-CN": "简体中文", // "zh-TW": "繁体中文", } l := New("testdata", "en", languages) l.Init() fmt.Printf("name: %s\n", l.T("en", "name")) fmt.Printf("name: %s\n", l.Dt("name")) fmt.Printf("name: %s\n", l.Tr("zh-CN", "name")) fmt.Printf("use args: %s\n", l.DefTr("argMsg", "inhere"))
Output: name: Blog name: Blog name: 博客 use args: hello inhere, welcome
Index ¶
- Constants
- func AddLang(lang string, name string)
- func Config(fn func(l *I18n))
- func DefTr(key string, args ...interface{}) string
- func Dt(key string, args ...interface{}) string
- func Dtr(key string, args ...interface{}) string
- func LangData(lang string) *ini.Ini
- func Reset()
- func T(lang, key string, args ...interface{}) string
- func Tr(lang, key string, args ...interface{}) string
- type I18n
- func (l *I18n) Add(lang string, name string)
- func (l *I18n) AddLang(lang string, name string)
- func (l *I18n) Config(fn func(l *I18n))
- func (l *I18n) DefTr(key string, args ...interface{}) string
- func (l *I18n) DelLang(lang string) bool
- func (l *I18n) Dt(key string, args ...interface{}) string
- func (l *I18n) Dtr(key string, args ...interface{}) string
- func (l *I18n) Export(lang string) string
- func (l *I18n) HasKey(lang, key string) (ok bool)
- func (l *I18n) HasLang(lang string) bool
- func (l *I18n) Init() *I18n
- func (l *I18n) Lang(lang string) *ini.Ini
- func (l *I18n) Languages() map[string]string
- func (l *I18n) LoadFile(lang string, file string) (err error)
- func (l *I18n) LoadString(lang string, data string) (err error)
- func (l *I18n) NewLang(lang string, name string)
- func (l *I18n) SetValues(lang, group string, values map[string]string) error
- func (l *I18n) T(lang, key string, args ...interface{}) string
- func (l *I18n) Tr(lang, key string, args ...interface{}) string
- func (l *I18n) WithLang(lang string, name string) *I18n
Examples ¶
Constants ¶
View Source
const ( // FileMode language name is file name. "en" -> "lang/en.ini" FileMode uint8 = 0 // DirMode language name is dir name, will load all file in the dir. "en" -> "lang/en/*.ini" DirMode uint8 = 1 // SprintfMode render message arguments by fmt.Sprintf SprintfMode uint8 = 0 // ReplaceMode render message arguments by string replace ReplaceMode uint8 = 1 )
Variables ¶
This section is empty.
Functions ¶
func LangData ¶ added in v1.1.4
func LangData(lang string) *ini.Ini
LangData get language data instance
Types ¶
type I18n ¶
type I18n struct { // LoadMode mode for the load language files. // 0 single language file // 1 multi-language directory LoadMode uint8 // TransMode translate mode. // 0 sprintf // 1 replace TransMode uint8 // DefaultLang default language name. eg. "en" DefaultLang string // FallbackLang spare(fallback) language name. eg. "en" FallbackLang string // contains filtered or unexported fields }
I18n language manager
func NewWithInit ¶ added in v1.0.1
NewWithInit an i18n instance and call init
func (*I18n) LoadFile ¶ added in v1.0.1
LoadFile append data to a exist language
Usage:
i18n.LoadFile("zh-CN", "path/to/zh-CN.ini")
func (*I18n) LoadString ¶ added in v1.0.1
LoadString load language data form a string
Usage:
i18n.LoadString("zh-CN", ` name = blog age = 233 `)
func (*I18n) NewLang ¶ added in v1.0.1
NewLang create/add a new language
Usage:
i18n.NewLang("zh-CN", "简体中文")
Click to show internal directories.
Click to hide internal directories.