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
lang files:
conf/ lang/ en.ini zh-CN.ini
init:
import "github/gookit/i18n" languages := map[string]string{ "en": "English", "zh-CN": "简体中文", "zh-TW": "繁体中文", } i18n.Init("conf/lang", "en", languages)
usage:
// translate from special language val := i18n.Tr("en", "key") // translate from default language val := i18n.DefTr("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.Tr("en", "name")) fmt.Printf("name %s\n", l.DefTr("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 DefTr(key string, args ...interface{}) string
- func Tr(lang string, key string, args ...interface{}) string
- type I18n
- func (l *I18n) Add(lang string, name string)
- func (l *I18n) DefTr(key string, args ...interface{}) string
- func (l *I18n) DelLang(lang string) bool
- 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) Tr(lang, key string, args ...interface{}) string
Examples ¶
Constants ¶
View Source
const ( // language name is file name. "en" -> "lang/en.ini" FileMode uint8 = 0 // language name is dir name, will load all file in the dir. "en" -> "lang/en/*.ini" DirMode uint8 = 1 )
language file load mode
Variables ¶
This section is empty.
Functions ¶
Types ¶
type I18n ¶
type I18n struct { // mode for the load language files. mode: 0 single file, 1 multi dir LoadMode uint8 // default language name. eg. "en" DefaultLang string // spare(fallback) language name. eg. "en" FallbackLang string // contains filtered or unexported fields }
I18n language manager
func NewWithInit ¶ added in v1.0.1
NewWithInit a 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.Set("zh-CN", "name = blog")
Click to show internal directories.
Click to hide internal directories.