Documentation ¶
Index ¶
- func CookieProvider(r *http.Request, key string) language.Tag
- func FormProvider(r *http.Request, key string) language.Tag
- func HeaderProvider(r *http.Request, key string) language.Tag
- func MustTr[T Message](ctx context.Context, message T) string
- func ParseFromHeader(val string) language.Tag
- func PostFormProvider(r *http.Request, key string) language.Tag
- func QueryProvider(r *http.Request, key string) language.Tag
- func Tr[T Message](ctx context.Context, message T) (string, error)
- type Entry
- type I18n
- type LOpt
- type LanguageKey
- 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 any) Option
- func WithLoader(loader Loader) Option
- func WithProvider[T, U any](provider LanguageProvider[T, U]) Option
- type Result
- type UnmarshalFunc
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ParseFromHeader ¶
Types ¶
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 LanguageKey ¶
type LanguageKey struct{}
type LanguageProvider ¶
type Localized ¶
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 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)))
func WithProvider ¶
func WithProvider[T, U any](provider LanguageProvider[T, U]) 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.WithProvider(i18n.HeaderProvider) )
type Result ¶
type Result struct { Funcs map[string]UnmarshalFunc Entries []Entry }
type UnmarshalFunc ¶
type UnmarshalFunc = i18n.UnmarshalFunc