Documentation ¶
Index ¶
- func CookieProvider(key string, r *http.Request) language.Tag
- func FormProvider(key string, r *http.Request) language.Tag
- func HeaderProvider(_ string, r *http.Request) language.Tag
- func MustTr[T Message](ctx context.Context, message T) string
- func ParseFromHeader(val string) language.Tag
- func PostFormProvider(key string, r *http.Request) language.Tag
- func QueryProvider(key string, r *http.Request) language.Tag
- func Tr[T Message](ctx context.Context, message T) (string, error)
- type Entry
- type I18n
- type LOpt
- type LanguageProvider
- type Loader
- type Localized
- type Message
- type Option
- func NewLoaderWithFS(fs fs.FS, opts ...LOpt) Option
- func NewLoaderWithPath(path string, opts ...LOpt) Option
- func WithDefaultLanguage(tag language.Tag) Option
- func WithLanguageKey(key string) Option
- func WithLanguageProvider(providers ...LanguageProvider) Option
- func WithLoader(loader Loader) Option
- type Result
- type UnmarshalFunc
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ParseFromHeader ¶ added in v2.1.1
Types ¶
type I18n ¶ added in v2.1.0
type I18n struct {
// contains filtered or unexported fields
}
func Initialize ¶ added in v2.1.0
type LOpt ¶
type LOpt func(*fsLoader)
func WithUnmarshal ¶
func WithUnmarshal(format string, fn UnmarshalFunc) LOpt
WithUnmarshal register single format unmarshal func
func WithUnmarshalls ¶
func WithUnmarshalls(fns map[string]UnmarshalFunc) LOpt
WithUnmarshalls register multi format unmarshal func
type Localized ¶ added in v2.1.3
type Localized = i18n.LocalizeConfig
type Option ¶
type Option func(*I18n)
func NewLoaderWithPath ¶
func WithDefaultLanguage ¶
WithDefaultLanguage specify the default language, which is used when it is not available from the LanguageProvider
Example:
i18n.loader :=i18n.NewLoaderWithPath("language_file_path") i18n.Handler(http.Handler, i18n.WithLoader(loader), i18n.WithDefaultLanguage(language.Chinese))
func WithLanguageKey ¶
WithLanguageKey specifies the default language key when obtained from the LanguageProvider Except from the Header, there is no limit if you specify LanguageProvider manually
Example:
i18n.loader :=i18n.NewLoaderWithPath("language_file_path") i18n.Handler(http.Handler, i18n.WithLoader(loader), i18n.WithLanguageKey("default_language_key"))
func WithLanguageProvider ¶
func WithLanguageProvider(providers ...LanguageProvider) Option
WithLanguageProvider get the language from *http.Request, default LanguageProvider the order of acquisition is: header(always get the value of Accept-Language) -> cookie -> query -> form -> postForm you can use WithLanguageKey change the default lang key
Example:
loader := i18n.NewLoaderWithPath("language_file_path") i18n.Handler(http.Handler, i18n.WithLoader(loader), i18n.WithLanguageProvider(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 WithLoader ¶
WithLoader Register the Loader interface to *I18n.bundle
Example:
//go:embed examples/lan2/* var langFS embed.FS i18n.Handler(http.Handler, i18n.NewLoaderWithPath("language_file_path")) i18n.Handler(http.Handler, i18n.NewLoaderWithFS(langFS, i18n.WithUnmarshal("json", json.Unmarshal)))
type Result ¶ added in v2.1.0
type Result struct { Funcs map[string]UnmarshalFunc Entries []Entry }
type UnmarshalFunc ¶
type UnmarshalFunc = i18n.UnmarshalFunc