i18n

package module
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 13, 2020 License: Apache-2.0 Imports: 5 Imported by: 3

README

i18n middleware

i18n middleware

Usage

Start using it

Download and install it:

$ go get github.com/suisrc/gin-i18n

Import it in your code:

import (
	i18n "github.com/suisrc/gin-i18n"
)
Canonical example:

See the example

package main

import (
	"github.com/gin-gonic/gin"
	i18n "github.com/suisrc/gin-i18n"
	"golang.org/x/text/language"
)

func setupRouter(bundle *i18n.Bundle) *gin.Engine {
	r := gin.Default()

	r.Use(i18n.Serve(bundle))
	r.GET("/ping", func(c *gin.Context) {

		text := i18n.FormatMessage(c,
			&i18n.Message{
				ID:    "ping-text",
				Other: "你好,{{.who}}",
			},
			map[string]interface{}{
				"who": "gin",
			})

		c.String(200, text)
	})
	r.GET("/ping2", func(c *gin.Context) {
		c.String(200, i18n.FormatMessage(c, &i18n.Message{ID: "ping2-text", Other: "我是{{.who}}"}, i18n.Data{"who": "gin"}))
	})
	r.GET("/ping3", func(c *gin.Context) {
		c.String(200, i18n.FormatText(c, &i18n.Message{ID: "ping3-text", Other: "测试"}))
	})

	return r
}

func main() {

	bundle := i18n.NewBundle(
		language.Chinese,
		"example/active.zh-CN.toml",
		"example/active.en-US.toml",
		"example/active.ja-JP.toml",
	)

	r := setupRouter(bundle)
	// Listen and Server in 0.0.0.0:8080
	r.Run(":8080")
}

鸣谢

gin
i18n

Documentation

Index

Constants

View Source
const (
	GinI18nKey = "suisrc/gin-i18n"
)

定义上下文中的键

Variables

This section is empty.

Functions

func FormatMessage

func FormatMessage(c *gin.Context, message *Message, args map[string]interface{}) string

FormatMessage fm

func FormatText

func FormatText(c *gin.Context, message *Message) string

FormatText ft

func GetLocalizer

func GetLocalizer(c *gin.Context) (*i18n.Localizer, bool)

GetLocalizer i18n

func MustFormat

func MustFormat(c *gin.Context, lc *i18n.LocalizeConfig) string

MustFormat must

func MustLocalizer

func MustLocalizer(c *gin.Context) *i18n.Localizer

MustLocalizer i18n

func Serve

func Serve(bundle *Bundle) gin.HandlerFunc

Serve 服务

func SetLocalizer

func SetLocalizer(c *gin.Context, l *i18n.Localizer)

SetLocalizer i18n

Types

type Bundle

type Bundle = i18n.Bundle

Bundle i18n.Bundle

func NewBundle

func NewBundle(tag language.Tag, tomls ...string) *Bundle

NewBundle new bundle

type Data

type Data = map[string]interface{}

Data TemplateData

type LocalizeConfig

type LocalizeConfig = i18n.LocalizeConfig

LocalizeConfig i18n.LocalizeConfig

type Message

type Message = i18n.Message

Message i18n.Message

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL