i18n

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2020 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package i18n i18n 国际化

 example:
 bundle := i18n.NewBundle(language.Chinese).LoadFiles("./locales", toml.Unmarshal)
	log.Println(bundle.NewPrinter(language.English).Translate("Hello", i18n.Data{"name": "medivh", "count": 156}, 156))
 # return 你好世界

Index

Constants

This section is empty.

Variables

View Source
var LangKey = "lang"

LangKey 默认从URL取语言值得键名

Functions

func GetAcceptLanguages

func GetAcceptLanguages(ctx *gin.Context) []language.Tag

GetAcceptLanguages 根据 URL 传参和 Accept-Language 获取查询语言

按权重对接受语言排序
如果URL存在正确language将会是最高权重

Types

type Bundle

type Bundle struct {
	// contains filtered or unexported fields
}

Bundle 国际化对外操作结构体

func NewBundle

func NewBundle(tag language.Tag) *Bundle

NewBundle 得到一个新的国际化实例

func (*Bundle) LoadFiles

func (bundle *Bundle) LoadFiles(path string, typ string, unmarshaler Unmarshal) *Bundle

LoadFiles walk file dir and load messages language file like

  • path | -- zh.yaml | -- en.yaml

func (*Bundle) NewPrinter

func (bundle *Bundle) NewPrinter(tags ...language.Tag) *Printer

NewPrinter 根据传入语言tag获得具体翻译组件

func (*Bundle) SetMessage

func (bundle *Bundle) SetMessage(tag language.Tag, key string, message map[string]string)

SetMessage add message

type Data

type Data map[string]interface{}

Data 自定义的需要被模板解析的信息, 在不需要传入结构体时可以使用此类型

type Message

type Message struct {
	ID      string             // message key
	Default *template.Template // message template
	Zero    *template.Template // message template
	One     *template.Template // message template
	Two     *template.Template // message template
	Few     *template.Template // message template
	Many    *template.Template // message template
}

Message 具体翻译模板的内容

type Messages

type Messages map[language.Tag]map[string]*Message

Messages 所有的模板信息

type Printer

type Printer struct {
	// contains filtered or unexported fields
}

Printer 一个新的message translate 对象, 一般不需要调用他

func (*Printer) SetFewRule

func (p *Printer) SetFewRule(min, max int) *Printer

SetFewRule 自定义 few 信息模板的few规则, 在min-max范围内将使用few模板

func (*Printer) SetManyRule

func (p *Printer) SetManyRule(min int) *Printer

SetManyRule 自定义 Many信息模板规则,大于等于 min 将使用 many 模板

func (*Printer) Translate

func (p *Printer) Translate(key string, data interface{}, plurals ...int) string

Translate 根据传入模板ID进行翻译, data can be nil

type Unmarshal

type Unmarshal func(data []byte, v interface{}) error

Unmarshal 模板文本decode方法

Jump to

Keyboard shortcuts

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