i18n

package
v1.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 4, 2020 License: Apache-2.0 Imports: 8 Imported by: 0

README

功能简介

  • 使用简单,支持加载多个语言,多个文件
  • 两种数据加载模式:单文件 FileMode 、文件夹 DirMode;默认是文件夹模式
  • 支持设置默认语言,备用语言;当在默认语言数据没找到时,自动尝试到备用语言查找
  • 支持参数替换,也有两种模式:SprintfMode 通过 fmt.Sprintf 替换参数,ReplaceMode 则使用 strings.Replacer 替换

快速使用

lang/
    en/
        default.ini
        ...
    zh-CN/
        default.ini
        ...

初始化

    import "github.com/abulo/ratel/i18n"

    languages := map[string]string{
        "en": "English",
        "zh-CN": "简体中文",
        // "zh-TW": "繁体中文",
    }

    // 这里直接初始化的默认实例
    i18n.Init("conf/lang", "en", languages)

    // 或者创建自定义的新实例
    // i18n.New(langDir string, defLang string, languages)
    // i18n.NewEmpty()

翻译数据

    // 从指定的语言翻译
    msg := i18n.Tr("en", "key")

    // 从默认语言翻译
    msg = i18n.DefTr("key")
    // with arguments. 
    msg = i18n.DefTr("key1", "arg1", "arg2")

参数替换模式

使用 SprintfMode(defaults) 模式:

# en.ini
desc = I am %s, age is %d

按kv顺序传入参数使用:

msg := i18n.Tr("en", "desc", "name", "tom", "age", 22)
// Output: "I am tom, age is 22"

使用 ReplaceMode 替换模式:

# en.ini
desc = I am {name}, age is {age}

传入 map[string]interface{} 参数使用:

i18n.TransMode = i18n.ReplaceMode

msg := i18n.Tr("en", "desc", "desc", map[string]interface{}{
    "name": "tom",
    "age": 22,
})
// Output: "I am tom, age is 22"

Documentation

Index

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 DefTr

func DefTr(key string, args ...interface{}) string

DefTr translate language key from default language

func Dt

func Dt(key string, args ...interface{}) string

Dt translate language key from default language

func T

func T(lang string, key string, args ...interface{}) string

T translate language key to value string

func Tr

func Tr(lang string, key string, args ...interface{}) string

Tr translate language key to value string

Types

type I18n

type I18n struct {

	// mode for the load language files.
	//  0 single language file
	//  1 multi language directory
	LoadMode uint8
	// TODO translate mode.
	//  0 sprintf
	//  1 replace
	TransMode 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 Default

func Default() *I18n

Default get default i18n instance

func Init

func Init(langDir string, defLang string, languages map[string]string) *I18n

Init the default language instance

func New

func New(langDir string, defLang string, languages map[string]string) *I18n

New an i18n instance

func NewEmpty

func NewEmpty() *I18n

NewEmpty nwe an empty i18n instance

func NewWithInit

func NewWithInit(langDir string, defLang string, languages map[string]string) *I18n

NewWithInit a i18n instance and call init

func (*I18n) Add

func (l *I18n) Add(lang string, name string)

Add new language

func (*I18n) DefTr

func (l *I18n) DefTr(key string, args ...interface{}) string

DefTr translate from default lang

func (*I18n) DelLang

func (l *I18n) DelLang(lang string) bool

DelLang from the i18n manager

func (*I18n) Dt

func (l *I18n) Dt(key string, args ...interface{}) string

Dt translate from default lang

func (*I18n) Export

func (l *I18n) Export(lang string) string

Export a language data as INI string

func (*I18n) HasKey

func (l *I18n) HasKey(lang, key string) (ok bool)

HasKey in the language data

func (*I18n) HasLang

func (l *I18n) HasLang(lang string) bool

HasLang in the manager

func (*I18n) Init

func (l *I18n) Init() *I18n

Init load add language files

func (*I18n) Lang

func (l *I18n) Lang(lang string) *ini.Ini

Lang get language data instance

func (*I18n) Languages

func (l *I18n) Languages() map[string]string

Languages get all languages

func (*I18n) LoadFile

func (l *I18n) LoadFile(lang string, file string) (err error)

LoadFile append data to a exist language Usage:

i18n.LoadFile("zh-CN", "path/to/zh-CN.ini")

func (*I18n) LoadString

func (l *I18n) LoadString(lang string, data string) (err error)

LoadString load language data form a string Usage:

i18n.Set("zh-CN", "name = blog")

func (*I18n) NewLang

func (l *I18n) NewLang(lang string, name string)

NewLang create/add a new language Usage:

i18n.NewLang("zh-CN", "简体中文")

func (*I18n) T

func (l *I18n) T(lang, key string, args ...interface{}) string

T translate from a lang by key

func (*I18n) Tr

func (l *I18n) Tr(lang, key string, args ...interface{}) string

Tr translate from a lang by key site.name => [site]

name = my blog

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL