README

I18N Middleware for CleverGo

Build Status Coverage Status Go.Dev reference Go Report Card Release Downloads Chat Community

$ go get -u clevergo.tech/i18nmiddleware

Usage

package main

import (
	"net/http"

	"clevergo.tech/clevergo"
	"clevergo.tech/i18nmiddleware"
	"github.com/nicksnyder/go-i18n/v2/i18n"
	"golang.org/x/text/language"
)

func main() {
	app := clevergo.New()
	bundle := i18n.NewBundle(language.English)
	bundle.ParseMessageFileBytes([]byte(`{"home": "Home"}`), "en.json")
	bundle.ParseMessageFileBytes([]byte(`{"home": "主页"}`), "zh-CN.json")
	app.Use(i18nmiddleware.New(bundle))
	app.Get("/", func(c *clevergo.Context) error {
		localizer := i18nmiddleware.Localizer(c)
		s, _, _ := localizer.LocalizeWithTag(&i18n.LocalizeConfig{
			MessageID: "home",
		})
		return c.String(http.StatusOK, s)
	})
	app.Run(":8080")
}
$ curl http://localhost:8080/
Home

$ curl http://localhost:8080/?lang=zh-CN
主页

$ curl -H "Accept-Language: zh-CN" http://localhost:8080
主页

Documentation

Index

Constants

View Source
const LocalizerKey contextKey = 0

    LocalizerKey is the context key of localizer.

    Variables

    This section is empty.

    Functions

    func Localizer

    func Localizer(c *clevergo.Context) *i18n.Localizer

      Localizer returns the localizer instance from context.

      func New

      func New(bundle *i18n.Bundle, opts ...Option) clevergo.MiddlewareFunc

        New returns a I18N middleware with the given bundle and optional options.

        Types

        type Option

        type Option func(*middleware)

          Option is a function for setting middleware.

          func WithFormField

          func WithFormField(field string) Option

            WithFormField is an option for setting form field.

            Source Files