Documentation
¶
Overview ¶
Package i18n is a wrapper for go-i18n. It provides a simple API to localize your application.
Index ¶
- Variables
- func Get(id string, opts ...any) string
- func GetCtx(ctx context.Context, id string, opts ...any) string
- func GetLanguage(ctx context.Context) language.Tag
- func Init(language language.Tag, opts ...Option) error
- func Middleware(next http.Handler) http.Handler
- func NewContextWithLanguage(ctx context.Context, language string) context.Context
- func T(id string, opts ...any) string
- func TCtx(ctx context.Context, id string, opts ...any) string
- type LOption
- type LocalizeOption
- type Option
- func WithExtractLanguageFunc(extractLanguageFunc func(ctx context.Context) string) Option
- func WithMissingTranslationHandler(missingTranslationHandler func(id string, err error) string) Option
- func WithTranslationFSFile(fs embed.FS, paths ...string) Option
- func WithTranslationFile(paths ...string) Option
- func WithUnmarshalFunc(format string, unmarshalFunc i18n.UnmarshalFunc) Option
- type Params
Constants ¶
This section is empty.
Variables ¶
var (
ErrI18nNotInitialized = errors.New("i18n is not initialized")
)
Functions ¶
func Get ¶
Get returns the translated message for the given message id.
It uses the default language tag.
Example:
message := i18n.Get("hello", i18n.Params{"name": "John"})
func GetCtx ¶
GetCtx returns the translated message for the given message id.
It uses the language from the context. You can set the language to the context with i18n.Middleware. If the language is not found in the context, it uses the default language tag.
Example:
message := i18n.GetCtx(ctx, "hello", i18n.Params{"name": "John"})
func GetLanguage ¶
GetLanguage returns the language tag from the context.
If the language tag is not found, it returns the default language tag.
func Init ¶
Init initializes the i18n package. It must be called before any other function.
Example:
if err := i18n.Init(language.English, i18n.WithUnmarshalFunc("yaml", yaml.Unmarshal), i18n.WithMessageFilePaths("locales/en.yaml", "locales/id.yaml"), ) err != nil { panic(err) }
func Middleware ¶
Middleware is a middleware that sets the language to the context from the request.
It uses the Accept-Language header to get the language.
func NewContextWithLanguage ¶ added in v0.1.1
NewContextWithLanguage sets the language to the context.
You can use this function to set the language to the context manually.
Types ¶
type LOption ¶ added in v0.1.1
type LOption interface { map[string]interface{} | LocalizeOption }
type LocalizeOption ¶
type LocalizeOption func(*localizeConfig)
LocalizeOption is a function that configures the localizeConfig.
func Default ¶ added in v0.1.1
func Default(defaultMessage string) LocalizeOption
Default sets the default message for the message.
It is used when the message is not found.
Example:
i18n.T("hello", i18n.Default("Hello, {{.name}}!"), i18n.Param("name", "John")))
func Lang ¶
func Lang(language string) LocalizeOption
Lang sets the language for the message.
Example:
i18n.T("hello", i18n.Lang("id"))
func Param ¶
func Param(key string, value interface{}) LocalizeOption
Param set single value of template data for the message.
Example:
i18n.T("hello", i18n.Param("name", "John"))
type Option ¶
type Option func(*config)
Option is the option for the i18n package.
func WithExtractLanguageFunc ¶ added in v0.1.1
WithExtractLanguageFunc sets the language extract function for the middleware.
It is used to extract the language from the context.
func WithMissingTranslationHandler ¶
func WithMissingTranslationHandler(missingTranslationHandler func(id string, err error) string) Option
WithMissingTranslationHandler sets the missing translation handler for the bundle.
It is used to handle the missing translation. The default handler returns the message ID.
func WithTranslationFSFile ¶
WithTranslationFSFile sets the message file paths for the bundle.
It is similar to WithTranslationFile, but it uses embed.FS as file system.
func WithTranslationFile ¶
WithTranslationFile sets the message file paths for the bundle.
func WithUnmarshalFunc ¶
func WithUnmarshalFunc(format string, unmarshalFunc i18n.UnmarshalFunc) Option
WithUnmarshalFunc sets the unmarshal function for the bundle.
It is used to unmarshal the message file. You can use yaml.Unmarshal, json.Unmarshal, or any other unmarshal function.