Documentation ¶
Index ¶
- func MustTr(messageId interface{}) string
- func Tr(messageId interface{}) (string, error)
- type ContextHandler
- type FSLoader
- type I18n
- func (i *I18n) AddLoader(loader Loader)
- func (i *I18n) MastParseMessageFileBytes(buf []byte, path string)
- func (i *I18n) RegisterUnmarshalFunc(format string, unmarshalFunc UnmarshalFunc)
- func (i *I18n) SetDefaultLang(lang language.Tag)
- func (i *I18n) SetLocalizer(lang language.Tag)
- func (i *I18n) Tr(p interface{}) (string, error)
- type LangHandler
- type LangHandlerFunc
- type Loader
- type LoaderOp
- type LoaderOpFunc
- type LocalizeConfig
- type Option
- type OptionFunc
- type UnmarshalFunc
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type ContextHandler ¶
type ContextHandler struct{}
func Localize ¶
func Localize(defaultLang language.Tag, opts ...Option) ContextHandler
Localize initialize i18n...
func (ContextHandler) ServeHTTP ¶
func (_ ContextHandler) ServeHTTP(_ http.ResponseWriter, r *http.Request)
type FSLoader ¶
type FSLoader struct {
// contains filtered or unexported fields
}
func (*FSLoader) ParseMessage ¶
type I18n ¶
type I18n struct {
// contains filtered or unexported fields
}
func (*I18n) MastParseMessageFileBytes ¶
func (*I18n) RegisterUnmarshalFunc ¶
func (i *I18n) RegisterUnmarshalFunc(format string, unmarshalFunc UnmarshalFunc)
func (*I18n) SetDefaultLang ¶
func (*I18n) SetLocalizer ¶
type LangHandlerFunc ¶
type LoaderOp ¶
type LoaderOp interface {
// contains filtered or unexported methods
}
func WithUnmarshal ¶
func WithUnmarshal(format string, fn UnmarshalFunc) LoaderOp
WithUnmarshal register single format unmarshal func
func WithUnmarshalls ¶
func WithUnmarshalls(fns map[string]UnmarshalFunc) LoaderOp
WithUnmarshalls register multi format unmarshal func
type LoaderOpFunc ¶
type LoaderOpFunc func(cfg *FSLoader)
type LocalizeConfig ¶
type LocalizeConfig = i18n.LocalizeConfig
type Option ¶
type Option interface {
// contains filtered or unexported methods
}
func NewLoaderWithPath ¶
func WithLangHandler ¶
func WithLangHandler(handler LangHandler) Option
WithLangHandler get the language from *http.Request, default LangHandler the order of acquisition is: header(always get the value of Accept-Language) -> cookie -> query -> form -> postForm you can use WithLangKey change the default lang key
Example:
loader := i18n.NewLoaderWithPath("language_file_path") i18n.Localize(language.Chinese, loader, i18n.WithLangHandler(i18n.LangHandlerFunc(func(r *http.Request) language.Tag { lang := r.Header.Get("Accept-Language") tag, err := language.Parse(lang) if err != nil { return language.Chinese } return tag })))
func WithLangKey ¶
WithLangKey specifies the default language key when obtained from the LangHandler Except from the Header, there is no limit if you specify LangHandler manually
Example:
i18n.loader :=i18n.NewLoaderWithPath("language_file_path") i18n.Localize(language.Chinese, loader, i18n.WithLangKey("default_language_key"))
func WithLoader ¶
WithLoader Register the Loader interface to *I18n.bundle
Example:
//go:embed examples/lan2/* var langFS embed.FS i18n.Localize(language.Chinese, i18n.NewLoaderWithPath("language_file_path")) i18n.Localize(language.Chinese, i18n.NewLoaderWithFS(langFS, i18n.WithUnmarshal("json", json.Unmarshal)))
type OptionFunc ¶
type OptionFunc func(*I18n)
type UnmarshalFunc ¶
type UnmarshalFunc = i18n.UnmarshalFunc
Click to show internal directories.
Click to hide internal directories.